How to monitor django application live events with sentry

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
Extract text with OCR for all image types in python using pytesseract Shirisha Gaddi

Optical Character Recognition(OCR) is the process of electronically extracting text from images or any documents like PDF and reusing it in a variety of ways ...

Continue Reading...
E-commerce (paypal) integration with Django Ashwin Kumar

E-commerce is integration is becoming almost essential for every web application now a days. There are so many payment gateways to integrate with our application. ...

Continue Reading...
Understanding 'GenericForeignKey' in Django Ashwin Kumar

In some cases the we might want to store generic model object, rather a particular specific model as 'ForeignKey'. Generic model object means adding a ...

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