ext_sentry.py 1.4 KB

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