Skip to content

Commit 7c127a3

Browse files
committed
add logic to extract traceID from _datadog header
1 parent 88f2099 commit 7c127a3

File tree

3 files changed

+68
-16
lines changed

3 files changed

+68
-16
lines changed

datadog_lambda/constants.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,8 @@
33
# This product includes software developed at Datadog (https://www.datadoghq.com/).
44
# Copyright 2019 Datadog, Inc.
55

6-
# Datadog trace sampling priority
7-
86

7+
# Datadog trace sampling priority
98
class SamplingPriority(object):
109
USER_REJECT = -1
1110
AUTO_REJECT = 0
@@ -18,6 +17,7 @@ class TraceHeader(object):
1817
TRACE_ID = "x-datadog-trace-id"
1918
PARENT_ID = "x-datadog-parent-id"
2019
SAMPLING_PRIORITY = "x-datadog-sampling-priority"
20+
TAGS = "x-datadog-tags"
2121

2222

2323
# X-Ray subsegment to save Datadog trace metadata

datadog_lambda/tracing.py

+22-11
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
TraceContextSource,
2525
XrayDaemon,
2626
Headers,
27+
TraceHeader,
2728
)
2829
from datadog_lambda.xray import (
2930
send_segment,
@@ -380,10 +381,26 @@ def extract_context_from_step_functions(event, lambda_context):
380381
execution_id = event.get("Execution").get("Id")
381382
state_name = event.get("State").get("Name")
382383
state_entered_time = event.get("State").get("EnteredTime")
383-
# returning 128 bits since 128bit traceId will be break up into
384-
# traditional traceId and _dd.p.tid tag
385-
# https://github.com/DataDog/dd-trace-py/blob/3e34d21cb9b5e1916e549047158cb119317b96ab/ddtrace/propagation/http.py#L232-L240
386-
trace_id = _deterministic_sha256_hash(execution_id, LOWER_64_BITS)
384+
meta = {}
385+
386+
if "_datadog" in event:
387+
# use the trace ID from the top-most parent when it exists
388+
trace_header = event.get("_datadog")
389+
trace_id = 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
394+
else:
395+
# returning 128 bits since 128bit traceId will be break up into
396+
# traditional traceId and _dd.p.tid tag
397+
# https://github.com/DataDog/dd-trace-py/blob/3e34d21cb9b5e1916e549047158cb119317b96ab/ddtrace/propagation/http.py#L232-L240
398+
trace_id = _deterministic_sha256_hash(execution_id, LOWER_64_BITS)
399+
# take the higher 64 bits as _dd.p.tid tag and use hex to encode
400+
# [2:] to remove '0x' in the hex str
401+
meta["_dd.p.tid"] = hex(
402+
_deterministic_sha256_hash(execution_id, HIGHER_64_BITS)
403+
)[2:]
387404

388405
parent_id = _deterministic_sha256_hash(
389406
f"{execution_id}#{state_name}#{state_entered_time}", HIGHER_64_BITS
@@ -394,13 +411,7 @@ def extract_context_from_step_functions(event, lambda_context):
394411
trace_id=trace_id,
395412
span_id=parent_id,
396413
sampling_priority=sampling_priority,
397-
# take the higher 64 bits as _dd.p.tid tag and use hex to encode
398-
# [2:] to remove '0x' in the hex str
399-
meta={
400-
"_dd.p.tid": hex(
401-
_deterministic_sha256_hash(execution_id, HIGHER_64_BITS)
402-
)[2:]
403-
},
414+
meta=meta,
404415
)
405416
except Exception as e:
406417
logger.debug("The Step Functions trace extractor returned with error %s", e)

tests/test_tracing.py

+44-3
Original file line numberDiff line numberDiff line change
@@ -617,7 +617,7 @@ def test_with_complete_datadog_trace_headers_with_trigger_tags(self):
617617
@with_trace_propagation_style("datadog")
618618
def test_step_function_trace_data(self):
619619
lambda_ctx = get_mock_context()
620-
sqs_event = {
620+
sf_event = {
621621
"Execution": {
622622
"Id": "665c417c-1237-4742-aaca-8b3becbb9e75",
623623
},
@@ -627,7 +627,7 @@ def test_step_function_trace_data(self):
627627
"EnteredTime": "Mon Nov 13 12:43:33 PST 2023",
628628
},
629629
}
630-
ctx, source, event_source = extract_dd_trace_context(sqs_event, lambda_ctx)
630+
ctx, source, event_source = extract_dd_trace_context(sf_event, lambda_ctx)
631631
self.assertEqual(source, "event")
632632
expected_context = Context(
633633
trace_id=3675572987363469717,
@@ -642,7 +642,48 @@ def test_step_function_trace_data(self):
642642
TraceHeader.TRACE_ID: "3675572987363469717",
643643
TraceHeader.PARENT_ID: "10713633173203262661",
644644
TraceHeader.SAMPLING_PRIORITY: "1",
645-
"x-datadog-tags": "_dd.p.tid=e987c84b36b11ab",
645+
TraceHeader.TAGS: "_dd.p.tid=e987c84b36b11ab",
646+
},
647+
)
648+
create_dd_dummy_metadata_subsegment(ctx, XraySubsegment.TRACE_KEY)
649+
self.mock_send_segment.assert_called_with(
650+
XraySubsegment.TRACE_KEY,
651+
expected_context,
652+
)
653+
654+
@with_trace_propagation_style("datadog")
655+
def test_step_function_trace_data(self):
656+
lambda_ctx = get_mock_context()
657+
sf_event = {
658+
"Execution": {
659+
"Id": "665c417c-1237-4742-aaca-8b3becbb9e75",
660+
},
661+
"StateMachine": {},
662+
"State": {
663+
"Name": "my-awesome-state",
664+
"EnteredTime": "Mon Nov 13 12:43:33 PST 2023",
665+
},
666+
"_datadog": {
667+
"x-datadog-trace-id": "4061173386180447114",
668+
"x-datadog-tags": "_dd.p.tid=aac3639aa724716",
669+
},
670+
}
671+
ctx, source, event_source = extract_dd_trace_context(sf_event, lambda_ctx)
672+
self.assertEqual(source, "event")
673+
expected_context = Context(
674+
trace_id=4061173386180447114,
675+
span_id=6880978411788117524,
676+
sampling_priority=1,
677+
meta={"_dd.p.tid": "aac3639aa724716"},
678+
)
679+
self.assertEqual(ctx, expected_context)
680+
self.assertEqual(
681+
get_dd_trace_context(),
682+
{
683+
TraceHeader.TRACE_ID: "4061173386180447114",
684+
TraceHeader.PARENT_ID: "10713633173203262661",
685+
TraceHeader.SAMPLING_PRIORITY: "1",
686+
TraceHeader.TAGS: "_dd.p.tid=aac3639aa724716",
646687
},
647688
)
648689
create_dd_dummy_metadata_subsegment(ctx, XraySubsegment.TRACE_KEY)

0 commit comments

Comments
 (0)