@@ -258,17 +258,7 @@ def extract_context_from_sqs_or_sns_event_or_context(
258258 try :
259259 dd_ctx = _extract_context_from_sqs_or_sns_record (record )
260260 if apm_context is None :
261- if dd_ctx and is_step_function_event (dd_ctx ):
262- try :
263- return extract_context_from_step_functions (dd_ctx , None )
264- except Exception :
265- logger .debug (
266- "Failed to extract Step Functions context from SQS/SNS event."
267- )
268- elif not dd_ctx :
269- apm_context = _extract_context_from_xray (record )
270- else :
271- apm_context = propagator .extract (dd_ctx )
261+ apm_context = _extract_apm_context (dd_ctx , record )
272262 except Exception as e :
273263 logger .debug ("The trace extractor returned with error %s" , e )
274264 if config .data_streams_enabled :
@@ -325,26 +315,39 @@ def _extract_context_from_sqs_or_sns_record(record):
325315 return None
326316
327317
328- def _extract_context_from_xray (record ):
329- attrs = record .get ("attributes" )
330- if attrs :
331- x_ray_header = attrs .get ("AWSTraceHeader" )
332- if x_ray_header :
333- x_ray_context = parse_xray_header (x_ray_header )
334- trace_id_parts = x_ray_context .get ("trace_id" , "" ).split ("-" )
335- if len (trace_id_parts ) > 2 and trace_id_parts [2 ].startswith (
336- DD_TRACE_JAVA_TRACE_ID_PADDING
337- ):
338- # If it starts with eight 0's padding,
339- # then this AWSTraceHeader contains Datadog injected trace context
318+ def _extract_apm_context (dd_ctx , record ):
319+ if dd_ctx :
320+ if is_step_function_event (dd_ctx ):
321+ try :
322+ return extract_context_from_step_functions (dd_ctx , None )
323+ except Exception :
340324 logger .debug (
341- "Found dd-trace injected trace context from AWSTraceHeader"
342- )
343- return Context (
344- trace_id = int (trace_id_parts [2 ][8 :], 16 ),
345- span_id = int (x_ray_context ["parent_id" ], 16 ),
346- sampling_priority = float (x_ray_context ["sampled" ]),
325+ "Failed to extract Step Functions context from SQS/SNS event."
347326 )
327+ else :
328+ return propagator .extract (dd_ctx )
329+ else :
330+ # Handle case where trace context is injected into attributes.AWSTraceHeader
331+ # example: Root=1-654321ab-000000001234567890abcdef;Parent=0123456789abcdef;Sampled=1
332+ attrs = record .get ("attributes" )
333+ if attrs :
334+ x_ray_header = attrs .get ("AWSTraceHeader" )
335+ if x_ray_header :
336+ x_ray_context = parse_xray_header (x_ray_header )
337+ trace_id_parts = x_ray_context .get ("trace_id" , "" ).split ("-" )
338+ if len (trace_id_parts ) > 2 and trace_id_parts [2 ].startswith (
339+ DD_TRACE_JAVA_TRACE_ID_PADDING
340+ ):
341+ # If it starts with eight 0's padding,
342+ # then this AWSTraceHeader contains Datadog injected trace context
343+ logger .debug (
344+ "Found dd-trace injected trace context from AWSTraceHeader"
345+ )
346+ return Context (
347+ trace_id = int (trace_id_parts [2 ][8 :], 16 ),
348+ span_id = int (x_ray_context ["parent_id" ], 16 ),
349+ sampling_priority = float (x_ray_context ["sampled" ]),
350+ )
348351 return None
349352
350353
0 commit comments