-
Notifications
You must be signed in to change notification settings - Fork 245
/
test_middleware_custom_labels.py
110 lines (97 loc) · 3.74 KB
/
test_middleware_custom_labels.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
import pytest
from prometheus_client import REGISTRY
from prometheus_client.metrics import MetricWrapperBase
from testapp.helpers import get_middleware
from testapp.test_middleware import M, T
from django_prometheus.middleware import (
Metrics,
PrometheusAfterMiddleware,
PrometheusBeforeMiddleware,
)
from django_prometheus.testutils import assert_metric_diff, save_registry
EXTENDED_METRICS = [
M("requests_latency_seconds_by_view_method"),
M("responses_total_by_status_view_method"),
]
class CustomMetrics(Metrics):
def register_metric(self, metric_cls, name, documentation, labelnames=(), **kwargs):
if name in EXTENDED_METRICS:
labelnames.extend(("view_type", "user_agent_type"))
return super().register_metric(metric_cls, name, documentation, labelnames=labelnames, **kwargs)
class AppMetricsBeforeMiddleware(PrometheusBeforeMiddleware):
metrics_cls = CustomMetrics
class AppMetricsAfterMiddleware(PrometheusAfterMiddleware):
metrics_cls = CustomMetrics
def label_metric(self, metric, request, response=None, **labels):
new_labels = labels
if metric._name in EXTENDED_METRICS:
new_labels = {"view_type": "foo", "user_agent_type": "browser"}
new_labels.update(labels)
return super().label_metric(metric, request, response=response, **new_labels)
class TestMiddlewareMetricsWithCustomLabels:
@pytest.fixture(autouse=True)
def _setup(self, settings):
settings.MIDDLEWARE = get_middleware(
"testapp.test_middleware_custom_labels.AppMetricsBeforeMiddleware",
"testapp.test_middleware_custom_labels.AppMetricsAfterMiddleware",
)
# Allow CustomMetrics to be used
for metric in Metrics._instance.__dict__.values():
if isinstance(metric, MetricWrapperBase):
REGISTRY.unregister(metric)
Metrics._instance = None
def test_request_counters(self, client):
registry = save_registry()
client.get("/")
client.get("/")
client.get("/help")
client.post("/", {"test": "data"})
assert_metric_diff(registry, 4, M("requests_before_middlewares_total"))
assert_metric_diff(registry, 4, M("responses_before_middlewares_total"))
assert_metric_diff(registry, 3, T("requests_total_by_method"), method="GET")
assert_metric_diff(registry, 1, T("requests_total_by_method"), method="POST")
assert_metric_diff(registry, 4, T("requests_total_by_transport"), transport="http")
assert_metric_diff(
registry,
2,
T("requests_total_by_view_transport_method"),
view="testapp.views.index",
transport="http",
method="GET",
)
assert_metric_diff(
registry,
1,
T("requests_total_by_view_transport_method"),
view="testapp.views.help",
transport="http",
method="GET",
)
assert_metric_diff(
registry,
1,
T("requests_total_by_view_transport_method"),
view="testapp.views.index",
transport="http",
method="POST",
)
assert_metric_diff(
registry,
2.0,
T("responses_total_by_status_view_method"),
status="200",
view="testapp.views.index",
method="GET",
view_type="foo",
user_agent_type="browser",
)
assert_metric_diff(
registry,
1.0,
T("responses_total_by_status_view_method"),
status="200",
view="testapp.views.help",
method="GET",
view_type="foo",
user_agent_type="browser",
)