Skip to main content
client_python
GitHub Toggle Dark/Light/Auto mode Toggle Dark/Light/Auto mode Toggle Dark/Light/Auto mode Back to homepage

Django

To use Prometheus with Django you can use the provided view class to add a metrics endpoint to your app.

# urls.py

from django.urls import path
from prometheus_client.django import PrometheusDjangoView

urlpatterns = [
    # ... any other urls that you want
    path("metrics/", PrometheusDjangoView.as_view(), name="prometheus-metrics"),
    # ... still more urls
]

By default, Multiprocessing support is activated if environment variable PROMETHEUS_MULTIPROC_DIR is set. You can override this through the view arguments:

from django.conf import settings

urlpatterns = [
    path(
        "metrics/",
        PrometheusDjangoView.as_view(
            multiprocess_mode=settings.YOUR_SETTING  # or any boolean value
        ),
        name="prometheus-metrics",
    ),
]

Full multiprocessing instructions are provided here.

django-prometheus

The included PrometheusDjangoView is useful if you want to define your own metrics from scratch.

An external package called django-prometheus can be used instead if you want to get a bunch of ready-made monitoring metrics for your Django application and easily benefit from utilities such as models monitoring.