sentry是一个监控系统, 可以在函数内部抛出异常时, 发送消息给sentry
1 注册账号
2 创建项目
3 代码示例
pip install —upgrade ‘sentry-sdk[flask]’
import sentry_sdk
from sentry_sdk.integrations.flask import FlaskIntegration
sentry_sdk.init(
dsn="https://e4613ce97dcb45839414a9b44afea304@o1056884.ingest.sentry.io/6048968",
integrations=[FlaskIntegration()],
# Set traces_sample_rate to 1.0 to capture 100%
# of transactions for performance monitoring.
# We recommend adjusting this value in production.
traces_sample_rate=1.0
)
@app.route('/debug-sentry')
def trigger_error():
import sentry_sdk
try:
division_by_zero = 1 / 0
except Exception as e: # 加了异常处理就要自己主动发
sentry_sdk.capture_message('Something went wrong', level="warning")
return jsonify(errno=RET.DBERR, errmsg='除0错误')