Skip to content

Logging uWSGI errors and alarms to Sentry

February 02, 2022Albéric Trancart2 min read

uWSGI and Sentry logos

If you already use Sentry to manage your application errors and uWSGI as a WSGI server for your Python project (such as Flask or Django), you are one step away to improve your monitoring setup in a matter of minutes.

It is important to do so: even if you have Sentry configured for your app, if the WSGI layer that is running it fails you won't get any Sentry issue about it.

The layers of a standard Python web server: an HTTP server with a WSGI server and a Python application

With uWSGI there are some events that you want to watch for, and triggering a Sentry issue is one easy way of fitting this into your existing error management flow.

Such events include:

  • when a uWSGI worker is killed because it is serving a request for an excessive long time (the "harakiri" behavior)
  • when the listen queue of the uWSGI socket is full
  • when a segfault happens
  • or any non-applicative log line that you want to monitor

How to integrate uWSGI with Sentry

Unbit (the company behind uWSGI) wrote a plugin exactly for this use case!

Make sure that a libcurl package is installed on the machine that runs uWSGI, for instance:

apt-get install libcurl4-openssl-dev

Install the plugin before running uWSGI:

uwsgi --build-plugin

NB: this is a fork of the original plugin that makes it work for current versions of uWSGI and Sentry, I will update this article if it is merged into the main repo.

Configure the plugin in your uWSGI INI file:

plugin = sentry

; Defines what to do when the "logsentry" alarm is triggered
alarm = logsentry sentry:dsn=,logger=uwsgi.sentry

; Alarm for listen queue full
alarm-backlog = logsentry

; Alarm for segfault
alarm-segfault = logsentry

; Alarm for harakiri (alarm-log uses regexp to match log lines)
alarm-log = logsentry HARAKIRI ON WORKER

You can find more customization options in the Alarm subystem docs.


Deploy this change to your server and bam, you're done!

Example of a Harakiri issue raised in Sentry

After this, you could reflect back on what other component of your architecture you are missing alerts on.

Bonus: if you are loving Sentry as we do, here are some tips on how to use Sentry effectively.