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
Querying with Django Q objects Dinesh Deshmukh

Querying with Django Q objects: Q object encapsulates a SQL expression in a Python object that can be used in database-related operations. Using Q objects ...

Continue Reading...
How to customize the admin actions in list pages of Django admin? Chaitanya Kattineni

Django by default provides automatic admin interface, that reads metadata from your models to provide a beautiful model interface where trusted users can manage content ...

Continue Reading...
Django Forms bascis explained Vamsi Popuri

Django forms is powerful module to help django application development in rendering html from model, validate input from http request to model specifications. And we ...

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