@@ -367,6 +367,77 @@ async def test_trace_from_headers_if_performance_disabled(
367367 assert error_event ["contexts" ]["trace" ]["trace_id" ] == trace_id
368368
369369
370+ @pytest .mark .asyncio
371+ async def test_trace_from_w3c_headers_if_performance_enabled (
372+ sentry_init ,
373+ asgi3_app_with_error_and_msg ,
374+ capture_events ,
375+ ):
376+ sentry_init (traces_sample_rate = 1.0 )
377+ app = SentryAsgiMiddleware (asgi3_app_with_error_and_msg )
378+
379+ trace_id = "582b43a4192642f0b136d5159a501701"
380+ w3c_trace_header = "00-{}-{}-{}" .format (trace_id , "6e8f22c393e68f19" , "01" )
381+
382+ with pytest .raises (ZeroDivisionError ):
383+ async with TestClient (app ) as client :
384+ events = capture_events ()
385+ await client .get (
386+ "/" ,
387+ headers = {
388+ "traceparent" : w3c_trace_header ,
389+ },
390+ )
391+
392+ msg_event , error_event , transaction_event = events
393+
394+ assert msg_event ["contexts" ]["trace" ]
395+ assert "trace_id" in msg_event ["contexts" ]["trace" ]
396+
397+ assert error_event ["contexts" ]["trace" ]
398+ assert "trace_id" in error_event ["contexts" ]["trace" ]
399+
400+ assert transaction_event ["contexts" ]["trace" ]
401+ assert "trace_id" in transaction_event ["contexts" ]["trace" ]
402+
403+ assert msg_event ["contexts" ]["trace" ]["trace_id" ] == trace_id
404+ assert error_event ["contexts" ]["trace" ]["trace_id" ] == trace_id
405+ assert transaction_event ["contexts" ]["trace" ]["trace_id" ] == trace_id
406+
407+
408+ @pytest .mark .asyncio
409+ async def test_trace_from_w3c_headers_if_performance_disabled (
410+ sentry_init ,
411+ asgi3_app_with_error_and_msg ,
412+ capture_events ,
413+ ):
414+ sentry_init ()
415+ app = SentryAsgiMiddleware (asgi3_app_with_error_and_msg )
416+
417+ trace_id = "582b43a4192642f0b136d5159a501701"
418+ w3c_trace_header = "00-{}-{}-{}" .format (trace_id , "6e8f22c393e68f19" , "01" )
419+
420+ with pytest .raises (ZeroDivisionError ):
421+ async with TestClient (app ) as client :
422+ events = capture_events ()
423+ await client .get (
424+ "/" ,
425+ headers = {
426+ "traceparent" : w3c_trace_header ,
427+ },
428+ )
429+
430+ msg_event , error_event = events
431+
432+ assert msg_event ["contexts" ]["trace" ]
433+ assert "trace_id" in msg_event ["contexts" ]["trace" ]
434+ assert msg_event ["contexts" ]["trace" ]["trace_id" ] == trace_id
435+
436+ assert error_event ["contexts" ]["trace" ]
437+ assert "trace_id" in error_event ["contexts" ]["trace" ]
438+ assert error_event ["contexts" ]["trace" ]["trace_id" ] == trace_id
439+
440+
370441@pytest .mark .asyncio
371442async def test_websocket (sentry_init , asgi3_ws_app , capture_events , request ):
372443 sentry_init (send_default_pii = True )
0 commit comments