Skip to content

Commit

Permalink
Move latency buckets setting to django_prometheus.conf and use it for…
Browse files Browse the repository at this point in the history
… django_http_requests_latency_including_middlewares_seconds and django_db_query_duration_seconds histograms (#343)
  • Loading branch information
AleksaC authored Jan 23, 2023
1 parent a0fd8a6 commit 1733888
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 26 deletions.
5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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/
Expand Down
21 changes: 21 additions & 0 deletions django_prometheus/conf/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
3 changes: 2 additions & 1 deletion django_prometheus/db/metrics.py
Original file line number Diff line number Diff line change
@@ -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",
Expand Down Expand Up @@ -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,
)
27 changes: 3 additions & 24 deletions django_prometheus/middleware.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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(
Expand All @@ -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(
Expand Down

0 comments on commit 1733888

Please sign in to comment.