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 configs import dify_config
  8. from core.model_runtime.errors.invoke import InvokeRateLimitError
  9. def before_send(event, hint):
  10. if "exc_info" in hint:
  11. exc_type, exc_value, tb = hint["exc_info"]
  12. if parse_error.defaultErrorResponse in str(exc_value):
  13. return None
  14. return event
  15. def init_app(app):
  16. if dify_config.SENTRY_DSN:
  17. sentry_sdk.init(
  18. dsn=dify_config.SENTRY_DSN,
  19. integrations=[FlaskIntegration(), CeleryIntegration()],
  20. ignore_errors=[
  21. HTTPException,
  22. ValueError,
  23. openai.APIStatusError,
  24. InvokeRateLimitError,
  25. parse_error.defaultErrorResponse,
  26. ],
  27. traces_sample_rate=dify_config.SENTRY_TRACES_SAMPLE_RATE,
  28. profiles_sample_rate=dify_config.SENTRY_PROFILES_SAMPLE_RATE,
  29. environment=dify_config.DEPLOY_ENV,
  30. release=f"dify-{dify_config.CURRENT_VERSION}-{dify_config.COMMIT_SHA}",
  31. before_send=before_send,
  32. )