From 17338882f11b06707b6fd503e8a163708058e3a7 Mon Sep 17 00:00:00 2001 From: Aleksa Cukovic Date: Mon, 23 Jan 2023 16:14:55 +0100 Subject: [PATCH] Move latency buckets setting to django_prometheus.conf and use it for django_http_requests_latency_including_middlewares_seconds and django_db_query_duration_seconds histograms (#343) --- README.md | 5 ++++- django_prometheus/conf/__init__.py | 21 +++++++++++++++++++++ django_prometheus/db/metrics.py | 3 ++- django_prometheus/middleware.py | 27 +++------------------------ 4 files changed, 30 insertions(+), 26 deletions(-) diff --git a/README.md b/README.md index d7232dfe..b1d4c192 100644 --- a/README.md +++ b/README.md @@ -72,7 +72,10 @@ urlpatterns = [ ### Configuration Prometheus uses Histogram based grouping for monitoring latencies. The default -buckets are here: https://github.com/prometheus/client_python/blob/master/prometheus_client/core.py +buckets are: +```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"),) +``` You can define custom buckets for latency, adding more buckets decreases performance but increases accuracy: https://prometheus.io/docs/practices/histograms/ diff --git a/django_prometheus/conf/__init__.py b/django_prometheus/conf/__init__.py index 09fbd378..85393936 100644 --- a/django_prometheus/conf/__init__.py +++ b/django_prometheus/conf/__init__.py @@ -2,5 +2,26 @@ NAMESPACE = "" +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"), +) + if settings.configured: NAMESPACE = getattr(settings, "PROMETHEUS_METRIC_NAMESPACE", NAMESPACE) + PROMETHEUS_LATENCY_BUCKETS = getattr(settings, "PROMETHEUS_LATENCY_BUCKETS", PROMETHEUS_LATENCY_BUCKETS) diff --git a/django_prometheus/db/metrics.py b/django_prometheus/db/metrics.py index 88c0ee64..689151e8 100644 --- a/django_prometheus/db/metrics.py +++ b/django_prometheus/db/metrics.py @@ -1,6 +1,6 @@ from prometheus_client import Counter, Histogram -from django_prometheus.conf import NAMESPACE +from django_prometheus.conf import NAMESPACE, PROMETHEUS_LATENCY_BUCKETS connections_total = Counter( "django_db_new_connections_total", @@ -46,5 +46,6 @@ "django_db_query_duration_seconds", ("Histogram of query duration by database and vendor."), ["alias", "vendor"], + buckets=PROMETHEUS_LATENCY_BUCKETS, namespace=NAMESPACE, ) diff --git a/django_prometheus/middleware.py b/django_prometheus/middleware.py index fd72d784..abcf702a 100644 --- a/django_prometheus/middleware.py +++ b/django_prometheus/middleware.py @@ -2,29 +2,9 @@ from django.utils.deprecation import MiddlewareMixin from prometheus_client import Counter, Histogram -from django_prometheus.conf import NAMESPACE +from django_prometheus.conf import NAMESPACE, PROMETHEUS_LATENCY_BUCKETS from django_prometheus.utils import PowersOf, Time, TimeSince -DEFAULT_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"), -) - class Metrics: _instance = None @@ -61,6 +41,7 @@ def register(self): "Histogram of requests processing time (including middleware " "processing time)." ), + buckets=PROMETHEUS_LATENCY_BUCKETS, namespace=NAMESPACE, ) self.requests_unknown_latency_before = self.register_metric( @@ -77,9 +58,7 @@ def register(self): "django_http_requests_latency_seconds_by_view_method", "Histogram of request processing time labelled by view.", ["view", "method"], - buckets=getattr( - settings, "PROMETHEUS_LATENCY_BUCKETS", DEFAULT_LATENCY_BUCKETS - ), + buckets=PROMETHEUS_LATENCY_BUCKETS, namespace=NAMESPACE, ) self.requests_unknown_latency = self.register_metric(