How do you raise a python exception and include additional data for Sentry?

AJP picture AJP · Apr 11, 2013 · Viewed 7.9k times · Source

Sentry can detect additional data associated with an exception such as:

enter image description here

How do you raise such an exception from Python (it's a Django app) with your own additional data fields?.

Answer

wes picture wes · Oct 16, 2013

I log exceptions using the logging library so after debugging the code a bit, I noticed the extra parameter:

import logging
logger = logging.getLogger('my_app_name')

def do_something():

    try:
        #do some stuff here that might break

    except Exception, e:
        logger.error(e, exc_info=1, extra={'extra-data': 'blah', })

Passing exc_info=1 is the same as calling logger.exception. However, exception() does not accept kwargs, which are required for using the extra parameter.

These values will show up in the 'Additional Data' section of the Sentry Error dashboard.