23

I'm using sentry-python SDK for capture exceptions from my django server.

sentry capture

I don't want to capture django.security.DisallowedHost like above. How to remove sentry handling for that logger?

I attached my server configuration below.

settings.py

LOGGING = {
    'version': 1,
    'disable_existing_loggers': False,
    'handlers': {
       'null': {
            'level': 'DEBUG',
            'class': 'logging.NullHandler',
        },
    },
    'loggers': {
        # Silence SuspiciousOperation.DisallowedHost exception ('Invalid
        # HTTP_HOST' header messages). Set the handler to 'null' so we don't
        # get those annoying emails.
        'django.security.DisallowedHost': {
            'handlers': ['null'],
            'propagate': False,
        },
    }
}

sentry_sdk.init(
    dsn=os.environ['SENTRY_DSN'],
    integrations=[DjangoIntegration()],
    send_default_pii=True,
    release=f"{os.environ['STAGE']}@{os.environ['VERSION']}",
)
Markus Unterwaditzer
  • 7,992
  • 32
  • 60
Dongeon Kim
  • 443
  • 4
  • 12

2 Answers2

29

Quick answer

See LoggingIntegration, eg:

from sentry_sdk.integrations.logging import ignore_logger


ignore_logger("a.spammy.logger")

logger = logging.getLogger("a.spammy.logger")
logger.error("hi")  # no error sent to sentry

A more elaborate but generic way to ignore events by certain characteristics

See before_breadcrumb and before_send, eg:

import sentry_sdk

def before_breadcrumb(crumb, hint):
    if crumb.get('category', None) == 'a.spammy.Logger':
        return None
    return crumb

def before_send(event, hint):
    if event.get('logger', None) == 'a.spammy.Logger':
        return None
    return event

sentry_sdk.init(before_breadcrumb=before_breadcrumb, before_send=before_send)
Florian
  • 2,562
  • 5
  • 25
  • 35
Markus Unterwaditzer
  • 7,992
  • 32
  • 60
  • 1. I'm sorry for mistypo in my question. `security.DisallowedHost` is not an exception. It is logger of Django exactly. How can i ignore some logger in sentry with DjangoIntegration? – Dongeon Kim Oct 23 '18 at 03:13
  • 2
    @kde713 I fixed my answer, please make a separate question for ignore_errors to keep this site clean – Markus Unterwaditzer Oct 23 '18 at 08:29
  • 1
    This code doesn't working. I added your code and replace `a.spammy.Logger` to `django.security.DisallowedHost`. But Invalid HTTP_HOST still sent to sentry. – Dongeon Kim Oct 24 '18 at 02:20
  • 1
    Regarding the edit I just let through: That was me amending my own answer, but I forgot to log in. The shorter way does the same exact thing. – Markus Unterwaditzer Dec 27 '18 at 14:38
9

Under the sentry_sdk I had to use the following code in before_send to get it to ignore the django.security.DisallowedHost exception.

def before_send(event, hint):
        """Don't log django.DisallowedHost errors in Sentry."""
        if 'log_record' in hint:
            if hint['log_record'].name == 'django.security.DisallowedHost':
                return None

        return event
kwiersma
  • 311
  • 3
  • 5
  • Since ignore_logger("django.security.DisallowedHost") does not work, I had to use this method. Thanks! – Jota Feb 14 '22 at 11:30