Skip to content

Commit 55dc3f4

Browse files
lym953claude
andauthored
feat: [SVLS-8720] Add span tag aws_lambda.durable_function.first_invocation (#747)
* Add durable_function_first_invocation tag to aws.lambda spans Extends extract_durable_function_tags() to accept context.state and sets durable_function_first_invocation ("true"/"false") using not state.is_replaying(). wrapper._before() passes context.state to the function. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com> * Add durable_function_first_invocation tag to aws.lambda spans Extends extract_durable_function_tags() to set durable_function_first_invocation ("true"/"false") by checking len(InitialExecutionState.Operations) <= 1, mirroring the SDK's own replay detection logic (ReplayStatus.REPLAY when len > 1). Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com> * Add comment * Change <= 1 to == 1 * Update comments * fmt * feat: [SVLS-8493] use aws_lambda prefix for durable function tags Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com> --------- Co-authored-by: Claude Sonnet 4.6 <noreply@anthropic.com>
1 parent a3f0640 commit 55dc3f4

File tree

2 files changed

+33
-3
lines changed

2 files changed

+33
-3
lines changed

datadog_lambda/durable.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,11 +41,17 @@ def extract_durable_function_tags(event):
4141
if not parsed:
4242
logger.error("Failed to parse DurableExecutionArn: %s", durable_execution_arn)
4343
return {}
44-
4544
execution_name, execution_id = parsed
45+
# Use the number of operations to determine if it's the first invocation. This is
46+
# what the durable execution SDK does to determine the replay status.
47+
operations = event.get("InitialExecutionState", {}).get("Operations", [])
48+
is_first_invocation = len(operations) == 1
4649
return {
4750
"aws_lambda.durable_function.execution_name": execution_name,
4851
"aws_lambda.durable_function.execution_id": execution_id,
52+
"aws_lambda.durable_function.first_invocation": str(
53+
is_first_invocation
54+
).lower(),
4955
}
5056

5157

tests/test_durable.py

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,18 +46,42 @@ def test_works_with_numeric_version_qualifier(self):
4646

4747

4848
class TestExtractDurableFunctionTags(unittest.TestCase):
49-
def test_extracts_tags_from_event_with_durable_execution_arn(self):
49+
def test_sets_first_invocation_true_when_only_execution_operation(self):
50+
# One operation (the current EXECUTION operation itself) → not replaying → first invocation
5051
event = {
5152
"DurableExecutionArn": "arn:aws:lambda:us-east-1:123456789012:function:my-func:1/durable-execution/my-execution/550e8400-e29b-41d4-a716-446655440004",
5253
"CheckpointToken": "some-token",
53-
"InitialExecutionState": {"Operations": []},
54+
"InitialExecutionState": {"Operations": [{"OperationType": "EXECUTION"}]},
5455
}
5556
result = extract_durable_function_tags(event)
5657
self.assertEqual(
5758
result,
5859
{
5960
"aws_lambda.durable_function.execution_name": "my-execution",
6061
"aws_lambda.durable_function.execution_id": "550e8400-e29b-41d4-a716-446655440004",
62+
"aws_lambda.durable_function.first_invocation": "true",
63+
},
64+
)
65+
66+
def test_sets_first_invocation_false_when_multiple_operations(self):
67+
# More than one operation → replaying → not first invocation
68+
event = {
69+
"DurableExecutionArn": "arn:aws:lambda:us-east-1:123456789012:function:my-func:1/durable-execution/my-execution/550e8400-e29b-41d4-a716-446655440004",
70+
"CheckpointToken": "some-token",
71+
"InitialExecutionState": {
72+
"Operations": [
73+
{"OperationType": "EXECUTION"},
74+
{"OperationType": "STEP"},
75+
]
76+
},
77+
}
78+
result = extract_durable_function_tags(event)
79+
self.assertEqual(
80+
result,
81+
{
82+
"aws_lambda.durable_function.execution_name": "my-execution",
83+
"aws_lambda.durable_function.execution_id": "550e8400-e29b-41d4-a716-446655440004",
84+
"aws_lambda.durable_function.first_invocation": "false",
6185
},
6286
)
6387

0 commit comments

Comments
 (0)