Skip to content

Commit cad67ed

Browse files
committed
fix: add test_tracing
1 parent 33209d4 commit cad67ed

File tree

2 files changed

+27
-5
lines changed

2 files changed

+27
-5
lines changed

.pre-commit-config.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
default_language_version:
2-
python: python3.10
2+
python: python3.12
33

44
repos:
55
- repo: https://github.com/compilerla/conventional-pre-commit

tests/integration/aiohttp/test_middlewares.py

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import asyncio
22
import json
3-
from typing import Callable
3+
from typing import Callable, cast
44

55
from aiohttp.test_utils import TestClient # noqa: TCH002
66
from 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:
249250
async 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:
278278
async 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

Comments
 (0)