-
Notifications
You must be signed in to change notification settings - Fork 40
Expand file tree
/
Copy pathdurable-execution.spec.ts
More file actions
148 lines (127 loc) · 4.49 KB
/
Copy pathdurable-execution.spec.ts
File metadata and controls
148 lines (127 loc) · 4.49 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
import { createDurableExecutionRootSpan, DurableExecutionEventTraceExtractor } from "./durable-execution";
import { TracerWrapper } from "../../tracer-wrapper";
jest.mock("dd-trace", () => ({
startSpan: jest.fn(),
}));
function makeTracerWrapper(extractReturn: any = null): TracerWrapper {
return { extract: jest.fn().mockReturnValue(extractReturn) } as unknown as TracerWrapper;
}
describe("DurableExecutionEventTraceExtractor", () => {
const tracer = require("dd-trace");
const startSpanMock = tracer.startSpan as jest.Mock;
beforeEach(() => {
jest.clearAllMocks();
});
it("delegates checkpoint headers to the standard propagator", () => {
const executionArn =
"arn:aws:lambda:us-east-2:123456789012:function:demo:$LATEST/durable-execution/demo/abc";
const checkpointHeaders = {
"x-datadog-trace-id": "149750110124521191",
"x-datadog-parent-id": "987654321012345678",
"x-datadog-sampling-priority": "1",
};
const event = {
DurableExecutionArn: executionArn,
CheckpointToken: "t-1",
InitialExecutionState: {
Operations: [
{
Id: "op-1",
Name: "_datadog_0",
Status: "SUCCEEDED",
StepDetails: {
Result: JSON.stringify(checkpointHeaders),
},
},
],
},
};
const sentinelContext = { sentinel: true };
const tracerWrapper = makeTracerWrapper(sentinelContext);
const extractor = new DurableExecutionEventTraceExtractor(tracerWrapper);
const context = extractor.extract(event);
expect(tracerWrapper.extract).toHaveBeenCalledWith(checkpointHeaders);
expect(context).toBe(sentinelContext);
});
it("returns null when no checkpoint or upstream context exists", () => {
const tracerWrapper = makeTracerWrapper();
const extractor = new DurableExecutionEventTraceExtractor(tracerWrapper);
const context = extractor.extract({
DurableExecutionArn: "arn:aws:lambda:us-east-2:123:function:demo",
CheckpointToken: "t-empty",
InitialExecutionState: { Operations: [] },
});
expect(context).toBeNull();
expect(tracerWrapper.extract).not.toHaveBeenCalled();
});
it("creates durable root span only for first invocation", () => {
const executionArn =
"arn:aws:lambda:us-east-2:123456789012:function:demo:$LATEST/durable-execution/demo/first";
const spanContext: any = {
_spanId: null,
_parentId: null,
toTraceId: () => "1111111111111111111",
toSpanId: () => "2222222222222222222",
};
const span = {
context: () => spanContext,
finish: jest.fn(),
};
startSpanMock.mockReturnValue(span);
const firstInvocationEvent = {
DurableExecutionArn: executionArn,
CheckpointToken: "t-first",
InitialExecutionState: {
Operations: [
{
Id: "op-1",
Name: "input",
Status: "RUNNING",
StartTimestamp: 1710000000000,
ExecutionDetails: {
InputPayload: JSON.stringify({ hello: "world" }),
},
},
],
},
};
const root = createDurableExecutionRootSpan(firstInvocationEvent, null);
expect(root).not.toBeNull();
expect(startSpanMock).toHaveBeenCalledTimes(1);
});
it("skips durable root span creation on replay invocations", () => {
const executionArn =
"arn:aws:lambda:us-east-2:123456789012:function:demo:$LATEST/durable-execution/demo/replay";
const replayEvent = {
DurableExecutionArn: executionArn,
CheckpointToken: "t-replay",
InitialExecutionState: {
Operations: [
{
Id: "op-1",
Name: "_datadog_0",
Status: "SUCCEEDED",
StepDetails: {
Result: JSON.stringify({
"x-datadog-trace-id": "149750110124521191",
"x-datadog-parent-id": "538591322263933970",
"x-datadog-sampling-priority": "1",
}),
},
},
{
Id: "op-2",
Name: "callback_step_prepare",
Status: "SUCCEEDED",
},
],
},
};
const tracerWrapper = makeTracerWrapper({ source: "Event" });
const extractor = new DurableExecutionEventTraceExtractor(tracerWrapper);
const extracted = extractor.extract(replayEvent);
const root = createDurableExecutionRootSpan(replayEvent, extracted);
expect(root).toBeNull();
expect(startSpanMock).not.toHaveBeenCalled();
});
});