Search code examples
djangosentryraven

Configuring Sentry handler for Django with new sentry_sdk without raven


The new sentry_sdk for django provides very brief installation (with intergration via raven marked for deprecation).

import sentry_sdk
from sentry_sdk.integrations.django import DjangoIntegration

sentry_sdk.init(
  dsn="https://<key>@sentry.io/<project>",
  integrations=[DjangoIntegration()]
)

Previously, one would configure sentry with raven as the handler class like this.

'handlers': {
    'sentry': {
        'level': 'ERROR', # To capture more than ERROR, change to WARNING, INFO, etc.
        'class': 'raven.contrib.django.raven_compat.handlers.SentryHandler',
        'tags': {'custom-tag': 'x'},
    },
...
...
'loggers':{
       'project.custom':{
             'level': 'DEBUG',
             'handlers': ['sentry', 'console', ,]
       }

See more as defined here

The challenge is that raven does not accept the new format of the SENTRY_DSN. in the format https://<key>@domain.com/project/ The old format is along the lines of https://<key>:<secret>@domain.com/project. Raven will throw InvalidDSN with the old format. The old DSN Key is marked for deprecation.

The documentation is very silent on how to define handlers. and clearly raven which has been deprecated is not happy with the new key format. I could rely on the old DSN deprecated format but will appreciate advice on how to configure the handler with the new format.


Solution

  • I have taken some reading and practicing. The new version does not depend on raven in any way. So you have to remove all references to raven. in settings.py and any references to the raven client.

    There is no need to worry about the handler for sentry_sdk. It will suffice to declare the handler for the console only and append that handler to every other logger defined as required.

    'handlers':{
        'console': {
            'level': 'DEBUG',
            'class': 'logging.StreamHandler',
            'formatter': 'verbose'
        }
        # No need to define a sentry handler, defined by the integration.
    }
    'loggers':{
       'project.custom': {
           'level': 'DEBUG',
           'handlers': ['console', ], # You dont have to add sentry handler here
       }
    }
    

    Also NOTE that logger.exception will just be ignored, you will have to use capture_exception from sentry_sdk or capture_message from sentry_sdk

    Apparently, it is possible that an event is not logged into sentry for a number of reasons, however logger.exception should always work