Skip to content

Commit cc16d98

Browse files
authored
Merge pull request #182 from asherf/buckets
Add custom latency buckets from Settings variable
2 parents 81d68d0 + 08795ac commit cc16d98

File tree

2 files changed

+34
-18
lines changed

2 files changed

+34
-18
lines changed

README.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,17 @@ urlpatterns = [
6060
]
6161
```
6262

63+
### Configuration
64+
Prometheus uses Histogram based grouping for monitoring latencies. The default
65+
buckets are here: https://github.com/prometheus/client_python/blob/master/prometheus_client/core.py
66+
67+
You can define custom buckets for latency, adding more buckets decreases performance but
68+
increases accuracy: https://prometheus.io/docs/practices/histograms/
69+
70+
```
71+
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"))
72+
```
73+
6374
### Monitoring your databases
6475

6576
SQLite, MySQL, and PostgreSQL databases can be monitored. Just

django_prometheus/middleware.py

Lines changed: 23 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,29 @@
11
from prometheus_client import Counter, Histogram
22

3+
from django.conf import settings
34
from django.utils.deprecation import MiddlewareMixin
45
from django_prometheus.utils import PowersOf, Time, TimeSince
56

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

728
def _register_metric(cls, name, documentation, labelnames=tuple(), **kwargs):
829
return cls(name, documentation, labelnames=labelnames, **kwargs)
@@ -52,24 +73,8 @@ def register(self):
5273
"django_http_requests_latency_seconds_by_view_method",
5374
"Histogram of request processing time labelled by view.",
5475
["view", "method"],
55-
buckets=(
56-
0.01,
57-
0.025,
58-
0.05,
59-
0.075,
60-
0.1,
61-
0.25,
62-
0.5,
63-
0.75,
64-
1.0,
65-
2.5,
66-
5.0,
67-
7.5,
68-
10.0,
69-
25.0,
70-
50.0,
71-
75.0,
72-
float("inf"),
76+
buckets=getattr(
77+
settings, "PROMETHEUS_LATENCY_BUCKETS", DEFAULT_LATENCY_BUCKETS
7378
),
7479
)
7580
self.requests_unknown_latency = _register_metric(

0 commit comments

Comments
 (0)