How to monitor a Django application with Prometheus

Prerequisites

A machine with the following installed:

tip

Since machines commonly have multiple versions of Python installed, in this tutorial we call pip using the python -m pip [foo] syntax instead of the pip [foo] syntax. This is to ensure that pip installs new components for the version of Python that we are using.

(Please skip this step if you already have a Django application.)

More info on Django.

Create a template project

Navigate to the directory where you want to create the project, and run:

  1. django-admin startproject mysite

This creates a mysite directory in your current directory, that looks something like this (more here):

  1. mysite/
  2. manage.py
  3. mysite/
  4. __init__.py
  5. settings.py
  6. urls.py
  7. asgi.py
  8. wsgi.py

Verify that Django is working

Change to the outer mysite directory, and run:

  1. python manage.py runserver

You should see something like this:

If that works, then visit http://127.0.0.1:8000/ from your Web browser. You should see a “Congratulations!” page, with a rocket taking off.

Step 2 - Export prometheus-style monitoring metrics from your Django application

We use the package for exporting prometheus-style monitoring metrics from our Django application.

  1. python -m pip install django-prometheus

Modify settings.py and urls.py

In settings.py, add:

  1. INSTALLED_APPS = [
  2. ...
  3. ...
  4. ]
  5. MIDDLEWARE = [
  6. 'django_prometheus.middleware.PrometheusBeforeMiddleware',
  7. # All your other middlewares go here, including the default
  8. # middlewares like SessionMiddleware, CommonMiddleware,
  9. # CsrfViewmiddleware, SecurityMiddleware, etc.
  10. 'django_prometheus.middleware.PrometheusAfterMiddleware',
  11. ]

In urls.py, make sure you have this in the header:

  1. from django.conf.urls import include, path

Then add this under urlpatterns:

Verify that metrics are being exported

Restart the application and curl the /metrics endpoint:

  1. python manage.py runserver
  2. curl localhost:8000/metrics

(Alternatively, once you’ve restarted your application, visit from your web browser.)

You should see something like this:

  1. # HELP python_gc_objects_collected_total Objects collected during gc
  2. # TYPE python_gc_objects_collected_total counter
  3. python_gc_objects_collected_total{generation="0"} 11716.0
  4. python_gc_objects_collected_total{generation="1"} 1699.0
  5. python_gc_objects_collected_total{generation="2"} 616.0
  6. # HELP python_gc_objects_uncollectable_total Uncollectable object found during GC
  7. # TYPE python_gc_objects_uncollectable_total counter
  8. python_gc_objects_uncollectable_total{generation="0"} 0.0
  9. python_gc_objects_uncollectable_total{generation="1"} 0.0
  10. python_gc_objects_uncollectable_total{generation="2"} 0.0
  11. # HELP python_gc_collections_total Number of times this generation was collected
  12. # TYPE python_gc_collections_total counter
  13. python_gc_collections_total{generation="0"} 7020.0
  14. python_gc_collections_total{generation="1"} 638.0
  15. python_gc_collections_total{generation="2"} 34.0
  16. # HELP python_info Python platform information
  17. # TYPE python_info gauge
  18. python_info{implementation="CPython",major="3",minor="8",patchlevel="0",version="3.8.0"} 1.0
  19. ...

(Note: This section assumes that you have a locally running Prometheus instance.)

Under scrape_configs:, add:

  1. - job_name: django
  2. scrape_interval: 10s
  3. static_configs:
  4. - targets:
  5. - localhost:8000
note

Restart Prometheus

Verify that Prometheus is scraping metrics from your Django application:

Once you are running Prometheus locally, visit the from your web browser.

For example, you can visit the below page, which graphs the total number of http requests your Django application received in the last hour:

Graph of Django HTTP requests, served on localhost

It should look something like this:

If you’d like to do more testing, visit your Django application several more times and reload the Prometheus Expression Browser to confirm that it is working. Also feel free to explore the other Django metrics that Prometheus is collecting.

Step 4 - Instrument additional aspects of your application (optional)

Django-prometheus is quite powerful, and allows you to easily instrument additional aspects of your application, including:

  • Your databases
  • Your models (for example, monitor the creation/deletion/update rate for your models)
  • Your caches
  • Your own custom metrics in your code

More information on how to do all of these is .

Congratulations! Now you are monitoring your Django application with Prometheus!