Using Sentry To Track Django live Events

Reading Time : ~ .

In the previous tutorial you saw how to setup sentry, let us now learn how to track exceptions and events in SENTRY. We will setup client and server end tracking for Django Project.

Note: My domain is set as "http://sentry.domain.com". Now after first login the screen should be similar to this.

As per Sentry's Internal Architecture, Team will have access to projects. Create a team and assign a project to team, every member in team will now have access to it. As per your convineance. any combination of members can grouped as teams (which can be managed in Team Settings option in Dashboard page). 

Creating Team and Project. 

  1. Click on Create Team button in Dashboard.
  2. decide a Team Name and save changes
  3. create a Project.
  4. In Project DashBoard> Settings >  Client Keys, you can find DSN (reffered in this tutorial as dsn) and DSN (Public) - (Reffered in this tutorial as public-dsn)

Modifications for Django Application.

The recieving end is sentry but error tracker and logger is RAVEN, Install raven in Django Project  environment 

pip install raven 

open settings.py

include the below lines in settings.py

RAVEN_CONFIG = {
    'dsn': '<your-dsn-here>',
}

LOGGING = {
    'version': 1,
    'disable_existing_loggers': True,
    'root': {
        'level': 'WARNING',
        'handlers': ['sentry'],
    },
    'formatters': {
        'verbose': {
            'format': '%(levelname)s %(asctime)s %(module)s '
                      '%(process)d %(thread)d %(message)s'
        },
    },
    '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'},
        },
        'console': {
            'level': 'DEBUG',
            'class': 'logging.StreamHandler',
            'formatter': 'verbose'
        }
    },
    'loggers': {
        'django.db.backends': {
            'level': 'ERROR',
            'handlers': ['console'],
            'propagate': False,
        },
        'raven': {
            'level': 'DEBUG',
            'handlers': ['console'],
            'propagate': False,
        },
        'sentry.errors': {
            'level': 'DEBUG',
            'handlers': ['console'],
            'propagate': False,
        },
    },
}

INSTALLED_APPS = INSTALLED_APPS + ('raven.contrib.django.raven_compat',)

MIDDLEWARE_CLASSES = MIDDLEWARE_CLASSES + (
    'raven.contrib.django.raven_compat.middleware.Sentry404CatchMiddleware',
    'raven.contrib.django.raven_compat.middleware.SentryResponseErrorIdMiddleware',
)

These lines will override Django Logging Module, setup raven tracking, Since Django Standard logging is compatible with sentry we can also log custom events  with error handlers.

With This above configuration we can track all wvents,exceptions, 404's(included in middleware) in Django Web Application Server.

Client Side Tracking (Browser errors):

Include this snippet in header file which will be imported to all pages(base page).

     <script type="text/javascript">
          Raven.config('<your-public-dsn-here>').install();
        window.onerror = Raven.process;
      </script>

Whenever Browser encounters an error, it is immediately relayed to sentry.  

Sentry does support event tracking of almost any kind of web/mobile/desktop application. Click on this link for other SDK Integrations.

 

    By Posted On
SENIOR DEVELOPER at MICROPYRAMID

Need any Help in your Project?Let's Talk

Latest Comments
Related Articles
Using Django's built in signals and writing custom signals. Divya Sri

Django has a beautiful feature of signals which will record all the actions performed on particular model. In the current blog post we’ll learn how ...

Continue Reading...
Dynamically Adding Google Maps with Marker In Django Nikhila Mergu

Google Maps allows you to display maps on your web site, we can also customize maps, and the information on maps.
The Google Maps API ...

Continue Reading...
Factory Boy - An alternative for Fixtures Shirisha Gaddi

Factory Boy is a fixtures replacement tool. It allows you to use objects customized for the current test, while only declaring the test-specific fields. For ...

Continue Reading...

Subscribe To our news letter

Subscribe to our news letter to receive latest blog posts into your inbox. Please fill your email address in the below form.
*We don't provide your email contact details to any third parties