Python - Sanitize keys in Sentry logging handler (django)

558 views Asked by At

I'm adding a Sentry logging handler to my Django project. I want to customise the sentry handler by adding sanitize keys, and two processors: raven.processors.SanitizePasswordsProcessor, raven.processors.SanitizeKeysProcessor.

Is there a way to do it in the logging configuration without writing a new handler class, wrapping the raven.contrib.django.raven_compat.handlers.SentryHandler class with the parameters I want?

This is my logging config:

LOGGING = {
'version': 1,
'disable_existing_loggers': False,
'formatters': {
    'x': {
        #'format': '[%(asctime)s #%(process)d] %(levelname)s: %(message)s'
        'format': '%(asctime)s - %(name)s  - %(levelname)s - %(message)s - {%(pathname)s:%(lineno)d}'
    }
},
'handlers': {
    'console': {
        'level': 'DEBUG',
        'class': 'logging.StreamHandler',
        'formatter': 'x'
    },
    'sentry': {
        'level': 'ERROR',
        'class': 'raven.contrib.django.raven_compat.handlers.SentryHandler'
    }
},
'loggers': {
    'django': {
        'level': 'INFO',
        'handlers': ['console', 'sentry'],
        'propagate': True
    },
}

Thank you

1

There are 1 answers

0
jwhitlock On

The LOGGING configuration is the wrong place to add these filters, and will not sanitize Sentry events.

As mentioned in the Client Arguments doc, the correct place is RAVEN_CONFIG, also in the Django settings file:

RAVEN_CONFIG = {
  'dsn': 'https://<key>:<secret>@sentry.io/<project>',
  'sanitize_keys': [
    'keyname1',
    'keyname2',
  ],
  'processors': (
    'raven.processors.SanitizeKeysProcessor',
    'raven.processors.SanitizePasswordsProcessor',
  )
}

Raven is supported for Django 1.4 to 2.0 (see Django docs). Sentry suggests the new sentry-sdk for Django 2.1 and later, and has Django integration instructions. In the Switching to Sentry-Python post, a comment mentions that Sentry SDK no longer provides these filters, and recommends writing a custom filter if needed. The send_default_pii parameter, False by default, controls recording and sending a lot of sensitive data, such as IP addresses, user details, and cookies, that were controlled by these filters in the past.