@@ -178,16 +178,19 @@ def extract_context_from_http_event_or_context(event, lambda_context):
178
178
return trace_id , parent_id , sampling_priority
179
179
180
180
181
- def extract_context_from_sqs_event_or_context (event , lambda_context ):
181
+ def extract_context_from_sqs_or_sns_event_or_context (event , lambda_context ):
182
182
"""
183
183
Extract Datadog trace context from the first SQS message attributes.
184
184
185
185
Falls back to lambda context if no trace data is found in the SQS message attributes.
186
186
"""
187
187
try :
188
188
first_record = event ["Records" ][0 ]
189
- msg_attributes = first_record .get ("messageAttributes" , {})
190
- dd_json_data = msg_attributes .get ("_datadog" , {}).get ("stringValue" , r"{}" )
189
+ msg_attributes = first_record .get (
190
+ "messageAttributes" , first_record .get ("MessageAttributes" , {})
191
+ )
192
+ dd_payload = msg_attributes .get ("_datadog" , {})
193
+ dd_json_data = dd_payload .get ("stringValue" , dd_payload .get ("Value" , r"{}" ))
191
194
dd_data = json .loads (dd_json_data )
192
195
trace_id = dd_data .get (TraceHeader .TRACE_ID )
193
196
parent_id = dd_data .get (TraceHeader .PARENT_ID )
@@ -242,7 +245,7 @@ def extract_dd_trace_context(event, lambda_context, extractor=None):
242
245
trace_id ,
243
246
parent_id ,
244
247
sampling_priority ,
245
- ) = extract_context_from_sqs_event_or_context (event , lambda_context )
248
+ ) = extract_context_from_sqs_or_sns_event_or_context (event , lambda_context )
246
249
else :
247
250
trace_id , parent_id , sampling_priority = extract_context_from_lambda_context (
248
251
lambda_context
0 commit comments