Monitoring Python Web Apps

Monitoring how your application is doing while in production is essential. It’s the only way you will be able to catch problems early and resolve them quickly to ensure the best uptime.

These tools are what I use to monitor my Python web apps which are typically built using Flask and ran via uWSGI. Of course, they can be used for any other type of Python web apps.

 Logging errors from your app

Sentry is an awesome Python application to log and aggregate errors. It provides a lot of information about each of them which really help to quickly understand what’s happening.

Sentry Image from the Sentry documentation

Sentry is provided as an hosted service on getsentry.com but is Open Source and can be installed following the steps from the quickstart guide.

Sentry can log errors from any type of application. You need a client which sends the errors to the Sentry server. The Python client is called raven.

Raven already provides the needed extension to be integrated with Flask as explained here.

 Monitoring your workers

Most of my web apps use Celery workers. Flower is a nice little application that monitors your queues and provides information on queued and running tasks.

Celery Flower Image from the Flower documentation

Monitoring uWSGI and Nginx

I love to use Munin to monitor my servers. It’s simple, quick to configure and works well. You can monitor a lot of services using Munin with the help of plugins.

Munin

To install Munin on Ubuntu, follow this tutorial: http://linhost.info/2010/06/install-munin-in-five-minutes-on-ubuntu-10-04/

I monitor my uWSGI processes using the plugin located here. Follow the simple installation steps and it should work out of the box.

Additionally, you can monitor uWSGI using uwsgitop.

Same for Nginx but Ubuntu already bundles the needed plugins. Simply symlink the plugins from /usr/share/munin/plugins (where all the available plugins are located) to /etc/munin/plugins.

ln -s /usr/share/munin/plugins/nginx_* /etc/munin/plugins

Add these lines to /etc/munin/plugin-conf.d/munin-node:

[nginx*]
env.url http://localhost/nginx_status

Finally, you’ll need to ensure that Nginx serves a page with its status information. Ubuntu does that by default but you should ensure that the following location directive is present in your default server config.

location /nginx_status {
    stub_status on;
    access_log off;
    allow 127.0.0.1;
    deny all;
  }


comments powered by Disqus

14/12/2012