ext_sentry.py 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738
  1. import openai
  2. import sentry_sdk
  3. from langfuse import parse_error
  4. from sentry_sdk.integrations.celery import CeleryIntegration
  5. from sentry_sdk.integrations.flask import FlaskIntegration
  6. from werkzeug.exceptions import HTTPException
  7. from core.model_runtime.errors.invoke import InvokeRateLimitError
  8. def before_send(event, hint):
  9. if "exc_info" in hint:
  10. exc_type, exc_value, tb = hint["exc_info"]
  11. if parse_error.defaultErrorResponse in str(exc_value):
  12. return None
  13. return event
  14. def init_app(app):
  15. if app.config.get("SENTRY_DSN"):
  16. sentry_sdk.init(
  17. dsn=app.config.get("SENTRY_DSN"),
  18. integrations=[FlaskIntegration(), CeleryIntegration()],
  19. ignore_errors=[
  20. HTTPException,
  21. ValueError,
  22. openai.APIStatusError,
  23. InvokeRateLimitError,
  24. parse_error.defaultErrorResponse,
  25. ],
  26. traces_sample_rate=app.config.get("SENTRY_TRACES_SAMPLE_RATE", 1.0),
  27. profiles_sample_rate=app.config.get("SENTRY_PROFILES_SAMPLE_RATE", 1.0),
  28. environment=app.config.get("DEPLOY_ENV"),
  29. release=f"dify-{app.config.get('CURRENT_VERSION')}-{app.config.get('COMMIT_SHA')}",
  30. before_send=before_send,
  31. )