Skip to main content
 首页 » 编程设计

python之删除 Sentry 中的当前面包屑

2024年11月24日8langtianya

我有这样简单的同步代码:

import sentry_sdk 
from time import sleep 
 
 
sentry_sdk.init(MY_DSN) 
while True: 
    # remove sentry breadcrumbs here, so they would not accumulate 
    try: 
        do_my_stuff() 
    except: 
        handle_my_exception() 
 
    sleep(some_non_linear_algorithm())  # I would like not to use crontab 

当异常发生时,sentry 会捕获它以及之前迭代中的所有面包屑,因此我需要在每次迭代开始时删除所有当前面包屑。但是我在 Sentry 文档中找不到任何 API 来执行此操作。

请您参考如下方法:

将您的代码包装在此:

with sentry_sdk.push_scope(): 
    try: 
        ... 
    except: 
        ... 

这将确保您不会在迭代之间泄漏面包屑、标签等。

或者你可以使用:

with sentry_sdk.configure_scope() as scope: 
    scope.clear()