Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix two latency metrics not using PROMETHEUS_LATENCY_BUCKETS setting #343

Merged
merged 1 commit into from
Jan 23, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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