Как игнорировать некоторые ошибки с помощью Sentry (не отправлять их)?

У меня есть проект, основанный на django (3.2.10) и sentry-sdk (1.16.0). Есть мой файл sentry-init:

from os import getenv


SENTRY_URL = getenv('SENTRY_URL')

if SENTRY_URL:
    from sentry_sdk import init
    from sentry_sdk.integrations.django import DjangoIntegration
    from sentry_sdk.integrations.redis import RedisIntegration
    from sentry_sdk.integrations.celery import CeleryIntegration

    init(
        dsn=SENTRY_URL,
        integrations=[DjangoIntegration(), RedisIntegration(), CeleryIntegration()],
        traces_sample_rate=1.0,
        send_default_pii=True,
        debug=True,
    )

У меня есть CustomError, унаследованный от Exception

Каждый раз, когда я поднимаю CustomError sentry-sdk отправляет его на dsn-url.

Я хочу игнорировать некоторый класс ошибок или что-то в этом роде. Как я могу это сделать?

Вы можете передать функцию, которая фильтрует ошибки для отправки:

from os import getenv


SENTRY_URL = getenv('SENTRY_URL')

if SENTRY_URL:
    from sentry_sdk import init
    from sentry_sdk.integrations.django import DjangoIntegration
    from sentry_sdk.integrations.redis import RedisIntegration
    from sentry_sdk.integrations.celery import CeleryIntegration
  
    def before_send(event, hint):
       if 'exc_info' in hint:
          exc_type, exc_value, tb = hint['exc_info']
          if isinstance(exc_value, CustomError):  # Replace CustomError with your custom error 
             return None
       return event

    init(
        dsn=SENTRY_URL,
        integrations=[DjangoIntegration(), RedisIntegration(), CeleryIntegration()],
        traces_sample_rate=1.0,
        send_default_pii=True,
        debug=True,
        before_send=before_send
    )

Больше информации вы можете найти в документации.

Вернуться на верх