Ignore exceptions in sentry-sdk

Using the following setup code for sentry-sdk in Python/Django but unable to ignore DisallowedHost exceptions. Am I missing something?

import sentry_sdk
from sentry_sdk.integrations.django import DjangoIntegration
from django.core.exceptions import DisallowedHost

ignore_errors = (DisallowedHost, )

def before_send(event, hint):
    if 'exc_info' in hint:
        exc_type, exc_value, tb = hint['exc_info']
        if isinstance(exc_value, ignore_errors):
            return None
    return event

sentry_sdk.init(
    dsn=os.environ.get('RAVEN_DSN', ''),
    integrations=[DjangoIntegration()],
    before_send=before_send,
)

Hi, I think the issue is that DisallowedHost is a logger. Please add a call like this to your code: sentry_sdk.integrations.logging.ignore_logger("django.security.DisallowedHost").

1 Like

Thanks. I tried both forms of your solution from https://stackoverflow.com/questions/52927353/how-to-ignore-a-logger-in-the-sentry-python-sdk but neither seem to work from the Django settings file.

Attempt 1

import sentry_sdk
from sentry_sdk.integrations.django import DjangoIntegration
from sentry_sdk.integrations.logging import ignore_logger

sentry_sdk.init(
    dsn=os.environ.get('RAVEN_DSN', ''),
    integrations=[DjangoIntegration()],
)

ignore_logger('django.security.DisallowedHost').

Attempt 2

import sentry_sdk
from sentry_sdk.integrations.django import DjangoIntegration

def before_breadcrumb(crumb, hint):
    if crumb.get('category', None) == 'django.security.DisallowedHost':
        return None
    return crumb

def before_send(event, hint):
    if event.get('logger', None) == 'django.security.DisallowedHost':
        return None
    return event

sentry_sdk.init(
    before_breadcrumb=before_breadcrumb,
    before_send=before_send,
    dsn=os.environ.get('RAVEN_DSN', ''),
    integrations=[DjangoIntegration()],
)

Could you post a link to a sentry issue that you don’t want to see?

It worked for me, replying here so that it may help someone.

def before_send(event, hint):
    if "log_record" in hint:
        if hint["log_record"].name == "django.security.DisallowedHost":
            return None
        return event

Originally posted here: https://stackoverflow.com/a/60794429