Skip to content

Commit 1733888

Browse files
authored
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)
1 parent a0fd8a6 commit 1733888

File tree

4 files changed

+30
-26
lines changed

4 files changed

+30
-26
lines changed

README.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,10 @@ urlpatterns = [
7272
### Configuration
7373

7474
Prometheus uses Histogram based grouping for monitoring latencies. The default
75-
buckets are here: https://github.com/prometheus/client_python/blob/master/prometheus_client/core.py
75+
buckets are:
76+
```python
77+
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"),)
78+
```
7679

7780
You can define custom buckets for latency, adding more buckets decreases performance but
7881
increases accuracy: https://prometheus.io/docs/practices/histograms/

django_prometheus/conf/__init__.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,26 @@
22

33
NAMESPACE = ""
44

5+
PROMETHEUS_LATENCY_BUCKETS = (
6+
0.01,
7+
0.025,
8+
0.05,
9+
0.075,
10+
0.1,
11+
0.25,
12+
0.5,
13+
0.75,
14+
1.0,
15+
2.5,
16+
5.0,
17+
7.5,
18+
10.0,
19+
25.0,
20+
50.0,
21+
75.0,
22+
float("inf"),
23+
)
24+
525
if settings.configured:
626
NAMESPACE = getattr(settings, "PROMETHEUS_METRIC_NAMESPACE", NAMESPACE)
27+
PROMETHEUS_LATENCY_BUCKETS = getattr(settings, "PROMETHEUS_LATENCY_BUCKETS", PROMETHEUS_LATENCY_BUCKETS)

django_prometheus/db/metrics.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
from prometheus_client import Counter, Histogram
22

3-
from django_prometheus.conf import NAMESPACE
3+
from django_prometheus.conf import NAMESPACE, PROMETHEUS_LATENCY_BUCKETS
44

55
connections_total = Counter(
66
"django_db_new_connections_total",
@@ -46,5 +46,6 @@
4646
"django_db_query_duration_seconds",
4747
("Histogram of query duration by database and vendor."),
4848
["alias", "vendor"],
49+
buckets=PROMETHEUS_LATENCY_BUCKETS,
4950
namespace=NAMESPACE,
5051
)

django_prometheus/middleware.py

Lines changed: 3 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -2,29 +2,9 @@
22
from django.utils.deprecation import MiddlewareMixin
33
from prometheus_client import Counter, Histogram
44

5-
from django_prometheus.conf import NAMESPACE
5+
from django_prometheus.conf import NAMESPACE, PROMETHEUS_LATENCY_BUCKETS
66
from django_prometheus.utils import PowersOf, Time, TimeSince
77

8-
DEFAULT_LATENCY_BUCKETS = (
9-
0.01,
10-
0.025,
11-
0.05,
12-
0.075,
13-
0.1,
14-
0.25,
15-
0.5,
16-
0.75,
17-
1.0,
18-
2.5,
19-
5.0,
20-
7.5,
21-
10.0,
22-
25.0,
23-
50.0,
24-
75.0,
25-
float("inf"),
26-
)
27-
288

299
class Metrics:
3010
_instance = None
@@ -61,6 +41,7 @@ def register(self):
6141
"Histogram of requests processing time (including middleware "
6242
"processing time)."
6343
),
44+
buckets=PROMETHEUS_LATENCY_BUCKETS,
6445
namespace=NAMESPACE,
6546
)
6647
self.requests_unknown_latency_before = self.register_metric(
@@ -77,9 +58,7 @@ def register(self):
7758
"django_http_requests_latency_seconds_by_view_method",
7859
"Histogram of request processing time labelled by view.",
7960
["view", "method"],
80-
buckets=getattr(
81-
settings, "PROMETHEUS_LATENCY_BUCKETS", DEFAULT_LATENCY_BUCKETS
82-
),
61+
buckets=PROMETHEUS_LATENCY_BUCKETS,
8362
namespace=NAMESPACE,
8463
)
8564
self.requests_unknown_latency = self.register_metric(

0 commit comments

Comments
 (0)