11import asyncio
22import json
3- from typing import Callable
3+ from typing import Callable , cast
44
55from aiohttp .test_utils import TestClient # noqa: TCH002
66from aiohttp .web import Application , Request , Response , json_response
@@ -115,7 +115,7 @@ async def test_metrics_with_tracing(aiohttp_client: Callable) -> None:
115115 app = Application ()
116116 app .router .add_get ("/" , index_handler )
117117
118- trace_cfg , _ = build_aiohttp_tracing_config ()
118+ trace_cfg , exporter = build_aiohttp_tracing_config ()
119119 metrics_cfg = MetricsConfig (
120120 app_name = "test" ,
121121 include_metrics_endpoint = False ,
@@ -131,6 +131,7 @@ async def test_metrics_with_tracing(aiohttp_client: Callable) -> None:
131131 # Assert
132132 assert response .status == 200
133133 metrics = get_latest_metrics (metrics_cfg .registry , openmetrics_format = True )
134+ span = exporter .get_finished_spans ()[0 ]
134135 pattern = (
135136 r"aiohttp_request_duration_seconds_bucket\{"
136137 r'app_name="test",le="([\d.]+)",method="GET",path="\/"}\ 1.0 # \{TraceID="(\w+)"\} (\d+\.\d+) (\d+\.\d+)'
@@ -249,7 +250,6 @@ async def test_json_response_handle(aiohttp_client: Callable) -> None:
249250async def test_error_handle_with_tracing (aiohttp_client : Callable ) -> None :
250251 # Arrange
251252 app = Application ()
252-
253253 app .router .add_get ("/error" , zero_division_handler )
254254
255255 trace_cfg , exporter = build_aiohttp_tracing_config ()
@@ -278,7 +278,6 @@ async def test_error_handle_with_tracing(aiohttp_client: Callable) -> None:
278278async def test_raise_handler_with_tracing (aiohttp_client : Callable ) -> None :
279279 # Arrange
280280 app = Application ()
281-
282281 app .router .add_get ("/raise_handler" , raise_handler )
283282
284283 trace_cfg , exporter = build_aiohttp_tracing_config ()
@@ -300,3 +299,26 @@ async def test_raise_handler_with_tracing(aiohttp_client: Callable) -> None:
300299 },
301300 ignore = ["exception.stacktrace" ],
302301 )
302+
303+
304+ async def test_tracing (aiohttp_client : Callable ) -> None :
305+ # Arrange
306+ app = Application ()
307+ app .router .add_get ("/" , index_handler )
308+
309+ trace_cfg , exporter = build_aiohttp_tracing_config ()
310+
311+ setup_tracing (app , trace_cfg )
312+
313+ client : TestClient = await aiohttp_client (app )
314+ # Act
315+ response = await client .get ("/" )
316+ span = cast ("tuple[Span, Span, Span]" , exporter .get_finished_spans ())
317+ assert_that (span [0 ].attributes ).is_equal_to ({
318+ 'http.host' : '0.0.0.0' ,
319+ 'net.host.port' : 80 ,
320+ 'http.url' : 'http://0.0.0.0' ,
321+ 'http.status_code' : 200
322+ }
323+ )
324+ assert response .status == 200
0 commit comments