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
How to customize Django Oscar Models, Views and URL's Chaitanya Kattineni

In order to customize models, views and urls, you need to fork an Oscar core app in which model/view resides. Then you should be able ...

Continue Reading...
Improving page speed score in Google Page Score test- PART2 Chaitanya Kattineni

In our previous blog post we got an idea of what are the things that Google's Page Speed Insights will take into count to give ...

Continue Reading...
Django Conditional Expressions in Queries Anjaneyulu Batta

Reduce database queries in django with Conditional Expressions. By using Conditional Expressions we can use "If...Elif...Else" expressions while querying the database so that we can ...

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