Django Prometheus

2.4.1 · active · verified Fri Apr 10

django-prometheus is a Python library that provides Prometheus metrics for Django applications. It offers automatic instrumentation for HTTP requests, database queries, and cache operations, as well as features for exposing custom application-level metrics. The library is actively maintained, with version 2.4.1 being the latest release, and regularly updates to support new Django and Python versions.

Warnings

Install

Imports

Quickstart

To quickly integrate django-prometheus, add 'django_prometheus' to your `INSTALLED_APPS`. Then, include `PrometheusBeforeMiddleware` and `PrometheusAfterMiddleware` in your `MIDDLEWARE` list, ensuring they wrap other core Django middlewares. Finally, include `django_prometheus.urls` in your project's `urls.py` to expose the metrics endpoint (typically at `/metrics/`).

import os

# settings.py
SECRET_KEY = os.environ.get('DJANGO_SECRET_KEY', 'your-secret-key')
DEBUG = os.environ.get('DJANGO_DEBUG', 'True') == 'True'

INSTALLED_APPS = [
    # ... other Django apps
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'django_prometheus',
]

MIDDLEWARE = [
    'django_prometheus.middleware.PrometheusBeforeMiddleware',
    'django.contrib.sessions.middleware.SessionMiddleware',
    'django.middleware.common.CommonMiddleware',
    'django.middleware.csrf.CsrfViewMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
    'django.contrib.messages.middleware.MessageMiddleware',
    'django.middleware.clickjacking.XFrameOptionsMiddleware',
    'django_prometheus.middleware.PrometheusAfterMiddleware',
]

# urls.py (in your project's main urls.py)
from django.contrib import admin
from django.urls import path, include

urlpatterns = [
    path('admin/', admin.site.urls),
    path('', include('django_prometheus.urls')),
    # Your other app URLs
]

# After running the Django dev server, metrics will be available at /metrics/

view raw JSON →