mozilla

Configuring Django with stacktraces routed through Heka

Integrating heka into Django to capture stacktraces requires doing a couple things.

  1. Setup Raven+Django integration
  2. Setup Heka clients
  3. Switch the standard Sentry client and instead use HekaDjangoClient

Step 1

Setup the sentry client (raven) for use with Django.

Setting up raven is documented completely in the raven-python documentation <http://raven.readthedocs.org/en/latest/>. To get the extra integration with Django, you will need to do the following steps:

  1. Add raven.contrib.django to your INSTALLED_APPS in settings.py
  2. Set SENTRY_CLIENT to ‘raven.contrib.django.DjangoClient’ in settings.py
  3. Set the SENTRY_DSN in settings.py

Here’s a typical configuration in settings.py

my_dsn = 'http://public:secret@example.com/1'

INSTALLED_APPS = (
    # You probably have more apps here
    'raven.contrib.django',
)

RAVEN_CONFIG = {
    # Forcing register_signals to True will make raven ignore the
    # DEBUG flag when trying to capture stack traces
    'register_signals': True,

    # The DSN should point to your Sentry instance.  See Sentry
    # documentation for details.
    'dsn': my_dsn
}

SENTRY_CLIENT = 'raven.contrib.django.DjangoClient'
SENTRY_DSN = my_dsn

The above settings should capture all stacktraces and route through the standard raven client and send messages on to Sentry.

Unforunately at this time, you will need to have the DSN configured in both the RAVEN_CONFIG and the SENTRY_DSN entries in settings.py

Step 2

Setup heka with just the DebugCaptureStream. Please refer to the full documentation on configuring heka to get a production setup.

Add the following to your settings.py

HEKA_CONF = {
    'stream_class': 'heka.streams.DebugCaptureStream',
}

from heka.config import client_from_dict_config
HEKA = client_from_dict_config(HEKA_CONF)

Step 3

Reconfigure Raven to use heka as it’s underlying sentry client. Just change the SENTRY_CLIENT setting in settings.py to use the alternate SENTRY_CLIENT

SENTRY_CLIENT = 'djangoraven.heka.HekaDjangoClient'

Due to the way that raven sets a project_id variable for Sentry, you will need to explicitly set the SENTRY_DSN setting

SENTRY_DSN = "udp://your:dsn@goeshere.com:9001/2"

At this point, you can safely remove the DSN entry in RAVEN_CONFIG. The HekaDjangoClient does not need it. Final routing of your stacktrace is handled by heka.

That’s it! Your raven messages will route through heka.

See the test suite to see how you can inspect messages when you’re testing.