Registry / observability / django-prometheus

django-prometheus

JSON →
library2.5.0pypypi✓ verified 26d ago

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.

pip install django-prometheus
INSTALL
IMPORT
SIG · DJANGO-PROMETHEUS
D
django-prometheus
observabilitypythonv2.5.0
Install
3.6s avg
Import
415ms
Disk
66MB
Pass rate
10/ 10
Env Coverage10 / 10
glibc
3.93.13
musl
3.93.13
Install & Compatibility
Where this runs
tested against v2.5.0 · pip install
no network on importno background threads
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
py 3.103.95 runs
installs and imports cleanly · install 0.0s · import 0.436s · 67.3MB
glibc
py 3.103.95 runs
installs and imports cleanly · install 3.6s · import 0.394s · 68MB
66MB installed
● package 66MB
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

django_prometheus
import django_prometheus
django_prometheus

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/
Debug
Known issues
breakingVersion 2.4.0 dropped support for Django 3.2 (and Python 3.7), Django 4.0, and Django 4.1. It added support for Django 5.0, 5.1, 5.2 and Python 3.12, 3.13.
fix
Upgrade your Django and Python versions to supported ones (Django >= 4.2, Python >= 3.9 for 2.4.1) before upgrading django-prometheus.
affects: >=2.4.0
breakingVersion 2.3.0 removed support for Python 3.6 and Django versions older than 3.2.
fix
Ensure your project runs on Python 3.7+ and Django 3.2+ before upgrading to django-prometheus 2.3.0 or newer.
affects: >=2.3.0
gotchaThe order of `PrometheusBeforeMiddleware` and `PrometheusAfterMiddleware` in your `MIDDLEWARE` setting is crucial. `PrometheusBeforeMiddleware` should be placed very early, and `PrometheusAfterMiddleware` very late, to properly wrap other core Django middlewares for accurate metric collection.
fix
Always follow the recommended order: `PrometheusBeforeMiddleware` at the start, `PrometheusAfterMiddleware` at the end, with other middlewares in between. Consult the official documentation for the exact order.
affects: All versions
gotchaSince version 2.3.0, the `PROMETHEUS_EXPORT_MIGRATIONS` setting defaults to `False`. If you previously relied on metrics for Django migrations, they will no longer be exported unless explicitly enabled.
fix
Set `PROMETHEUS_EXPORT_MIGRATIONS = True` in your `settings.py` if you wish to continue monitoring Django migrations.
affects: >=2.3.0
gotchaPrior to version 2.3.0, there was a fix for two latency metrics not correctly using the `PROMETHEUS_LATENCY_BUCKETS` setting. This could lead to inconsistent bucket behavior for custom latency configurations.
fix
Upgrade to django-prometheus version 2.3.0 or newer to ensure all latency metrics consistently respect the `PROMETHEUS_LATENCY_BUCKETS` setting.
affects: <2.3.0
Upgrade
Version history
2.5.0latest on PyPI · released May 26, 2026
Audit
Dependencies
DjangorequiredCore framework integration; requires Django >= 4.2 as of version 2.4.1.
prometheus_clientrequiredPrometheus Python client library for metrics handling; installed as a dependency.
Agent activity
24 hits · last 30 days
node
20
OpenAI (training)
2
Resources
django-prometheus — pip install django-prometheus · libregistry