Skip to content

Commit 4e947ef

Browse files
committed
Add test for get_path
1 parent 377bc64 commit 4e947ef

File tree

3 files changed

+34
-1
lines changed

3 files changed

+34
-1
lines changed

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ lint.ignore = [
9494
]
9595

9696
[tool.ruff.lint.per-file-ignores]
97-
"tests/**" = ["TID252", "PLR2004", "S101", "T201", "SLF001"]
97+
"tests/**" = ["TID252", "PLR2004", "S101", "T201", "SLF001", "PERF203"]
9898
"examples/**" = ["T201", "B018", "INP001"]
9999

100100
[tool.ruff.lint.mccabe]

tests/integration/conftest.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import pytest
2+
from prometheus_client import REGISTRY
3+
4+
5+
@pytest.fixture(autouse=True)
6+
def _reset_registry() -> None:
7+
collectors = tuple(REGISTRY._collector_to_names.keys())
8+
for collector in collectors:
9+
try:
10+
collector._metrics.clear()
11+
collector._metric_init()
12+
except AttributeError:
13+
pass

tests/integration/fastapi/test_middlewares.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,3 +56,23 @@ async def test_metrics_with_tracing() -> None:
5656
r'app_name="test",le="([\d.]+)",method="GET",path="\/"}\ 1.0 # \{TraceID="(\w+)"\} (\d+\.\d+) (\d+\.\d+)'
5757
)
5858
assert_that(metrics.payload.decode()).matches(pattern)
59+
assert_that(metrics.payload.decode()).contains('fastapi_app_info{app_name="test"} 1.0')
60+
61+
62+
async def test_metrics_get_path() -> None:
63+
# Arrange
64+
app = FastAPI()
65+
setup_metrics(app=app, app_name="test", include_trace_exemplar=False, include_metrics_endpoint=True)
66+
67+
# Act
68+
async with fastapi_app(app) as client:
69+
response = client.get("/metrics/")
70+
71+
# Assert
72+
assert response.status_code == 200
73+
assert_that(response.content.decode()).contains(
74+
'fastapi_app_info{app_name="test"} 1.0',
75+
'fastapi_requests_total{app_name="test",method="GET",path="/metrics"} 1.0',
76+
'fastapi_requests_created{app_name="test",method="GET",path="/metrics"}',
77+
'fastapi_requests_in_progress{app_name="test",method="GET",path="/metrics"} 1.0',
78+
)

0 commit comments

Comments
 (0)