Using Sentry To Track Django live Events

MicroPyramid
2 min readNov 3, 2017

--

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.

The article was originally published at MicroPyramid blog

--

--

MicroPyramid
MicroPyramid

Written by MicroPyramid

Python, Django, Android and IOS, reactjs, react-native, AWS, Salesforce consulting & development company

No responses yet