44
55import importlib
66from collections .abc import Callable , Iterator
7+ from functools import partial
78from typing import Any
89
910import pip
1011import pytest
1112from aiohttp import web
1213from aiohttp .test_utils import TestClient
14+ from opentelemetry import trace
1315from opentelemetry .sdk .trace .export .in_memory_span_exporter import InMemorySpanExporter
1416from pydantic import ValidationError
1517from 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