### Run Demo Prometheus Source: https://github.com/django-commons/django-prometheus/blob/master/examples/prometheus/README.md Command to start a Prometheus server with a specified configuration file and console templates. Ensure the paths to Prometheus executable and console libraries are correct for your setup. ```shell ~/prometheus/prometheus \ --config.file=prometheus.yml \ --web.console.templates consoles/ \ --web.console.libraries ~/prometheus/console_libraries/ ``` -------------------------------- ### Accessing Metrics Source: https://context7.com/django-commons/django-prometheus/llms.txt After starting the server, metrics are available at the /metrics endpoint. Example output shows common metrics. ```bash curl http://localhost:8000/metrics # Example output: # django_http_requests_total_by_method_total{method="GET"} 42.0 # django_http_responses_total_by_status_total{status="200"} 40.0 # django_http_requests_latency_seconds_by_view_method_bucket{...} ... ``` -------------------------------- ### Install django-prometheus from source Source: https://github.com/django-commons/django-prometheus/blob/master/README.md If you are using a development version cloned from the repository, install it using setup.py. ```shell python path-to-where-you-cloned-django-prometheus/setup.py install ``` -------------------------------- ### Install django-prometheus Source: https://github.com/django-commons/django-prometheus/blob/master/README.md Install the library using pip. This will also install prometheus_client as a dependency. ```shell pip install django-prometheus ``` -------------------------------- ### Programmatically Setup Prometheus Endpoint on a Port Source: https://context7.com/django-commons/django-prometheus/llms.txt Use SetupPrometheusEndpointOnPort in an AppConfig's ready method to start a standalone Prometheus HTTP server on a specific port in a background daemon thread. This must be run with --noreload. ```python # myapp/apps.py from django.apps import AppConfig from django_prometheus.exports import SetupPrometheusEndpointOnPort class MyAppConfig(AppConfig): name = 'myapp' def ready(self): # Must run with --noreload; will AssertionError if autoreloader is active SetupPrometheusEndpointOnPort(port=8001, addr='') # Metrics now available at http://0.0.0.0:8001/metrics # independently of Django request handling ``` -------------------------------- ### Run Test Django App Source: https://github.com/django-commons/django-prometheus/blob/master/CONTRIBUTING.md Start the development server for the test Django application. Metrics will be available at /metrics. ```shell cd tests/end2end/ && PYTHONPATH=../.. ./manage.py runserver ``` -------------------------------- ### Install and Configure Django Prometheus Source: https://context7.com/django-commons/django-prometheus/llms.txt Install the package using pip and add the necessary entries to your Django settings and URL configuration. ```shell pip install django-prometheus ``` ```python # settings.py INSTALLED_APPS = [ # ... 'django_prometheus', ] MIDDLEWARE = [ 'django_prometheus.middleware.PrometheusBeforeMiddleware', # ... all other Django middlewares (Session, CSRF, Security, etc.) ... 'django_prometheus.middleware.PrometheusAfterMiddleware', ] ``` ```python # urls.py from django.urls import path, include urlpatterns = [ # Exposes GET /metrics path('', include('django_prometheus.urls')), # Or under a prefix: path('monitoring/', include('django_prometheus.urls')) ] ``` -------------------------------- ### Run All Tests Source: https://github.com/django-commons/django-prometheus/blob/master/CONTRIBUTING.md Execute all unit and Django end-to-end tests. Consider running 'python setup.py install' to avoid setting PYTHONPATH repeatedly. ```shell python setup.py test cd tests/end2end/ && PYTHONPATH=../.. ./manage.py test ``` -------------------------------- ### Setup Prometheus Endpoint on a Port Range Source: https://context7.com/django-commons/django-prometheus/llms.txt Use SetupPrometheusEndpointOnPortRange to claim the first available port from a given range for serving Prometheus metrics. Returns the claimed port or None. ```python from django_prometheus.exports import SetupPrometheusEndpointOnPortRange # Attempt ports 8001–8049; first available is used claimed_port = SetupPrometheusEndpointOnPortRange(range(8001, 8050), addr='') if claimed_port: print(f"Prometheus metrics served on port {claimed_port}") else: print("No ports available — metrics not exported") # Expected output: "Prometheus metrics served on port 8001" ``` -------------------------------- ### Create /metrics Endpoint with URL Wrapper Source: https://github.com/django-commons/django-prometheus/blob/master/README.md Define a URL wrapper file to include django-prometheus's URLs, creating a '/prometheus/metrics' endpoint for exporting metrics. This is part of the setup for using the library with only local settings. ```python from django.urls import include, path urlpatterns = [] urlpatterns.append(path('prometheus/', include('django_prometheus.urls'))) urlpatterns.append(path('', include('myapp.urls'))) ``` -------------------------------- ### Configure Django Prometheus Middlewares and Apps Source: https://github.com/django-commons/django-prometheus/blob/master/README.md To use Django Prometheus without modifying the codebase, inject its middlewares and add 'django_prometheus' to INSTALLED_APPS. This setup enables default metrics collection. ```python MIDDLEWARE = \ ['django_prometheus.middleware.PrometheusBeforeMiddleware'] + \ MIDDLEWARE + \ ['django_prometheus.middleware.PrometheusAfterMiddleware'] INSTALLED_APPS += ['django_prometheus'] ``` -------------------------------- ### Configure Database Backend Monitoring Source: https://context7.com/django-commons/django-prometheus/llms.txt Replace the ENGINE value in DATABASES with the django-prometheus backend for supported engines. This enables automatic recording of database operation metrics labeled by alias and vendor. ```python # settings.py — multiple databases with monitoring DATABASES = { 'default': { 'ENGINE': 'django_prometheus.db.backends.postgresql', 'NAME': 'mydb', 'USER': 'myuser', 'PASSWORD': 'secret', 'HOST': 'localhost', }, 'analytics': { 'ENGINE': 'django_prometheus.db.backends.mysql', 'NAME': 'analytics_db', 'USER': 'analyst', 'HOST': 'mysql-host', }, 'local': { 'ENGINE': 'django_prometheus.db.backends.sqlite3', 'NAME': '/tmp/local.db', }, } # Metrics automatically recorded (labeled by alias and vendor): # django_db_new_connections_total{alias, vendor} # django_db_new_connection_errors_total{alias, vendor} # django_db_execute_total{alias, vendor} # django_db_execute_many_total{alias, vendor} # django_db_errors_total{alias, vendor, type} # django_db_query_duration_seconds{alias, vendor} ← Histogram ``` -------------------------------- ### Configure Cache Backend Monitoring Source: https://context7.com/django-commons/django-prometheus/llms.txt Replace cache BACKEND values with django-prometheus equivalents for supported cache backends. This enables automatic recording of cache operation metrics. ```python # settings.py # CACHES = { # 'default': { # 'BACKEND': 'django_prometheus.cache.backends.redis.RedisCache', # 'LOCATION': 'redis://127.0.0.1:6379/1', # } # } # Metrics automatically recorded (labeled by backend type): # django_cache_get_total{backend} # django_cache_get_multi_total{backend} # django_cache_set_total{backend} # django_cache_set_many_total{backend} # django_cache_delete_total{backend} # django_cache_delete_multi_total{backend} # django_cache_keys_total{backend} # django_cache_clear_total{backend} # django_cache_add_total{backend} # django_cache_add_multi_total{backend} # django_cache_incr_total{backend} # django_cache_decr_total{backend} # django_cache_contains_total{backend} # django_cache_has_key_total{backend} # django_cache_get_or_set_total{backend} # django_cache_set_with_key_prefix_total{backend} # django_cache_get_with_key_prefix_total{backend} # django_cache_delete_with_key_prefix_total{backend} # django_cache_incr_with_key_prefix_total{backend} # django_cache_decr_with_key_prefix_total{backend} # django_cache_close_total{backend} # django_cache_hits_total{backend} # django_cache_misses_total{backend} # django_cache_errors_total{backend, type} # django_cache_get_duration_seconds{backend} # django_cache_set_duration_seconds{backend} # django_cache_delete_duration_seconds{backend} # django_cache_close_duration_seconds{backend} ``` -------------------------------- ### Configure Multi-process Aggregated Export with uwsgi Source: https://context7.com/django-commons/django-prometheus/llms.txt Use PROMETHEUS_MULTIPROC_DIR in uwsgi.ini for multi-process aggregated export. Optionally, configure prometheus_client to use uwsgi worker IDs. ```ini # uwsgi.ini [uwsgi] env = PROMETHEUS_MULTIPROC_DIR=/run/django_metrics lazy-apps = true ``` ```python # settings.py — optional: use uwsgi worker IDs instead of PIDs to reduce file count try: import prometheus_client import uwsgi prometheus_client.values.ValueClass = prometheus_client.values.MultiProcessValue( process_identifier=uwsgi.worker_id ) except ImportError: pass ``` -------------------------------- ### Configure Cache Backends with Django Prometheus Source: https://context7.com/django-commons/django-prometheus/llms.txt Define cache backends in settings.py using django_prometheus cache backends for metrics collection. ```python # settings.py CACHES = { 'default': { 'BACKEND': 'django_prometheus.cache.backends.redis.RedisCache', 'LOCATION': 'redis://127.0.0.1:6379/1', }, 'sessions': { 'BACKEND': 'django_prometheus.cache.backends.memcached.PyMemcacheCache', 'LOCATION': '127.0.0.1:11211', }, 'local': { 'BACKEND': 'django_prometheus.cache.backends.locmem.LocMemCache', }, 'files': { 'BACKEND': 'django_prometheus.cache.backends.filebased.FileBasedCache', 'LOCATION': '/var/tmp/django_cache', }, } ``` -------------------------------- ### Export Migrations Source: https://context7.com/django-commons/django-prometheus/llms.txt Enable the export of migration counts at application startup. This setting is off by default. ```python # settings.py # Export migration counts at app startup (default: False) PROMETHEUS_EXPORT_MIGRATIONS = True ``` -------------------------------- ### Custom Latency Buckets Source: https://context7.com/django-commons/django-prometheus/llms.txt Configure custom histogram buckets for latency measurements in seconds. This allows finer control over the granularity of latency data. ```python # settings.py # Custom histogram latency buckets (in seconds) # Default: (0.01, 0.025, 0.05, 0.075, 0.1, 0.25, 0.5, 0.75, 1.0, 2.5, 5.0, 7.5, 10.0, 25.0, 50.0, 75.0, inf) PROMETHEUS_LATENCY_BUCKETS = (0.1, 0.25, 0.5, 1.0, 2.5, 5.0, 10.0, float("inf")) ``` -------------------------------- ### Configure Django Settings for Prometheus Source: https://github.com/django-commons/django-prometheus/blob/master/README.md Add 'django_prometheus' to INSTALLED_APPS and include the Prometheus middleware in your MIDDLEWARE settings. Ensure Prometheus middleware is placed correctly relative to other middlewares. ```python INSTALLED_APPS = [ ... 'django_prometheus', ... ] MIDDLEWARE = [ 'django_prometheus.middleware.PrometheusBeforeMiddleware', # All your other middlewares go here, including the default # middlewares like SessionMiddleware, CommonMiddleware, # CsrfViewmiddleware, SecurityMiddleware, etc. 'django_prometheus.middleware.PrometheusAfterMiddleware', ] ``` -------------------------------- ### Configure Dedicated Port Metrics Export Source: https://context7.com/django-commons/django-prometheus/llms.txt Set PROMETHEUS_METRICS_EXPORT_PORT and PROMETHEUS_METRICS_EXPORT_ADDRESS in settings.py to serve metrics on a specific port in a background thread. Requires running the server with --noreload. ```python # settings.py — serves metrics on port 8001 in a daemon thread # Run with: python manage.py runserver --noreload PROMETHEUS_METRICS_EXPORT_PORT = 8001 PROMETHEUS_METRICS_EXPORT_ADDRESS = '' ``` -------------------------------- ### Configure Prometheus Latency Buckets Source: https://github.com/django-commons/django-prometheus/blob/master/README.md Define custom buckets for latency monitoring. More buckets increase accuracy but decrease performance. The default buckets are provided for reference. ```python PROMETHEUS_LATENCY_BUCKETS = ( 0.01, 0.025, 0.05, 0.075, 0.1, 0.25, 0.5, 0.75, 1.0, 2.5, 5.0, 7.5, 10.0, 25.0, 50.0, 75.0, float("inf"), ) ``` ```python 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")) ``` -------------------------------- ### Assert Metric Equality and Inspect Vectors Source: https://context7.com/django-commons/django-prometheus/llms.txt Use `assert_metric_equal()` and `assert_metric_not_equal()` to check for exact metric values, including those with labels. `get_metrics_vector()` retrieves all label-value pairs for a given metric, allowing for detailed inspection. ```python from django_prometheus.testutils import assert_metric_equal, assert_metric_not_equal, get_metrics_vector # Assert exact value for a labeled metric assert_metric_equal(3.0, "django_model_inserts_total", model="article") assert_metric_not_equal(0.0, "django_model_inserts_total", model="article") # Inspect all label-value pairs currently registered for a metric vector = get_metrics_vector("django_http_requests_total_by_method_total") # Returns: [({'method': 'GET'}, 42.0), ({'method': 'POST'}, 7.0)] for labels, value in vector: print(f" {labels} → {value}") ``` -------------------------------- ### Export Metrics with a Custom Prefix Source: https://github.com/django-commons/django-prometheus/blob/master/documentation/exports.md Use a path prefix like 'monitoring/' to export metrics at a custom URL, e.g., /monitoring/metrics. Prometheus must be configured to use this new path. ```python urlpatterns = [ ... path('monitoring/', include('django_prometheus.urls')), ] ``` -------------------------------- ### Snapshot and Assert Metric Differences Source: https://context7.com/django-commons/django-prometheus/llms.txt Use `save_registry()` to snapshot the current Prometheus registry state. Then, use `assert_metric_diff()` to verify that a specific metric has changed by an exact delta between the snapshot and the current state. This is useful for testing metric increments or decrements after specific actions. ```python import pytest from django_prometheus.testutils import ( save_registry, assert_metric_diff, assert_metric_equal, assert_metric_not_equal, assert_metric_compare, get_metric, get_metrics_vector, ) class TestArticleMetrics: def test_model_insert_counter(self, db): from myapp.models import Article registry = save_registry() Article.objects.create(title="First", body="Body text") Article.objects.create(title="Second", body="More text") assert_metric_diff(registry, 2.0, "django_model_inserts_total", model="article") assert_metric_diff(registry, 0.0, "django_model_updates_total", model="article") assert_metric_diff(registry, 0.0, "django_model_deletes_total", model="article") def test_model_delete_counter(self, db): from myapp.models import Article a = Article.objects.create(title="To delete", body="...") registry = save_registry() a.delete() assert_metric_diff(registry, 1.0, "django_model_deletes_total", model="article") def test_absolute_metric_value(self): value = get_metric("django_http_requests_before_middlewares_total") assert value is not None and value >= 0 def test_metric_compare_with_predicate(self, db, client): from myapp.models import Article registry = save_registry() Article.objects.create(title="X", body="Y") # Assert counter increased (any positive delta) assert_metric_compare( registry, lambda before, after: after > (before or 0), "django_model_inserts_total", model="article", ) ``` -------------------------------- ### Middleware Configuration and Metrics Source: https://context7.com/django-commons/django-prometheus/llms.txt Configure the Prometheus middleware classes in your Django settings. PrometheusBeforeMiddleware tracks total requests and latency, while PrometheusAfterMiddleware records detailed per-request metrics. ```python # settings.py MIDDLEWARE = [ 'django_prometheus.middleware.PrometheusBeforeMiddleware', 'django.middleware.security.SecurityMiddleware', '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', ] # Metrics automatically recorded per request: # # Counters (PrometheusBeforeMiddleware): # django_http_requests_before_middlewares_total # django_http_responses_before_middlewares_total # # Histograms: # django_http_requests_latency_including_middlewares_seconds # django_http_requests_latency_seconds_by_view_method{view, method} # django_http_requests_body_total_bytes # django_http_responses_body_total_bytes # # Counters (PrometheusAfterMiddleware): # django_http_requests_total_by_method_total{method} # django_http_requests_total_by_transport_total{transport} ``` -------------------------------- ### Configure PROMETHEUS_MULTIPROC_DIR in uWSGI Source: https://github.com/django-commons/django-prometheus/blob/master/documentation/exports.md Set the PROMETHEUS_MULTIPROC_DIR environment variable in your uWSGI configuration to specify the directory for storing multi-process metrics. ```ini env = PROMETHEUS_MULTIPROC_DIR=/path/to/django_metrics ``` -------------------------------- ### Monitor Django Caches Source: https://github.com/django-commons/django-prometheus/blob/master/README.md Replace the Django cache BACKEND with the one provided by django-prometheus to enable cache metric collection. ```python CACHES = { 'default': { 'BACKEND': 'django_prometheus.cache.backends.filebased.FileBasedCache', 'LOCATION': '/var/tmp/django_cache', } } ``` -------------------------------- ### Monitor Django Databases Source: https://github.com/django-commons/django-prometheus/blob/master/README.md Replace the Django database ENGINE with the one provided by django-prometheus to enable database metric collection. ```python DATABASES = { 'default': { 'ENGINE': 'django_prometheus.db.backends.sqlite3', 'NAME': os.path.join(BASE_DIR, 'db.sqlite3'), }, } ``` -------------------------------- ### Set ROOT_URLCONF to Prometheus URL Wrapper Source: https://github.com/django-commons/django-prometheus/blob/master/README.md Update the ROOT_URLCONF setting to point to the URL wrapper file created for django-prometheus. This ensures the '/prometheus/metrics' endpoint is correctly registered. ```python ROOT_URLCONF = "graphite.urls_prometheus_wrapper" ``` -------------------------------- ### Configure Django URLs for Prometheus Source: https://github.com/django-commons/django-prometheus/blob/master/README.md Include django_prometheus.urls in your project's urls.py to expose the metrics endpoint. ```python urlpatterns = [ ... path('', include('django_prometheus.urls')), ] ``` -------------------------------- ### Django Database New Connections Rate Source: https://github.com/django-commons/django-prometheus/blob/master/examples/prometheus/consoles/django.html Visualize the rate of new database connections established by Django applications. This metric is aggregated over 30-second intervals. ```javascript new PromConsole.Graph({ node: document.querySelector("#gr_db_conn"), expr: "job:django_db_new_connections_total:sum_rate30s", name: "\[\[ alias \]\]/\[\[ vendor \] - ", yAxisFormatter: PromConsole.NumberFormatter.humanizeNoSmallPrefix, yHoverFormatter: PromConsole.NumberFormatter.humanizeNoSmallPrefix, yUnits: "/s", yTitle: "Connections", min: 0 }) ``` -------------------------------- ### Port-Range Exporter Source: https://context7.com/django-commons/django-prometheus/llms.txt Configure a range of ports for the exporter, suitable for multi-process WSGI deployments where each worker claims a port. ```python # settings.py # Port-range exporter — for multi-process WSGI (each worker claims a port) PROMETHEUS_METRICS_EXPORT_PORT_RANGE = range(8001, 8050) ``` -------------------------------- ### Settings-Only Django Prometheus Integration Source: https://context7.com/django-commons/django-prometheus/llms.txt Integrate django-prometheus by modifying `settings.py` to include the middleware and app, and by setting up a URL wrapper for the metrics endpoint. This method requires no changes to your application's code. ```python # settings.py additions MIDDLEWARE = ( ['django_prometheus.middleware.PrometheusBeforeMiddleware'] + MIDDLEWARE + ['django_prometheus.middleware.PrometheusAfterMiddleware'] ) INSTALLED_APPS += ['django_prometheus'] # urls_prometheus_wrapper.py (new file, referenced as ROOT_URLCONF) from django.urls import include, path urlpatterns = [ path('prometheus/', include('django_prometheus.urls')), # → /prometheus/metrics path('', include('myapp.urls')), ] # settings.py ROOT_URLCONF = 'myapp.urls_prometheus_wrapper' ``` -------------------------------- ### Configure Custom Metric Namespace Source: https://github.com/django-commons/django-prometheus/blob/master/README.md Set a custom namespace for your Prometheus metrics to avoid naming collisions and organize them. ```python PROMETHEUS_METRIC_NAMESPACE = "project" ``` -------------------------------- ### Configure Port-Range Metrics Export for Multi-process WSGI Source: https://context7.com/django-commons/django-prometheus/llms.txt Define PROMETHEUS_METRICS_EXPORT_PORT_RANGE in settings.py for multi-process WSGI servers like uwsgi or gunicorn. Ensure preload_app is False in gunicorn.conf.py. ```python # settings.py — each worker claims the first available port PROMETHEUS_METRICS_EXPORT_PORT_RANGE = range(8001, 8050) ``` ```python # gunicorn.conf.py — must disable preloading so each worker initializes independently preload_app = False ``` ```yaml # prometheus.yml scrape config scrape_configs: - job_name: django static_configs: - targets: - localhost:8001 - localhost:8002 - localhost:8003 ``` -------------------------------- ### Use ExportToDjangoView for Metrics Rendering Source: https://context7.com/django-commons/django-prometheus/llms.txt Map the ExportToDjangoView function in urls.py to render Prometheus metrics. It automatically handles single-process and multi-process scenarios. ```python # Custom URL mapping (alternative to include('django_prometheus.urls')) from django.urls import path from django_prometheus.exports import ExportToDjangoView urlpatterns = [ path('internal/prom-metrics', ExportToDjangoView, name='metrics'), ] # curl http://localhost:8000/internal/prom-metrics # Content-Type: text/plain; version=0.0.4; charset=utf-8 # # # HELP django_http_requests_before_middlewares_total Total count of requests before middlewares run. # # TYPE django_http_requests_before_middlewares_total counter # django_http_requests_before_middlewares_total 157.0 # ... ``` -------------------------------- ### Enable Migration Metrics in Django Prometheus Source: https://context7.com/django-commons/django-prometheus/llms.txt Set PROMETHEUS_EXPORT_MIGRATIONS to True in settings.py to expose applied and unapplied migration counts as Prometheus gauges. ```python # settings.py PROMETHEUS_EXPORT_MIGRATIONS = True ``` ```yaml # Example Prometheus alert rule (prometheus.yml): # - alert: UnappliedMigrations # expr: django_migrations_unapplied_total > 0 # for: 5m # labels: # severity: warning # annotations: # summary: "Unapplied migrations on {{ $labels.connection }}" ``` -------------------------------- ### Define Custom Application Metrics Source: https://context7.com/django-commons/django-prometheus/llms.txt Define custom metrics like Counters, Histograms, and Gauges using `prometheus_client`. These custom metrics are automatically included in the `/metrics` endpoint when django-prometheus is integrated. ```python # myapp/metrics.py from prometheus_client import Counter, Histogram, Gauge orders_created = Counter( 'myapp_orders_created_total', 'Total number of orders created', ['payment_method', 'region'], ) order_value_dollars = Histogram( 'myapp_order_value_dollars', 'Distribution of order values', buckets=[5, 10, 25, 50, 100, 250, 500, 1000, float("inf")], ) active_sessions = Gauge( 'myapp_active_sessions', 'Number of currently active user sessions', ) # myapp/views.py from myapp.metrics import orders_created, order_value_dollars, active_sessions def create_order(request): # ... order logic ... orders_created.labels(payment_method='credit_card', region='us-east').inc() order_value_dollars.observe(149.99) return JsonResponse({'status': 'created'}) ``` -------------------------------- ### Display SQL Query and Results Source: https://github.com/django-commons/django-prometheus/blob/master/django_prometheus/tests/end2end/testapp/templates/sql.html This snippet displays the executed SQL query and its results. It is conditionally rendered only if a query has been submitted. ```html {% for d in databases %}{{ d }}{% endfor %} {% if query %}{{ query }}{% else %} SELECT * FROM testapp_lawn; {% endif %} {% if query %}

Your query was:

{{ query }}

Your results were:

{{ rows }}

{% endif %} ``` -------------------------------- ### Custom Metric Namespace Source: https://context7.com/django-commons/django-prometheus/llms.txt Set a custom metric namespace to prefix all generated metric names. This helps in organizing metrics from different applications. ```python # settings.py # Custom metric namespace — prefixes all metric names with "myapp_" # Default: "" (no prefix) PROMETHEUS_METRIC_NAMESPACE = "myapp" # Result: myapp_django_http_requests_total_by_method_total{method="GET"} 1.0 ``` -------------------------------- ### Dedicated Port Exporter Source: https://context7.com/django-commons/django-prometheus/llms.txt Configure a dedicated port for serving metrics on a separate HTTP thread, independent of the Django application. Note that this is incompatible with Django's autoreloader. ```python # settings.py # Dedicated port exporter — serves /metrics on a separate HTTP thread (not Django) # Incompatible with Django's autoreloader; use --noreload PROMETHEUS_METRICS_EXPORT_PORT = 8001 PROMETHEUS_METRICS_EXPORT_ADDRESS = "" # bind to all interfaces ``` -------------------------------- ### Django Requests Rate by View Source: https://github.com/django-commons/django-prometheus/blob/master/examples/prometheus/consoles/django.html Monitor the HTTP request rate broken down by individual Django views. This helps identify performance bottlenecks within specific endpoints. Aggregated over 30-second intervals. ```javascript new PromConsole.Graph({ node: document.querySelector("#gr_requests_byview"), expr: "job:django_http_requests_total_by_view:sum_rate30s", name: "\[\[ view \] - ", yAxisFormatter: PromConsole.NumberFormatter.humanizeNoSmallPrefix, yHoverFormatter: PromConsole.NumberFormatter.humanizeNoSmallPrefix, yUnits: "/s", yTitle: "Requests", renderer: "area", min: 0 }) ``` -------------------------------- ### Enable Thread-Based Metrics Export Source: https://github.com/django-commons/django-prometheus/blob/master/documentation/exports.md Configure Prometheus metrics to be exported in a separate daemon thread using PROMETHEUS_METRICS_EXPORT_PORT and PROMETHEUS_METRICS_EXPORT_ADDRESS. This prevents Django app issues from affecting metric availability. Note: This is incompatible with Django's autoreloader and will assert-fail if active. ```python PROMETHEUS_METRICS_EXPORT_PORT = 8001 PROMETHEUS_METRICS_EXPORT_ADDRESS = '' # all addresses ``` -------------------------------- ### Django Database Queries Rate Source: https://github.com/django-commons/django-prometheus/blob/master/examples/prometheus/consoles/django.html Visualize the rate of database queries executed by Django applications. This metric is aggregated over 30-second intervals. ```javascript new PromConsole.Graph({ node: document.querySelector("#gr_db_execs"), expr: "job:django_db_execute_total:sum_rate30s", name: "\[\[ alias \]\]/\[\[ vendor \] - ", yAxisFormatter: PromConsole.NumberFormatter.humanizeNoSmallPrefix, yHoverFormatter: PromConsole.NumberFormatter.humanizeNoSmallPrefix, yUnits: "/s", yTitle: "Queries", min: 0 }) ``` -------------------------------- ### Configure WSGI Metrics Export with Port Range Source: https://github.com/django-commons/django-prometheus/blob/master/documentation/exports.md For WSGI applications with multiple processes, use PROMETHEUS_METRICS_EXPORT_PORT_RANGE to specify a range of ports for metrics export. Django-Prometheus will attempt to use each port sequentially if the previous one is occupied. This approach requires the application to be loaded into each child process. ```python PROMETHEUS_METRICS_EXPORT_PORT_RANGE = range(8001, 8050) ``` -------------------------------- ### Use uWSGI Worker ID for Process Identification Source: https://github.com/django-commons/django-prometheus/blob/master/documentation/exports.md Modify prometheus_client's ValueClass to use uwsgi.worker_id for process identification. This is an internal interface and may change in future versions. Ensure this is set before any metrics are created. ```python try: import prometheus_client import uwsgi prometheus_client.values.ValueClass = prometheus_client.values.MultiProcessValue( process_identifier=uwsgi.worker_id) except ImportError: pass # not running in uwsgi ``` -------------------------------- ### Extend Middleware Metrics with Custom Labels Source: https://context7.com/django-commons/django-prometheus/llms.txt Extend Metrics to add extra label dimensions to any middleware metric by wiring up custom middleware subclasses. Ensure to update your settings.py to include the custom middleware. ```python # myapp/middleware.py from django_prometheus.middleware import Metrics, PrometheusBeforeMiddleware, PrometheusAfterMiddleware EXTENDED_METRICS = [ "django_http_requests_latency_seconds_by_view_method", "django_http_responses_total_by_status_view_method", ] class CustomMetrics(Metrics): def register_metric(self, metric_cls, name, documentation, labelnames=(), **kwargs): if name in EXTENDED_METRICS: labelnames = list(labelnames) + ["user_type", "app_version"] return super().register_metric(metric_cls, name, documentation, labelnames=labelnames, **kwargs) class CustomBeforeMiddleware(PrometheusBeforeMiddleware): metrics_cls = CustomMetrics class CustomAfterMiddleware(PrometheusAfterMiddleware): metrics_cls = CustomMetrics def label_metric(self, metric, request, response=None, **labels): if metric._name in EXTENDED_METRICS: labels = { "user_type": "authenticated" if request.user.is_authenticated else "anonymous", "app_version": "2.0", **labels, } return super().label_metric(metric, request, response=response, **labels) # settings.py MIDDLEWARE = [ 'myapp.middleware.CustomBeforeMiddleware', # ... other middlewares ... 'myapp.middleware.CustomAfterMiddleware', ] ``` -------------------------------- ### Django Total Requests Rate Source: https://github.com/django-commons/django-prometheus/blob/master/examples/prometheus/consoles/django.html Visualize the total HTTP request rate for Django applications. Use this to monitor overall traffic volume. The metric is aggregated over 30-second intervals. ```javascript new PromConsole.Graph({ node: document.querySelector("#gr_requests_total"), expr: "job:django_http_requests_total:sum_rate30s", name: "Requests", yAxisFormatter: PromConsole.NumberFormatter.humanizeNoSmallPrefix, yHoverFormatter: PromConsole.NumberFormatter.humanizeNoSmallPrefix, yUnits: "/s", yTitle: "Requests", min: 0 }) ``` -------------------------------- ### Django Model Insertions Rate Source: https://github.com/django-commons/django-prometheus/blob/master/examples/prometheus/consoles/django.html Visualize the rate of model insertions per second in Django applications. This metric is aggregated over 1-minute intervals. ```javascript new PromConsole.Graph({ node: document.querySelector("#gr_model_inserts"), expr: "job:django_model_inserts_total:sum_rate1m", yAxisFormatter: PromConsole.NumberFormatter.humanizeNoSmallPrefix, yHoverFormatter: PromConsole.NumberFormatter.humanizeNoSmallPrefix, yUnits: "/s", yTitle: "Insertions", min: 0 }) ``` -------------------------------- ### Django 99.9th Percentile Request Latency Source: https://github.com/django-commons/django-prometheus/blob/master/examples/prometheus/consoles/django.html Monitor the tail latency (99.9th percentile) of Django HTTP requests. This helps understand worst-case response times. Aggregated over 30 seconds, displayed in seconds. ```javascript new PromConsole.Graph({ node: document.querySelector("#gr_requests_latency_tail"), expr: "job:django_http_requests_latency_seconds:quantile_rate30s{quantile=\"99.9\"}", name: "tail latency", xUnits: "s", yAxisFormatter: PromConsole.NumberFormatter.humanize, yHoverFormatter: PromConsole.NumberFormatter.humanize, yTitle: "s", min: 0 }) ``` -------------------------------- ### Django Database New Connection Errors Rate Source: https://github.com/django-commons/django-prometheus/blob/master/examples/prometheus/consoles/django.html Monitor the rate of errors encountered when establishing new database connections in Django. This metric is aggregated over 30-second intervals. ```javascript new PromConsole.Graph({ node: document.querySelector("#gr_db_connerr"), expr: "job:django_db_new_connection_erros_total:sum_rate30s", name: "\[\[ alias \]\]/\[\[ vendor \] - ", yAxisFormatter: PromConsole.NumberFormatter.humanizeNoSmallPrefix, yHoverFormatter: PromConsole.NumberFormatter.humanizeNoSmallPrefix, yUnits: "/s", yTitle: "Connection errors", min: 0 }) ``` -------------------------------- ### Monitor Model Operations with ExportModelOperationsMixin Source: https://github.com/django-commons/django-prometheus/blob/master/README.md Add ExportModelOperationsMixin to your Django models to automatically export metrics for insert, update, and delete operations. This mixin is safe to add to existing models without requiring migrations. ```python from django_prometheus.models import ExportModelOperationsMixin class Dog(ExportModelOperationsMixin('dog'), models.Model): name = models.CharField(max_length=100, unique=True) breed = models.CharField(max_length=100, blank=True, null=True) age = models.PositiveIntegerField(blank=True, null=True) ``` -------------------------------- ### Django Median Request Latency Source: https://github.com/django-commons/django-prometheus/blob/master/examples/prometheus/consoles/django.html Track the median latency of Django HTTP requests. This metric indicates typical response times. It uses a 30-second rate aggregation and displays time in seconds. ```javascript new PromConsole.Graph({ node: document.querySelector("#gr_requests_latency_median"), expr: "job:django_http_requests_latency_seconds:quantile_rate30s{quantile=\"50\"}", name: "median latency", xUnits: "s", yAxisFormatter: PromConsole.NumberFormatter.humanize, yHoverFormatter: PromConsole.NumberFormatter.humanize, yTitle: "s", min: 0 }) ``` -------------------------------- ### Django Model Updates Rate Source: https://github.com/django-commons/django-prometheus/blob/master/examples/prometheus/consoles/django.html Monitor the rate of model updates per second in Django applications. This metric is aggregated over 1-minute intervals. ```javascript new PromConsole.Graph({ node: document.querySelector("#gr_model_updates"), expr: "job:django_model_updates_total:sum_rate1m", yAxisFormatter: PromConsole.NumberFormatter.humanizeNoSmallPrefix, yHoverFormatter: PromConsole.NumberFormatter.humanizeNoSmallPrefix, yUnits: "/s", yTitle: "Updates", min: 0 }) ``` -------------------------------- ### Django Model Deletions Rate Source: https://github.com/django-commons/django-prometheus/blob/master/examples/prometheus/consoles/django.html Track the rate of model deletions per second in Django applications. This metric is aggregated over 1-minute intervals. ```javascript new PromConsole.Graph({ node: document.querySelector("#gr_model_deletes"), expr: "job:django_model_deletes_total:sum_rate1m", yAxisFormatter: PromConsole.NumberFormatter.humanizeNoSmallPrefix, yHoverFormatter: PromConsole.NumberFormatter.humanizeNoSmallPrefix, yUnits: "/s", yTitle: "Deletions", min: 0 }) ``` -------------------------------- ### Disable Autoreloader for Thread-Based Export Source: https://github.com/django-commons/django-prometheus/blob/master/documentation/exports.md When using the thread-based exporter, ensure Django's autoreloader is disabled by passing '--noreload' to manage.py. This is crucial because the autoreloader forks processes that would conflict with the dedicated metrics port. ```python execute_from_command_line(sys.argv + ['--noreload']) ``` -------------------------------- ### Django Database Errors Rate Source: https://github.com/django-commons/django-prometheus/blob/master/examples/prometheus/consoles/django.html Monitor the rate of errors occurring during database operations in Django applications. This metric is aggregated over 30-second intervals. ```javascript new PromConsole.Graph({ node: document.querySelector("#gr_db_errs"), expr: "job:django_db_errors_total:sum_rate30s", name: "\[\[ alias \]\]/\[\[ vendor \] - ", yAxisFormatter: PromConsole.NumberFormatter.humanizeNoSmallPrefix, yHoverFormatter: PromConsole.NumberFormatter.humanizeNoSmallPrefix, yUnits: "/s", yTitle: "Errors", min: 0 }) ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.