Skip to content

Commit ff292a9

Browse files
committed
feat: Checked handled path
1 parent c72b11a commit ff292a9

File tree

2 files changed

+38
-0
lines changed

2 files changed

+38
-0
lines changed

src/asgi_monitor/integrations/aiohttp.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
from aiohttp.web import Application, Request, Response, middleware
77
from aiohttp.web_exceptions import HTTPException, HTTPInternalServerError
8+
from aiohttp.web_urldispatcher import MatchInfoError
89
from opentelemetry import trace
910
from opentelemetry.instrumentation.utils import http_status_to_status_code
1011
from opentelemetry.metrics import Meter, MeterProvider, get_meter_provider
@@ -113,6 +114,9 @@ def build_metrics_middleware(
113114
) -> Callable[..., Coroutine]:
114115
@middleware
115116
async def metrics_middleware(request: Request, handler: Callable) -> Any:
117+
if isinstance(await request.app.router.resolve(request), MatchInfoError):
118+
return await handler(request)
119+
116120
status_code = HTTPInternalServerError.status_code
117121

118122
method = request.method

tests/integration/aiohttp/test_middlewares.py

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,40 @@ async def test_metrics(aiohttp_client: AiohttpClient) -> None:
6666
)
6767

6868

69+
async def test_not_handled(aiohttp_client: AiohttpClient) -> None:
70+
# Arrange
71+
expected_content_type = "text/plain; version=0.0.4; charset=utf-8"
72+
73+
app = Application()
74+
75+
metrics_cfg = MetricsConfig(
76+
app_name="test",
77+
include_metrics_endpoint=True,
78+
include_trace_exemplar=False,
79+
openmetrics_format=False,
80+
)
81+
82+
setup_metrics(app, metrics_cfg)
83+
84+
client: TestClient = await aiohttp_client(app)
85+
86+
# Act
87+
not_found_response = await client.get("/not_found")
88+
response = await client.get("/metrics")
89+
90+
# Assert
91+
assert response.headers["content-type"] == expected_content_type
92+
assert not_found_response.status == 404
93+
assert response.status == 200
94+
assert_that(await response.text()).does_not_contain("not_found")
95+
assert_that(await response.text()).contains(
96+
'aiohttp_app_info{app_name="test"} 1.0',
97+
'aiohttp_requests_total{app_name="test",method="GET",path="/metrics"} 1.0',
98+
'aiohttp_requests_created{app_name="test",method="GET",path="/metrics"}',
99+
'aiohttp_requests_in_progress{app_name="test",method="GET",path="/metrics"} 1.0',
100+
)
101+
102+
69103
async def test_metrics_global_registry(aiohttp_client: AiohttpClient) -> None:
70104
# Arrange
71105
app = Application()

0 commit comments

Comments
 (0)