在新的 sentry-python SDK 中,将额外的字典传递给 captureException 的等价物是什么?

cra*_*tin 3 python flask sentry

将附加项传递给新的 sentry-python SDK 方法的 capture_exception 和 capture_message 的最佳方法是什么?

以前,我会: sentry_id = sentry.captureException(extra=extra)

基于文档和这个 github 问题(https://github.com/getsentry/sentry-python/issues/113),它就像以下选项之一是可比的,但我想不出办法。

使用capture_exception很接近...

except Exception as e:
    sentry_id = capture_exception(e, extra=extra) # Error
Run Code Online (Sandbox Code Playgroud)

...但不允许第二个额外的参数:(

使用python 日志记录集成非常接近......

except Exception as e:
    sentry_id = logging.error(e, exc_info=True, extra=extra)
Run Code Online (Sandbox Code Playgroud)

...但不返回哨兵ID :(

使用python 日志记录集成和 capture_exception很接近......

except Exception as e:
    logging.error(e, exc_info=True, extra=extra)
    sentry_id = capture_exception(e)
Run Code Online (Sandbox Code Playgroud)

...但在哨兵中导致两个单独的错误条目:(

使用带有 push_scope 的 capture_exception很接近...

except Exception as e:
    with push_scope() as scope:
        scope.set_extra(extra) # Error
        sentry_id = capture_exception(e)
Run Code Online (Sandbox Code Playgroud)

...但不接受字典:(

使用最后一种方法的解决方案是使用一个辅助函数将额外的 dict 解包到许多scope.set_extra(key, val)调用中吗?

谢谢您的帮助!

Mar*_*zer 5

except Exception as e:
    with push_scope() as scope:
        for k, v in extra.items():
            scope.set_extra(k, v)
        sentry_id = capture_exception(e)
Run Code Online (Sandbox Code Playgroud)

但是,我认为您设置extra的时间点错误。理想情况下,您应该在额外的上下文数据变得可用并且与当前正在执行的代码相关时立即设置它。推送作用域只是为了调用capture_exception表明您构建调用的方式存在问题set_extra

取而代之的是:

logger.error("Event via logging", extra={"foo": 42})

try:
    1/0
except Exception:
    with push_scope() as scope:
        scope.set_extra("foo", 42)
        capture_exception()
Run Code Online (Sandbox Code Playgroud)

做这个:

with push_scope() as scope:
    scope.set_extra("foo", 42)
    logger.error("Event via logging")

    try:
        1/0
    except Exception:
        capture_exception()
Run Code Online (Sandbox Code Playgroud)