{"id":2480,"library":"django-prometheus","title":"Django Prometheus","description":"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.","status":"active","version":"2.4.1","language":"en","source_language":"en","source_url":"https://github.com/django-commons/django-prometheus","tags":["django","prometheus","monitoring","metrics","observability"],"install":[{"cmd":"pip install django-prometheus","lang":"bash","label":"Install stable version"}],"dependencies":[{"reason":"Core framework integration; requires Django >= 4.2 as of version 2.4.1.","package":"Django","optional":false},{"reason":"Prometheus Python client library for metrics handling; installed as a dependency.","package":"prometheus_client","optional":false}],"imports":[{"note":"Add 'django_prometheus' to your Django project's INSTALLED_APPS.","symbol":"django_prometheus","correct":"INSTALLED_APPS = [... 'django_prometheus', ...]"},{"note":"Place this middleware early in your MIDDLEWARE list.","symbol":"PrometheusBeforeMiddleware","correct":"MIDDLEWARE = ['django_prometheus.middleware.PrometheusBeforeMiddleware', ...]"},{"note":"Place this middleware late in your MIDDLEWARE list, after other core Django middlewares.","symbol":"PrometheusAfterMiddleware","correct":"MIDDLEWARE = [... 'django_prometheus.middleware.PrometheusAfterMiddleware']"},{"note":"Include 'django_prometheus.urls' to expose the /metrics endpoint in your project's main urls.py.","symbol":"django_prometheus.urls","correct":"from django.urls import path, include\nurlpatterns = [... path('', include('django_prometheus.urls')), ...]"},{"note":"Used to add creation/deletion/update rate metrics to Django models.","symbol":"ExportModelOperationsMixin","correct":"from django_prometheus.models import ExportModelOperationsMixin\nclass MyModel(ExportModelOperationsMixin('mymodel'), models.Model): ..."}],"quickstart":{"code":"import os\n\n# settings.py\nSECRET_KEY = os.environ.get('DJANGO_SECRET_KEY', 'your-secret-key')\nDEBUG = os.environ.get('DJANGO_DEBUG', 'True') == 'True'\n\nINSTALLED_APPS = [\n    # ... other Django apps\n    'django.contrib.admin',\n    'django.contrib.auth',\n    'django.contrib.contenttypes',\n    'django.contrib.sessions',\n    'django.contrib.messages',\n    'django.contrib.staticfiles',\n    'django_prometheus',\n]\n\nMIDDLEWARE = [\n    'django_prometheus.middleware.PrometheusBeforeMiddleware',\n    'django.contrib.sessions.middleware.SessionMiddleware',\n    'django.middleware.common.CommonMiddleware',\n    'django.middleware.csrf.CsrfViewMiddleware',\n    'django.contrib.auth.middleware.AuthenticationMiddleware',\n    'django.contrib.messages.middleware.MessageMiddleware',\n    'django.middleware.clickjacking.XFrameOptionsMiddleware',\n    'django_prometheus.middleware.PrometheusAfterMiddleware',\n]\n\n# urls.py (in your project's main urls.py)\nfrom django.contrib import admin\nfrom django.urls import path, include\n\nurlpatterns = [\n    path('admin/', admin.site.urls),\n    path('', include('django_prometheus.urls')),\n    # Your other app URLs\n]\n\n# After running the Django dev server, metrics will be available at /metrics/","lang":"python","description":"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/`)."},"warnings":[{"fix":"Upgrade your Django and Python versions to supported ones (Django >= 4.2, Python >= 3.9 for 2.4.1) before upgrading django-prometheus.","message":"Version 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.","severity":"breaking","affected_versions":">=2.4.0"},{"fix":"Ensure your project runs on Python 3.7+ and Django 3.2+ before upgrading to django-prometheus 2.3.0 or newer.","message":"Version 2.3.0 removed support for Python 3.6 and Django versions older than 3.2.","severity":"breaking","affected_versions":">=2.3.0"},{"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.","message":"The 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.","severity":"gotcha","affected_versions":"All versions"},{"fix":"Set `PROMETHEUS_EXPORT_MIGRATIONS = True` in your `settings.py` if you wish to continue monitoring Django migrations.","message":"Since 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.","severity":"gotcha","affected_versions":">=2.3.0"},{"fix":"Upgrade to django-prometheus version 2.3.0 or newer to ensure all latency metrics consistently respect the `PROMETHEUS_LATENCY_BUCKETS` setting.","message":"Prior 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.","severity":"gotcha","affected_versions":"<2.3.0"}],"env_vars":null,"last_verified":"2026-04-10T00:00:00.000Z","next_check":"2026-07-09T00:00:00.000Z"}