Skip to content

Commit 20b10f3

Browse files
kunalgrover05asherf
authored andcommitted
Add custom latency buckets from Settings variable
1 parent 81d68d0 commit 20b10f3

File tree

2 files changed

+32
-18
lines changed

2 files changed

+32
-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: 21 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,27 @@
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.005,
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+
float("inf"),
23+
)
24+
625

726
def _register_metric(cls, name, documentation, labelnames=tuple(), **kwargs):
827
return cls(name, documentation, labelnames=labelnames, **kwargs)
@@ -52,24 +71,8 @@ def register(self):
5271
"django_http_requests_latency_seconds_by_view_method",
5372
"Histogram of request processing time labelled by view.",
5473
["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"),
74+
buckets=getattr(
75+
settings, "PROMETHEUS_LATENCY_BUCKETS", DEFAULT_LATENCY_BUCKETS
7376
),
7477
)
7578
self.requests_unknown_latency = _register_metric(

0 commit comments

Comments
 (0)