Skip to main content

Prometheus client wrapper for django or django rest framework based applications.

Project description


Python prometheus library for django and django rest framework. This helps in monitoring the application on a granular level. You can customize which part of the application you want to monitor. Through this you can monitor a REST API, a python function , a code segment.



  • Django >= 1.8
  • djangorestframework >= 3.0
  • prometheus_client >= 0.7.1


Install with:

pip install prometheus-python

Or, if you're using a development version cloned from this repository:

git clone
python prometheus/ install

This will install Django >= 1.8 and djangorestframework >= 3.0 and prometheus_client as a dependency if not installed already.


In your


In your

urlpatterns = [
    url('', include('prometheus.urls')),

In your

from rest_framework import generics, status
from rest_framework.response import Response
from prometheus import monitor

class TestAPIView(generics.RetrieveAPIView):

    @monitor(app_name="test") # app_name should be unique through out the application.
    def retrieve(self, request, *args, **kwargs):
        data = {}
        return Response(data, status=status.HTTP_200_OK)

So as you can see in the above example I have decorated the retrieve function by our monitor decorator which will provide monitoring metrics for this function only. And you can identify how much time this function is taking to execute, how many requests are in progress currently, how many request totally served till now.

Metrics are exposed to:


Default list of monitored metrics

* request_count
* request_latency
* request_in_progress
* response_by_status_total


Prometheus uses Histogram based grouping for monitoring latencies. The default buckets are here:

You can define custom buckets for latency, adding more buckets decreases performance but increases accuracy:

In your

PROMETHEUS_LATENCY_BUCKETS = (.1, .2, .5, .6, .8, 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.5, 9.0, 12.0, 15.0, 20.0, 30.0, float("inf"))

Monitor in multiprocess mode (uWSGI, Gunicorn)

In your

PROMETHEUS_MULTIPROC_MODE = True # default is False
PROMETHEUS_MULTIPROC_DIR = /path/to/prometheus_multiproc_dir # default it will save db files in prometheus/multiproc_dir/

Monitoring of Batch Jobs

So in prometheus legacy system we have to collect the metrics and push those metrics to the pushgateway and then prometheus server has to scrape those metrics from push gateway. But now I have modified this apporach. Now I have exposed an endpoint in this prometheus client to push your metrics.

So as usual you must be running prometheus client with server (Django, Django Rest Framework).

In these settings is actually where your server is running.


In your any

from prometheus import batch_monitor

def sum(a,b):
   return a+b

sum(10, 20)

So here this batch_monitor decorator will push the metrics to you server and add monitored metrics into your server's metrics.

Default Batch Job Monitored Metrics

* request_count
* time_taken
* last_success
* Last_failure

These metrics can be seen at /metrics endpoint.

Project details

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

prometheus-python-1.0.3.tar.gz (3.6 kB view hashes)

Uploaded Source

Built Distribution

prometheus_python-1.0.3-py2.py3-none-any.whl (7.2 kB view hashes)

Uploaded Python 2 Python 3

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page