forked from perdy/starlette-prometheus
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_end_to_end.py
149 lines (112 loc) · 5.05 KB
/
test_end_to_end.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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
import pytest
from starlette.applications import Starlette
from starlette.responses import PlainTextResponse
from starlette.testclient import TestClient
from starlette_prometheus import PrometheusMiddleware, metrics
class TestCasePrometheusMiddleware:
@pytest.fixture(scope="class")
def app(self):
app_ = Starlette()
app_.add_middleware(PrometheusMiddleware)
app_.add_route("/metrics/", metrics)
@app_.route("/foo/")
def foo(request):
return PlainTextResponse("Foo")
@app_.route("/bar/")
def bar(request):
raise ValueError("bar")
@app_.route("/foo/{bar}/")
def foobar(request):
return PlainTextResponse(f"Foo: {request.path_params['bar']}")
return app_
@pytest.fixture
def client(self, app):
return TestClient(app)
def test_view_ok(self, client):
# Do a request
client.get("/foo/")
# Get metrics
response = client.get("/metrics/")
metrics_text = response.content.decode()
# Asserts: Requests
assert 'starlette_requests_total{method="GET",path_template="/foo/"} 1.0' in metrics_text
# Asserts: Responses
assert 'starlette_responses_total{method="GET",path_template="/foo/",status_code="200"} 1.0' in metrics_text
# Asserts: Requests in progress
assert 'starlette_requests_in_progress{method="GET",path_template="/foo/"} 0.0' in metrics_text
assert 'starlette_requests_in_progress{method="GET",path_template="/metrics/"} 1.0' in metrics_text
def test_view_exception(self, client):
# Do a request
with pytest.raises(ValueError):
client.get("/bar/")
# Get metrics
response = client.get("/metrics/")
metrics_text = response.content.decode()
# Asserts: Requests
assert 'starlette_requests_total{method="GET",path_template="/bar/"} 1.0' in metrics_text
# Asserts: Responses
assert (
"starlette_exceptions_total{"
'exception_type="ValueError",method="GET",path_template="/bar/"'
"} 1.0" in metrics_text
)
assert (
"starlette_responses_total{" 'method="GET",path_template="/bar/",status_code="500"' "} 1.0" in metrics_text
)
# Asserts: Requests in progress
assert 'starlette_requests_in_progress{method="GET",path_template="/bar/"} 0.0' in metrics_text
assert 'starlette_requests_in_progress{method="GET",path_template="/metrics/"} 1.0' in metrics_text
def test_path_substituion(self, client):
# Do a request
client.get("/foo/baz/")
# Get metrics
response = client.get("/metrics/")
metrics_text = response.content.decode()
# Asserts: Headers
assert response.headers["content-type"] == "text/plain; version=0.0.4; charset=utf-8"
# Asserts: Requests
assert 'starlette_requests_total{method="GET",path_template="/foo/{bar}/"} 1.0' in metrics_text
# Asserts: Responses
assert (
'starlette_responses_total{method="GET",path_template="/foo/{bar}/",status_code="200"} 1.0' in metrics_text
)
# Asserts: Requests in progress
assert 'starlette_requests_in_progress{method="GET",path_template="/foo/{bar}/"} 0.0' in metrics_text
assert 'starlette_requests_in_progress{method="GET",path_template="/metrics/"} 1.0' in metrics_text
def test_unhandled_paths(self, client):
# Do a request
client.get("/any/unhandled/path")
# Get metrics
response = client.get("/metrics/")
metrics_text = response.content.decode()
# Asserts: Requests
assert 'starlette_requests_total{method="GET",path_template="/any/unhandled/path"} 1.0' in metrics_text
# Asserts: Responses
assert (
'starlette_responses_total{method="GET",path_template="/any/unhandled/path",status_code="404"} 1.0'
in metrics_text
)
# Asserts: Requests in progress
assert 'starlette_requests_in_progress{method="GET",path_template="/any/unhandled/path"} 0.0' in metrics_text
assert 'starlette_requests_in_progress{method="GET",path_template="/metrics/"} 1.0' in metrics_text
class TestCasePrometheusMiddlewareFilterUnhandledPaths:
@pytest.fixture(scope="class")
def app(self):
app_ = Starlette()
app_.add_middleware(PrometheusMiddleware, filter_unhandled_paths=True)
app_.add_route("/metrics/", metrics)
return app_
@pytest.fixture
def client(self, app):
return TestClient(app)
def test_filter_unhandled_paths(self, client):
# Do a request
path = "/other/unhandled/path"
client.get(path)
# Get metrics
response = client.get("/metrics/")
metrics_text = response.content.decode()
# Asserts: metric is filtered
assert path not in metrics_text
# Asserts: Requests in progress
assert 'starlette_requests_in_progress{method="GET",path_template="/metrics/"} 1.0' in metrics_text