Capturing Errors with Sentry

When deploying applications into production, the challenge then becomes keeping on top of any errors that may be thrown during day-to-day operation.

In the bad old days, you might put in some error trapping to log to a disk file (and then remember to check this file regularly) or send yourself an email.

Thankfully, we've largely outgrown this approach, and these days there's a number of services which provide online error tracking, such as Raygun; Paul Irish has a list of similar services.

If you want to install an error tracking service on your own hardware, then Sentry is a pretty good choice, being a Python application that is available in both SAAS and OpenSource versions.

Sentry is the product of David Cramer (an engineer at Dropbox) and Chris Jennings (a designer at Github) and was started while they were at Disqus. In an interview on LeanStack they mention that they consider themselves fortunate enough to have good day jobs such that Sentry can thrive as a OpenSource project:

Yeah, so we open-sourced it because we wanted people to use it. Not because we wanted to create this thing and make money off of it. And it's still the same thing today. A lot of people don't pay for Sentry. I would say 90% of people just host it themselves...But those 90% of people who aren't paying us are directly making Sentry better for the people who are paying us so it works.

The Sentry dashboard, showing a number of different errors

Sentry provides an HTTP API to allow your application to capture and send it information about errors, typically achieved using the officially sanctioned Raven libraries, and Raven provides error collectors for languages such as PHP, Python, Ruby, Node, and client-side JavaScript (a full list can be found in the Sentry docs).

Installation

Although I initially tried to install Sentry using pip and easy_install, I ran into some problems with reported errors not making it onto the dashboard, so ended up installing from source.

The following notes were developed through deploying Sentry on an Ubuntu VPS, but should be broadly similar for any Linux-like environment.

As I'm a fan of Ansible, I include some of the steps below as Ansible scripts; if you're not conversant with Ansible, don't worry as you should be able to figure out the equivalent shell commands from the scripts without too much problem.

Here we install a number of python-based packages, as well as Node from the officially-sanctioned PPA. Note that we had to change wheezy to lucid in the PPA definition as it appears the PPA is not yet supporting wheezy:

Create our virtual environment

We want to use a python virtual environment to isolate our Sentry python dependencies, and ensure that we can run different versions of python for other applications.

We also install our Sentry conf — normally this is installed using sentry init once Sentry has been installed, however we want to template our Sentry conf for use with Ansible and customise a few of the directives, such as using MySQL rather than the default SQLite. More details about the Sentry configuration can be found on the Sentry site.

The important part of the sentry.conf is the SENTRY_URL_PREFIX, which should be set to your domain:

Importantly, the above script will create our virtual environment (in /home/sentry/.virtualenvs/sentry-env) and will add the following line to our .bashrc, which ensures that the virtual environment is loaded when we run Sentry under our sentry user:

source $HOME/.virtualenvs/{{ sentry.env}}/bin/activate

Installing Sentry

Now that we've done the hard work of setting up the environment, the actual install of Sentry is quite easy: