Skip to content

Commit a0bc75c

Browse files
committed
add check that propagated trace id is correct in aiohttp
1 parent e800462 commit a0bc75c

File tree

1 file changed

+12
-2
lines changed

1 file changed

+12
-2
lines changed

packages/service-library/tests/aiohttp/test_tracing.py

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,14 @@
44

55
import importlib
66
from collections.abc import Callable, Iterator
7+
from functools import partial
78
from typing import Any
89

910
import pip
1011
import pytest
1112
from aiohttp import web
1213
from aiohttp.test_utils import TestClient
14+
from opentelemetry import trace
1315
from opentelemetry.sdk.trace.export.in_memory_span_exporter import InMemorySpanExporter
1416
from pydantic import ValidationError
1517
from servicelib.aiohttp.tracing import get_tracing_lifespan
@@ -174,12 +176,17 @@ async def test_trace_id_in_response_header(
174176
service_name = "simcore_service_webserver"
175177
tracing_settings = TracingSettings()
176178

177-
async def handler(request: web.Request) -> web.Response:
179+
async def handler(handler_data: dict, request: web.Request) -> web.Response:
180+
current_span = trace.get_current_span()
181+
handler_data[_OSPARC_TRACE_ID_HEADER] = format(
182+
current_span.get_span_context().trace_id, "032x"
183+
)
178184
if isinstance(server_response, web.HTTPException):
179185
raise server_response
180186
return server_response
181187

182-
app.router.add_get("/", handler)
188+
handler_data = dict()
189+
app.router.add_get("/", partial(handler, handler_data))
183190

184191
async for _ in get_tracing_lifespan(
185192
app=app,
@@ -192,3 +199,6 @@ async def handler(request: web.Request) -> web.Response:
192199
assert _OSPARC_TRACE_ID_HEADER in response.headers
193200
trace_id = response.headers[_OSPARC_TRACE_ID_HEADER]
194201
assert len(trace_id) == 32 # Ensure trace ID is a 32-character hex string
202+
assert (
203+
trace_id == handler_data[_OSPARC_TRACE_ID_HEADER]
204+
) # Ensure trace IDs match

0 commit comments

Comments
 (0)