Skip to content

Commit 1f8a5e7

Browse files
lym953claude
andauthored
feat: [SVLS-8721] add first_invocation tag for durable functions (#761)
* remove logDebug from durable function tag application Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com> * add back logDebug for durable function tag application Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com> * feat: [SVLS-8493] add first_invocation tag for durable functions Tag `aws_lambda.durable_function.first_invocation` is set to "true" when `InitialExecutionState.Operations` has exactly one entry (first invocation) and "false" when it has more (replay). Omitted when InitialExecutionState is absent. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com> --------- Co-authored-by: Claude Sonnet 4.6 <noreply@anthropic.com>
1 parent f8bbb2a commit 1f8a5e7

File tree

2 files changed

+48
-1
lines changed

2 files changed

+48
-1
lines changed

src/trace/durable-function-context.spec.ts

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,9 +58,47 @@ describe("durable-function-context", () => {
5858
expect(result).toEqual({
5959
"aws_lambda.durable_function.execution_name": "my-execution",
6060
"aws_lambda.durable_function.execution_id": "550e8400-e29b-41d4-a716-446655440004",
61+
"aws_lambda.durable_function.first_invocation": "false",
6162
});
6263
});
6364

65+
it("sets first_invocation to true when Operations has exactly one entry", () => {
66+
const event = {
67+
DurableExecutionArn:
68+
"arn:aws:lambda:us-east-1:123456789012:function:my-func:1/durable-execution/my-execution/550e8400-e29b-41d4-a716-446655440004",
69+
InitialExecutionState: {
70+
Operations: [{ type: "TaskScheduled" }],
71+
},
72+
};
73+
const result = extractDurableFunctionContext(event);
74+
75+
expect(result?.["aws_lambda.durable_function.first_invocation"]).toBe("true");
76+
});
77+
78+
it("sets first_invocation to false when Operations has more than one entry", () => {
79+
const event = {
80+
DurableExecutionArn:
81+
"arn:aws:lambda:us-east-1:123456789012:function:my-func:1/durable-execution/my-execution/550e8400-e29b-41d4-a716-446655440004",
82+
InitialExecutionState: {
83+
Operations: [{ type: "TaskScheduled" }, { type: "TaskCompleted" }],
84+
},
85+
};
86+
const result = extractDurableFunctionContext(event);
87+
88+
expect(result?.["aws_lambda.durable_function.first_invocation"]).toBe("false");
89+
});
90+
91+
it("omits first_invocation when InitialExecutionState is absent", () => {
92+
const event = {
93+
DurableExecutionArn:
94+
"arn:aws:lambda:us-east-1:123456789012:function:my-func:1/durable-execution/my-execution/550e8400-e29b-41d4-a716-446655440004",
95+
};
96+
const result = extractDurableFunctionContext(event);
97+
98+
expect(result).toBeDefined();
99+
expect(result?.["aws_lambda.durable_function.first_invocation"]).toBeUndefined();
100+
});
101+
64102
it("returns undefined for regular Lambda event without DurableExecutionArn", () => {
65103
const event = {
66104
body: '{"key": "value"}',

src/trace/durable-function-context.ts

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { logDebug } from "../utils";
33
export interface DurableFunctionContext {
44
"aws_lambda.durable_function.execution_name": string;
55
"aws_lambda.durable_function.execution_id": string;
6+
"aws_lambda.durable_function.first_invocation"?: string;
67
}
78

89
export function extractDurableFunctionContext(event: any): DurableFunctionContext | undefined {
@@ -18,10 +19,18 @@ export function extractDurableFunctionContext(event: any): DurableFunctionContex
1819
return undefined;
1920
}
2021

21-
return {
22+
const context: DurableFunctionContext = {
2223
"aws_lambda.durable_function.execution_name": parsed.executionName,
2324
"aws_lambda.durable_function.execution_id": parsed.executionId,
2425
};
26+
27+
// Use the number of operations to determine if it's the first invocation.
28+
const operations = event?.InitialExecutionState?.Operations;
29+
if (Array.isArray(operations)) {
30+
context["aws_lambda.durable_function.first_invocation"] = String(operations.length === 1);
31+
}
32+
33+
return context;
2534
}
2635

2736
/**

0 commit comments

Comments
 (0)