将额外内容传递给capture_exception和capture_message的新哨兵python SDK方法的最佳方式是什么?
以前,我会: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
..。但不允许第二个额外的参数:(
使用python日志记录集成非常接近...
except Exception as e:
sentry_id = logging.error(e, exc_info=True, extra=extra)
..。但是不返回一个卫兵id :(
使用的python日志记录集成和capture_exception很接近...
except Exception as e:
logging.error(e, exc_info=True, extra=extra)
sentry_id = capture_exception(e)
..。但是会在sentry中产生两个单独的错误条目:(
将capture_exception与push_scope配合使用已接近...
except Exception as e:
with push_scope() as scope:
scope.set_extra(extra) # Error
sentry_id = capture_exception(e)
..。但不接受字典:(
使用最后一种方法的解决方案是使用辅助函数,该函数将额外的字典解压到许多scope.set_extra(key, val)
调用中吗?
谢谢你的帮助!
发布于 2018-12-07 04:32:33
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)
但是,我认为您在错误的时间点设置了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()
执行以下操作:
with push_scope() as scope:
scope.set_extra("foo", 42)
logger.error("Event via logging")
try:
1/0
except Exception:
capture_exception()
https://stackoverflow.com/questions/53634836
复制相似问题