Skip to content

Commit 3bfc1e7

Browse files
committed
added unit tests
1 parent e2e732b commit 3bfc1e7

File tree

4 files changed

+76
-13
lines changed

4 files changed

+76
-13
lines changed

datadog_lambda/tracing.py

Lines changed: 21 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -371,7 +371,10 @@ def _deterministic_sha256_hash(s: str, part: str) -> int:
371371

372372

373373
def _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
384386
def _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

406409
def 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")

datadog_lambda/trigger.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -256,7 +256,6 @@ def parse_event_source_arn(source: _EventSource, event: dict, context: Any) -> s
256256
if source.event_type == EventTypes.CLOUDWATCH_EVENTS and event.get("resources"):
257257
return event.get("resources")[0]
258258

259-
# todo: testme
260259
# e.g. arn:aws:states:us-east-1:123456789012:stateMachine:stateMachineName
261260
if source.event_type == EventTypes.STEPFUNCTIONS:
262261
context = event
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
{
2+
"Records": [
3+
{
4+
"_datadog": {
5+
"Execution": {
6+
"Id": "arn:aws:states:ca-central-1:425362996713:execution:MyStateMachine-wsx8chv4d:1356a963-42a5-48b0-ba3f-73bde559a50c",
7+
"StartTime": "2024-11-13T16:46:47.715Z",
8+
"Name": "1356a963-42a5-48b0-ba3f-73bde559a50c",
9+
"RoleArn": "arn:aws:iam::425362996713:role/service-role/StepFunctions-MyStateMachine-wsx8chv4d-role-1su0fkfd3",
10+
"RedriveCount": 0
11+
},
12+
"StateMachine": {
13+
"Id": "arn:aws:states:ca-central-1:425362996713:stateMachine:MyStateMachine-wsx8chv4d",
14+
"Name": "MyStateMachine-wsx8chv4d"
15+
},
16+
"State": {
17+
"Name": "Lambda Invoke",
18+
"EnteredTime": "2024-11-13T16:46:47.740Z",
19+
"RetryCount": 0
20+
},
21+
"RootExecutionId": "arn:aws:states:ca-central-1:425362996713:execution:MyStateMachine-wsx8chv4d:1356a963-42a5-48b0-ba3f-73bde559a50c",
22+
"serverless-version": "v2"
23+
}
24+
}
25+
]
26+
}

tests/test_trigger.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -230,6 +230,20 @@ def test_event_source_sqs(self):
230230
"arn:aws:sqs:eu-west-1:601427279990:InferredSpansQueueNode",
231231
)
232232

233+
def test_event_source_stepfunctions(self):
234+
event_sample_source = "stepfunctions"
235+
test_file = event_samples + event_sample_source + ".json"
236+
with open(test_file, "r") as event:
237+
event = json.load(event)
238+
ctx = get_mock_context()
239+
event_source = parse_event_source(event)
240+
event_source_arn = get_event_source_arn(event_source, event, ctx)
241+
self.assertEqual(event_source.to_string(), event_sample_source)
242+
self.assertEqual(
243+
event_source_arn,
244+
"arn:aws:states:ca-central-1:425362996713:stateMachine:MyStateMachine-wsx8chv4d",
245+
)
246+
233247
def test_event_source_unsupported(self):
234248
event_sample_source = "custom"
235249
test_file = event_samples + event_sample_source + ".json"
@@ -485,6 +499,21 @@ def test_extract_trigger_tags_sqs(self):
485499
},
486500
)
487501

502+
def test_extract_trigger_tags_stepfunctions(self):
503+
event_sample_source = "stepfunctions"
504+
test_file = event_samples + event_sample_source + ".json"
505+
ctx = get_mock_context()
506+
with open(test_file, "r") as event:
507+
event = json.load(event)
508+
tags = extract_trigger_tags(event, ctx)
509+
self.assertEqual(
510+
tags,
511+
{
512+
"function_trigger.event_source": "stepfunctions",
513+
"function_trigger.event_source_arn": "arn:aws:states:ca-central-1:425362996713:stateMachine:MyStateMachine-wsx8chv4d",
514+
},
515+
)
516+
488517
def test_extract_trigger_tags_unsupported(self):
489518
event_sample_source = "custom"
490519
test_file = event_samples + event_sample_source + ".json"

0 commit comments

Comments
 (0)