Skip to content

Commit d97d9bb

Browse files
authored
Step Functions Legacy Lambda Span Linking (#511)
This change will check if events fall into the Legacy Lambda case and parse them accordingly so that we can extract the trace context and infer a span link between the Step Function and the downstream Legacy Lambda
1 parent 692b9c9 commit d97d9bb

File tree

3 files changed

+39
-0
lines changed

3 files changed

+39
-0
lines changed

Diff for: datadog_lambda/tracing.py

+8
Original file line numberDiff line numberDiff line change
@@ -407,6 +407,14 @@ def extract_context_from_step_functions(event, lambda_context):
407407
return extract_context_from_lambda_context(lambda_context)
408408

409409

410+
def is_legacy_lambda_step_function(event):
411+
"""
412+
Check if the event is a step function that called a legacy lambda
413+
"""
414+
event = event.get("Payload", {})
415+
return "Execution" in event and "StateMachine" in event and "State" in event
416+
417+
410418
def extract_context_custom_extractor(extractor, event, lambda_context):
411419
"""
412420
Extract Datadog trace context using a custom trace extractor function

Diff for: datadog_lambda/wrapper.py

+3
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@
4444
is_authorizer_response,
4545
tracer,
4646
propagator,
47+
is_legacy_lambda_step_function,
4748
)
4849
from datadog_lambda.trigger import (
4950
extract_trigger_tags,
@@ -277,6 +278,8 @@ def _before(self, event, context):
277278
self.response = None
278279
set_cold_start(init_timestamp_ns)
279280
submit_invocations_metric(context)
281+
if is_legacy_lambda_step_function(event):
282+
event = event["Payload"]
280283
self.trigger_tags = extract_trigger_tags(event, context)
281284
# Extract Datadog trace context and source from incoming requests
282285
dd_context, trace_context_source, event_source = extract_dd_trace_context(

Diff for: tests/test_tracing.py

+28
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
service_mapping as global_service_mapping,
4141
propagator,
4242
emit_telemetry_on_exception_outside_of_handler,
43+
is_legacy_lambda_step_function,
4344
)
4445
from datadog_lambda.trigger import EventTypes
4546

@@ -647,6 +648,33 @@ def test_step_function_trace_data(self):
647648
expected_context,
648649
)
649650

651+
def test_is_legacy_lambda_step_function(self):
652+
sf_event = {
653+
"Payload": {
654+
"Execution": {
655+
"Id": "665c417c-1237-4742-aaca-8b3becbb9e75",
656+
},
657+
"StateMachine": {},
658+
"State": {
659+
"Name": "my-awesome-state",
660+
"EnteredTime": "Mon Nov 13 12:43:33 PST 2023",
661+
},
662+
}
663+
}
664+
self.assertTrue(is_legacy_lambda_step_function(sf_event))
665+
666+
sf_event = {
667+
"Execution": {
668+
"Id": "665c417c-1237-4742-aaca-8b3becbb9e75",
669+
},
670+
"StateMachine": {},
671+
"State": {
672+
"Name": "my-awesome-state",
673+
"EnteredTime": "Mon Nov 13 12:43:33 PST 2023",
674+
},
675+
}
676+
self.assertFalse(is_legacy_lambda_step_function(sf_event))
677+
650678

651679
class TestXRayContextConversion(unittest.TestCase):
652680
def test_convert_xray_trace_id(self):

0 commit comments

Comments
 (0)