Skip to content

Commit 97b189a

Browse files
committed
added root arn case
1 parent 3eccb20 commit 97b189a

File tree

2 files changed

+54
-7
lines changed

2 files changed

+54
-7
lines changed

datadog_lambda/tracing.py

+13-6
Original file line numberDiff line numberDiff line change
@@ -384,13 +384,20 @@ def extract_context_from_step_functions(event, lambda_context):
384384
meta = {}
385385

386386
if "_datadog" in event:
387-
# use the trace ID from the top-most parent when it exists
388387
trace_header = event.get("_datadog")
389-
trace_id = int(trace_header.get(TraceHeader.TRACE_ID))
390-
tags = trace_header.get(TraceHeader.TAGS)
391-
for tag in tags.split(","):
392-
tag_key, tag_val = tag.split("=")
393-
meta[tag_key] = tag_val
388+
if TraceHeader.TRACE_ID in trace_header:
389+
# use the trace ID from the top-most parent when it exists
390+
trace_id = int(trace_header.get(TraceHeader.TRACE_ID))
391+
tags = trace_header.get(TraceHeader.TAGS, "")
392+
for tag in tags.split(","):
393+
tag_key, tag_val = tag.split("=")
394+
meta[tag_key] = tag_val
395+
elif "x-datadog-execution-arn" in trace_header:
396+
root_execution_id = trace_header.get("x-datadog-execution-arn")
397+
trace_id = _deterministic_sha256_hash(root_execution_id, LOWER_64_BITS)
398+
meta["_dd.p.tid"] = hex(
399+
_deterministic_sha256_hash(root_execution_id, HIGHER_64_BITS)
400+
)[2:]
394401
else:
395402
# returning 128 bits since 128bit traceId will be break up into
396403
# traditional traceId and _dd.p.tid tag

tests/test_tracing.py

+41-1
Original file line numberDiff line numberDiff line change
@@ -652,7 +652,7 @@ def test_step_function_trace_data(self):
652652
)
653653

654654
@with_trace_propagation_style("datadog")
655-
def test_step_function_trace_data_with_header(self):
655+
def test_step_function_trace_data_with_trace_header(self):
656656
lambda_ctx = get_mock_context()
657657
sf_event = {
658658
"Execution": {
@@ -692,6 +692,46 @@ def test_step_function_trace_data_with_header(self):
692692
expected_context,
693693
)
694694

695+
@with_trace_propagation_style("datadog")
696+
def test_step_function_trace_data_with_arn_header(self):
697+
lambda_ctx = get_mock_context()
698+
sf_event = {
699+
"Execution": {
700+
"Id": "665c417c-1237-4742-aaca-8b3becbb9e75",
701+
},
702+
"StateMachine": {},
703+
"State": {
704+
"Name": "my-awesome-state",
705+
"EnteredTime": "Mon Nov 13 12:43:33 PST 2023",
706+
},
707+
"_datadog": {
708+
"x-datadog-execution-arn": "ca7383bc-e370-4a85-a266-a4686bd7d00f"
709+
},
710+
}
711+
ctx, source, event_source = extract_dd_trace_context(sf_event, lambda_ctx)
712+
self.assertEqual(source, "event")
713+
expected_context = Context(
714+
trace_id=6970872619724504833,
715+
span_id=6880978411788117524,
716+
sampling_priority=1,
717+
meta={"_dd.p.tid": "71dab8f4d4629263"},
718+
)
719+
self.assertEqual(ctx, expected_context)
720+
self.assertEqual(
721+
get_dd_trace_context(),
722+
{
723+
TraceHeader.TRACE_ID: "6970872619724504833",
724+
TraceHeader.PARENT_ID: "10713633173203262661",
725+
TraceHeader.SAMPLING_PRIORITY: "1",
726+
TraceHeader.TAGS: "_dd.p.tid=71dab8f4d4629263",
727+
},
728+
)
729+
create_dd_dummy_metadata_subsegment(ctx, XraySubsegment.TRACE_KEY)
730+
self.mock_send_segment.assert_called_with(
731+
XraySubsegment.TRACE_KEY,
732+
expected_context,
733+
)
734+
695735
def test_is_legacy_lambda_step_function(self):
696736
sf_event = {
697737
"Payload": {

0 commit comments

Comments
 (0)