sentry是一个监控系统, 可以在函数内部抛出异常时, 发送消息给sentry

1 注册账号

https://sentry.io/

2 创建项目

3 代码示例

pip install —upgrade ‘sentry-sdk[flask]’

  1. import sentry_sdk
  2. from sentry_sdk.integrations.flask import FlaskIntegration
  3. sentry_sdk.init(
  4. dsn="https://e4613ce97dcb45839414a9b44afea304@o1056884.ingest.sentry.io/6048968",
  5. integrations=[FlaskIntegration()],
  6. # Set traces_sample_rate to 1.0 to capture 100%
  7. # of transactions for performance monitoring.
  8. # We recommend adjusting this value in production.
  9. traces_sample_rate=1.0
  10. )
  11. @app.route('/debug-sentry')
  12. def trigger_error():
  13. import sentry_sdk
  14. try:
  15. division_by_zero = 1 / 0
  16. except Exception as e: # 加了异常处理就要自己主动发
  17. sentry_sdk.capture_message('Something went wrong', level="warning")
  18. return jsonify(errno=RET.DBERR, errmsg='除0错误')

image.png
image.png