@@ -371,7 +371,10 @@ def _deterministic_sha256_hash(s: str, part: str) -> int:
371371
372372
373373def _parse_high_64_bits (trace_tags : str ) -> str :
374- # todo: testme
374+ """
375+ Parse a list of trace tags such as [_dd.p.tid=66bcb5eb00000000,_dd.p.dm=-0] and return the value of the _dd.p.tid
376+ tag or an empty string if not found.
377+ """
375378 if trace_tags :
376379 for tag in trace_tags .split ("," ):
377380 if "_dd.p.tid=" in tag :
@@ -380,7 +383,6 @@ def _parse_high_64_bits(trace_tags: str) -> str:
380383 return ""
381384
382385
383- # todo: testme
384386def _generate_sfn_parent_id (context : dict ) -> int :
385387 execution_id = context .get ("Execution" ).get ("Id" )
386388 state_name = context .get ("State" ).get ("Name" )
@@ -391,24 +393,31 @@ def _generate_sfn_parent_id(context: dict) -> int:
391393 )
392394
393395
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 ):
396+ def _generate_sfn_trace_id (execution_id : str , part : str ):
397+ """
398+ Take the SHA-256 hash of the execution_id to calculate the trace ID. If the high 64 bits are specified, we take
399+ those bits and use hex to encode it. We also remove the first two characters as they will be '0x in the hex string.
400+
401+ We care about full 128 bits because they will break up into traditional traceID and _dd.p.tid tag.
402+ https://github.com/DataDog/dd-trace-py/blob/3e34d21cb9b5e1916e549047158cb119317b96ab/ddtrace/propagation/http.py#L232-L240
403+ """
401404 if part == HIGHER_64_BITS :
402- return hex (_deterministic_sha256_hash (pre_hash , part ))[2 :]
403- return _deterministic_sha256_hash (pre_hash , part )
405+ return hex (_deterministic_sha256_hash (execution_id , part ))[2 :]
406+ return _deterministic_sha256_hash (execution_id , part )
404407
405408
406409def extract_context_from_step_functions (event , lambda_context ):
407410 """
408411 Only extract datadog trace context when Step Functions Context Object is injected
409412 into lambda's event dict.
413+
414+ If '_datadog' header is present, we have two cases:
415+ 1. Root is a Lambda and we use its traceID
416+ 2. Root is a SFN, and we use its executionARN to calculate the traceID
417+ We calculate the parentID the same in both cases by using the parent SFN's context object.
418+
419+ Otherwise, we're dealing with the legacy case where we only have the parent SFN's context object.
410420 """
411- # todo: update docstring
412421 try :
413422 meta = {}
414423 dd_data = event .get ("_datadog" )
0 commit comments