Skip to content

Commit e2e732b

Browse files
committed
pull sfn trace id generation out into a helper
1 parent 832ae1b commit e2e732b

File tree

2 files changed

+25
-67
lines changed

2 files changed

+25
-67
lines changed

datadog_lambda/tracing.py

Lines changed: 25 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -380,8 +380,8 @@ def _parse_high_64_bits(trace_tags: str) -> str:
380380
return ""
381381

382382

383-
def _sfn_context_to_parent_id(context: dict) -> int:
384-
# todo: testme
383+
# todo: testme
384+
def _generate_sfn_parent_id(context: dict) -> int:
385385
execution_id = context.get("Execution").get("Id")
386386
state_name = context.get("State").get("Name")
387387
state_entered_time = context.get("State").get("EnteredTime")
@@ -391,6 +391,18 @@ def _sfn_context_to_parent_id(context: dict) -> int:
391391
)
392392

393393

394+
# take the higher 64 bits as _dd.p.tid tag and use hex to encode
395+
# [2:] to remove '0x' in the hex str
396+
# todo testme
397+
# returning 128 bits since 128bit traceId will be break up into
398+
# traditional traceId and _dd.p.tid tag
399+
# https://github.com/DataDog/dd-trace-py/blob/3e34d21cb9b5e1916e549047158cb119317b96ab/ddtrace/propagation/http.py#L232-L240
400+
def _generate_sfn_trace_id(pre_hash: str, part: str):
401+
if part == HIGHER_64_BITS:
402+
return hex(_deterministic_sha256_hash(pre_hash, part))[2:]
403+
return _deterministic_sha256_hash(pre_hash, part)
404+
405+
394406
def extract_context_from_step_functions(event, lambda_context):
395407
"""
396408
Only extract datadog trace context when Step Functions Context Object is injected
@@ -410,29 +422,18 @@ def extract_context_from_step_functions(event, lambda_context):
410422
if high_64_bit_trace_id:
411423
meta["_dd.p.tid"] = high_64_bit_trace_id
412424
else: # sfn root
413-
trace_id = _deterministic_sha256_hash(
414-
dd_data.get("RootExecutionId"), LOWER_64_BITS
425+
root_execution_id = dd_data.get("RootExecutionId")
426+
trace_id = _generate_sfn_trace_id(root_execution_id, LOWER_64_BITS)
427+
meta["_dd.p.tid"] = _generate_sfn_trace_id(
428+
root_execution_id, HIGHER_64_BITS
415429
)
416-
meta["_dd.p.tid"] = hex(
417-
_deterministic_sha256_hash(
418-
dd_data.get("RootExecutionId"), HIGHER_64_BITS
419-
)
420-
)[2:]
421430

422-
parent_id = _sfn_context_to_parent_id(dd_data)
431+
parent_id = _generate_sfn_parent_id(dd_data)
423432
else:
424433
execution_id = event.get("Execution").get("Id")
425-
# returning 128 bits since 128bit traceId will be break up into
426-
# traditional traceId and _dd.p.tid tag
427-
# https://github.com/DataDog/dd-trace-py/blob/3e34d21cb9b5e1916e549047158cb119317b96ab/ddtrace/propagation/http.py#L232-L240
428-
trace_id = _deterministic_sha256_hash(execution_id, LOWER_64_BITS)
429-
# take the higher 64 bits as _dd.p.tid tag and use hex to encode
430-
# [2:] to remove '0x' in the hex str
431-
meta["_dd.p.tid"] = hex(
432-
_deterministic_sha256_hash(execution_id, HIGHER_64_BITS)
433-
)[2:]
434-
435-
parent_id = _sfn_context_to_parent_id(event)
434+
trace_id = _generate_sfn_trace_id(execution_id, LOWER_64_BITS)
435+
meta["_dd.p.tid"] = _generate_sfn_trace_id(execution_id, HIGHER_64_BITS)
436+
parent_id = _generate_sfn_parent_id(event)
436437

437438
sampling_priority = SamplingPriority.AUTO_KEEP
438439
return Context(
@@ -454,8 +455,9 @@ def is_legacy_lambda_step_function(event):
454455
return False
455456

456457
event = event.get("Payload")
457-
return "_datadog" in event or (
458-
"Execution" in event and "StateMachine" in event and "State" in event
458+
return isinstance(event, dict) and (
459+
"_datadog" in event
460+
or ("Execution" in event and "StateMachine" in event and "State" in event)
459461
)
460462

461463

tests/test_tracing.py

Lines changed: 0 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -665,10 +665,7 @@ def test_step_function_trace_data_lambda_root(self):
665665
"EnteredTime": "Mon Nov 13 12:43:33 PST 2023",
666666
},
667667
"x-datadog-trace-id": "5821803790426892636",
668-
"x-datadog-sampling-priority": "1",
669668
"x-datadog-tags": "_dd.p.dm=-0,_dd.p.tid=672a7cb100000000",
670-
"traceparent": "00-672a7cb10000000050cb33b3c06ae95c-5fda9d8d1d1373f9-01",
671-
"tracestate": "dd=p:5fda9d8d1d1373f9;s:1;t.dm:-0;t.tid:672a7cb100000000",
672669
"serverless-version": "v2",
673670
}
674671
}
@@ -737,47 +734,6 @@ def test_step_function_trace_data_sfn_root(self):
737734
expected_context,
738735
)
739736

740-
def test_is_legacy_lambda_step_function(self):
741-
sf_event = {
742-
"Payload": {
743-
"Execution": {
744-
"Id": "665c417c-1237-4742-aaca-8b3becbb9e75",
745-
},
746-
"StateMachine": {},
747-
"State": {
748-
"Name": "my-awesome-state",
749-
"EnteredTime": "Mon Nov 13 12:43:33 PST 2023",
750-
},
751-
}
752-
}
753-
self.assertTrue(is_legacy_lambda_step_function(sf_event))
754-
755-
sf_event = {
756-
"Payload": {
757-
"_datadog": {
758-
"x-datadog-trace-id-hash": "fed93f8c162880cb9aa90fcd1f8395383835841d5470d30215f3dd52906ebc58",
759-
"x-datadog-parent-id-hash": "c5eb94cc9220ab5783e1db53debd54b8c93f6f2a3eae1c680d7b849f2d34e551",
760-
"serverless-version": "v2",
761-
}
762-
}
763-
}
764-
self.assertTrue(is_legacy_lambda_step_function(sf_event))
765-
766-
sf_event = {
767-
"Execution": {
768-
"Id": "665c417c-1237-4742-aaca-8b3becbb9e75",
769-
},
770-
"StateMachine": {},
771-
"State": {
772-
"Name": "my-awesome-state",
773-
"EnteredTime": "Mon Nov 13 12:43:33 PST 2023",
774-
},
775-
}
776-
self.assertFalse(is_legacy_lambda_step_function(sf_event))
777-
778-
other_event = ["foo", "bar"]
779-
self.assertFalse(is_legacy_lambda_step_function(other_event))
780-
781737

782738
class TestXRayContextConversion(unittest.TestCase):
783739
def test_convert_xray_trace_id(self):

0 commit comments

Comments
 (0)