time

Setting Sentry for error alarm

dj_sentry.png

If you explore about django's exceptions through mail reports than you should try Sentry. It can aggregat error reports from different projects in one place with beautiful dashboard.

We'll install sentry to server and raven to client. I expect that you use virtualenv.

According to this you should use Python 2.7.

Server

Install dependency

sudo apt-get install python-setuptools python-pip python-dev libxslt1-dev gcc libffi-dev libjpeg-dev 
libxml2-dev libxslt-dev libyaml-dev

Install PostgreSQL and setup it

sudo apt-get update
sudo apt-get install postgresql postgresql-contrib
sudo passwd postgres
su - postgres -c "initdb --locale en_US.UTF-8 -D '/var/lib/postgres/data'"
su - postgres
createdb -E utf-8 sentry

Install Redis

sudo apt-get install redis-server

or install Redis from source

sudo apt-get install build-essential tcl8.5
wget http://download.redis.io/releases/redis-stable.tar.gz
tar xzf redis-stable.tar.gz
cd redis-stable
make
make test
sudo make install
# redis comes with a script that enables it to run in the background
cd utils
sudo ./install_server.sh

Redis is used as the default implementation for various backend services, including the time-series storage, SQL update buffers, and rate limiting.

Install sentry

mkvirtualenv -p /usr/bin/python2 sentryenv
pip install sentry

Now you’ll need to create the default configuration.

sentry init /path/to/sentry/

Starting with 8.0.0, init now creates two files, sentry.conf.py and config.yml.

Next we update config file to set correct url for postgresql and disable user registration

# vim /path/to/sentry/sentry.conf.py

DATABASES = {
    'default': {
        'ENGINE': 'sentry.db.postgres',
        'NAME': 'sentry',
        'USER': 'sentry',
        'PASSWORD': 'password',
        'HOST': '',
    }
}

SENTRY_FEATURES['auth:register'] = False

Once done, you can create the initial schema using the upgrade command and create the first user, which will act as a superuser

export SENTRY_CONF=/path/to/sentry 
sentry upgrade

Last steps is to setup nginx for web access to our dashboard and supervisor for sentry start.

Install nginx

sudo apt-get install nginx-full

Edit nginx's config file

# sudo vim /etc/nginx/sites-enabled/sentry.conf

server {
    server_name sentry.example.com www.sentry.example.com;

    access_log /var/log/nginx/sentry.access.log;
    error_log /var/log/nginx/sentry.error.log;

    location / {
        proxy_pass http://localhost:9000;
        proxy_redirect off;
        proxy_read_timeout 5m;
        proxy_set_header Host            $host;
        proxy_set_header X-Real-IP       $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    }
}

Reload nginx

sudo service nginx reload

Now we'll setup supervisor and start three processes

  • Sentry provides a built-in webserver (powered by uWSGI) to get you off the ground quickly, sentry run web.
  • A large amount of Sentry’s work is managed via background workers. These need run in addition to the web service workers, sentry run worker.
  • Sentry also needs a cron process, sentry run cron.

Install supervisor

sudo apt-get install supervisor

Edit supervisor's config file

# sudo vim /etc/supervisor/conf.d/sentry.conf

[program:sentry-web]
user=www
group=www
environment=SENTRY_CONF="/home/www/example.com/subdomains/sentry"
directory=/home/www/example.com/subdomains/sentry
command=/home/www/.virtualenvs/sentri/bin/sentry start
autostart=true
autorestart=true
redirect_stderr=True
daemon = False
debug = False
stdout_logfile=/home/www/example.com/logs/supervisor_sentry_web.log

[program:sentry-worker]
user=www
group=www
environment=SENTRY_CONF="/home/www/example.com/subdomains/sentry"
directory=/home/www/example.com/subdomains/sentry
command=/home/www/.virtualenvs/sentri/bin/sentry run worker
autostart=true
autorestart=true
redirect_stderr=True
daemon = False
debug = False
stdout_logfile=/home/www/example.com/logs/supervisor_sentry_worker.log

[program:sentry-cron]
user=www
group=www
environment=SENTRY_CONF="/home/www/example.com/subdomains/sentry"
directory=/home/www/example.com/subdomains/sentry
command=/home/www/.virtualenvs/sentri/bin/sentry run cron
autostart=true
autorestart=true
redirect_stderr=True
daemon = False
debug = False
stdout_logfile=/home/www/example.com/logs/supervisor_sentry_worker.log

Update supervisor

sudo supervisorctl reread
sudo supervisorctl update

Open sentry.example.com with your browser and create project for logging.

Client

Install raven

pip install raven

Add 'raven.contrib.django.raven_compat', to the list of installed apps

Add RAVEN_CONFIG (generated when we created project in sentry web interface) to settins.py

import raven

RAVEN_CONFIG = {
    'dsn': 'http://key1:key2@sentry.example.com/2',
}

That's it! Raven automatically installs an error handling hook to pipe all uncaught exceptions to Sentry.

If you wish to test your connection to the server, you can use the raven test command:

python manage.py raven test
comments powered by Disqus