@@ -178,6 +178,18 @@ 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 create_sns_event (message ):
182
+ return {
183
+ "Records" : [
184
+ {
185
+ "EventSource" : "aws:sns" ,
186
+ "EventVersion" : "1.0" ,
187
+ "Sns" : message ,
188
+ }
189
+ ]
190
+ }
191
+
192
+
181
193
def extract_context_from_sqs_or_sns_event_or_context (event , lambda_context ):
182
194
"""
183
195
Extract Datadog trace context from the first SQS message attributes.
@@ -186,6 +198,19 @@ def extract_context_from_sqs_or_sns_event_or_context(event, lambda_context):
186
198
"""
187
199
try :
188
200
first_record = event ["Records" ][0 ]
201
+
202
+ # logic to deal with SNS => SQS event
203
+ if "body" in first_record :
204
+ body_str = first_record .get ("body" , {})
205
+ try :
206
+ body = json .loads (body_str )
207
+ if body .get ("Type" , "" ) == "Notification" and "TopicArn" in body :
208
+ logger .debug ("Found SNS message inside SQS event" )
209
+ first_record = get_first_record (create_sns_event (body ))
210
+ except Exception :
211
+ first_record = event ["Records" ][0 ]
212
+ pass
213
+
189
214
msg_attributes = first_record .get (
190
215
"messageAttributes" ,
191
216
first_record .get ("Sns" , {}).get ("MessageAttributes" , {}),
@@ -533,6 +558,8 @@ def create_inferred_span_from_http_api_event(event, context):
533
558
534
559
535
560
def create_inferred_span_from_sqs_event (event , context ):
561
+ trace_ctx = tracer .current_trace_context ()
562
+
536
563
event_record = get_first_record (event )
537
564
queue_name = event_record ["eventSourceARN" ].split (":" )[- 1 ]
538
565
tags = {
@@ -546,11 +573,35 @@ def create_inferred_span_from_sqs_event(event, context):
546
573
"resource" : queue_name ,
547
574
"span_type" : "web" ,
548
575
}
576
+ start_time = int (request_time_epoch ) / 1000
577
+
578
+ # logic to deal with SNS => SQS event
579
+ sns_span = None
580
+ if "body" in event_record :
581
+ body_str = event_record .get ("body" , {})
582
+ try :
583
+ body = json .loads (body_str )
584
+ if body .get ("Type" , "" ) == "Notification" and "TopicArn" in body :
585
+ logger .debug ("Found SNS message inside SQS event" )
586
+ sns_span = create_inferred_span_from_sns_event (
587
+ create_sns_event (body ), context
588
+ )
589
+ sns_span .finish (finish_time = start_time )
590
+ except Exception :
591
+ logger .debug ("Unable to create SNS span from SQS message" )
592
+ pass
593
+
594
+ # trace context needs to be set again as it is reset
595
+ # when sns_span.finish executes
596
+ tracer .context_provider .activate (trace_ctx )
549
597
tracer .set_tags ({"_dd.origin" : "lambda" })
550
598
span = tracer .trace ("aws.sqs" , ** args )
551
599
if span :
552
600
span .set_tags (tags )
553
- span .start = int (request_time_epoch ) / 1000
601
+ span .start = start_time
602
+ if sns_span :
603
+ span .parent_id = sns_span .span_id
604
+
554
605
return span
555
606
556
607
0 commit comments