-
Notifications
You must be signed in to change notification settings - Fork 40
Expand file tree
/
Copy pathextractor-utils.spec.ts
More file actions
164 lines (146 loc) · 6.01 KB
/
extractor-utils.spec.ts
File metadata and controls
164 lines (146 loc) · 6.01 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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
import { TracerWrapper } from "../tracer-wrapper";
import { extractTraceContext, extractFromAWSTraceHeader } from "./extractor-utils";
import { StepFunctionContextService } from "../step-function-service";
describe("extractor-utils", () => {
beforeEach(() => {
StepFunctionContextService["_instance"] = undefined as any;
});
describe("extractTraceContext", () => {
it("returns span context when tracer wrapper successfully extracts from headers", () => {
const legacyStepFunctionEvent = {
Execution: {
Id: "arn:aws:states:sa-east-1:425362996713:express:logs-to-traces-sequential:85a9933e-9e11-83dc-6a61-b92367b6c3be:3f7ef5c7-c8b8-4c88-90a1-d54aa7e7e2bf",
Input: {
MyInput: "MyValue",
},
Name: "85a9933e-9e11-83dc-6a61-b92367b6c3be",
RoleArn: "arn:aws:iam::425362996713:role/service-role/StepFunctions-logs-to-traces-sequential-role-ccd69c03",
RedriveCount: 0,
StartTime: "2022-12-08T21:08:17.924Z",
},
State: {
Name: "step-one",
EnteredTime: "2022-12-08T21:08:19.224Z",
RetryCount: 2,
},
StateMachine: {
Id: "arn:aws:states:sa-east-1:425362996713:stateMachine:logs-to-traces-sequential",
Name: "my-state-machine",
},
};
const tracerWrapper = new TracerWrapper();
const result = extractTraceContext(legacyStepFunctionEvent, tracerWrapper);
// Should return a span context from Step Function context since headers extraction fails
expect(result).not.toBeNull();
});
it("returns null when no trace context can be extracted", () => {
const emptyEvent = {
someOtherProperty: "value",
};
const tracerWrapper = new TracerWrapper();
const result = extractTraceContext(emptyEvent, tracerWrapper);
expect(result).toBeNull();
});
it("extracts context from LambdaRootStepFunctionContext", () => {
const lambdaRootStepFunctionEvent = {
_datadog: {
Execution: {
Id: "arn:aws:states:sa-east-1:425362996713:express:logs-to-traces-sequential:85a9933e-9e11-83dc-6a61-b92367b6c3be:3f7ef5c7-c8b8-4c88-90a1-d54aa7e7e2bf",
Input: {
MyInput: "MyValue",
},
Name: "85a9933e-9e11-83dc-6a61-b92367b6c3be",
RoleArn:
"arn:aws:iam::425362996713:role/service-role/StepFunctions-logs-to-traces-sequential-role-ccd69c03",
RedriveCount: 0,
StartTime: "2022-12-08T21:08:17.924Z",
},
State: {
Name: "step-one",
EnteredTime: "2022-12-08T21:08:19.224Z",
RetryCount: 2,
},
StateMachine: {
Id: "arn:aws:states:sa-east-1:425362996713:stateMachine:logs-to-traces-sequential",
Name: "my-state-machine",
},
"x-datadog-trace-id": "10593586103637578129",
"x-datadog-tags": "_dd.p.dm=-0,_dd.p.tid=6734e7c300000000",
"serverless-version": "v1",
},
};
const tracerWrapper = new TracerWrapper();
const result = extractTraceContext(lambdaRootStepFunctionEvent, tracerWrapper);
expect(result).not.toBeNull();
});
it("extracts context from NestedStepFunctionContext", () => {
const nestedStepFunctionEvent = {
_datadog: {
Execution: {
Id: "arn:aws:states:sa-east-1:425362996713:express:logs-to-traces-sequential:85a9933e-9e11-83dc-6a61-b92367b6c3be:3f7ef5c7-c8b8-4c88-90a1-d54aa7e7e2bf",
Input: {
MyInput: "MyValue",
},
Name: "85a9933e-9e11-83dc-6a61-b92367b6c3be",
RoleArn:
"arn:aws:iam::425362996713:role/service-role/StepFunctions-logs-to-traces-sequential-role-ccd69c03",
RedriveCount: 0,
StartTime: "2022-12-08T21:08:17.924Z",
},
State: {
Name: "step-one",
EnteredTime: "2022-12-08T21:08:19.224Z",
RetryCount: 2,
},
StateMachine: {
Id: "arn:aws:states:sa-east-1:425362996713:stateMachine:logs-to-traces-sequential",
Name: "my-state-machine",
},
RootExecutionId:
"arn:aws:states:sa-east-1:425362996713:express:logs-to-traces-sequential:a1b2c3d4-e5f6-7890-1234-56789abcdef0:9f8e7d6c-5b4a-3c2d-1e0f-123456789abc",
"serverless-version": "v1",
},
};
const tracerWrapper = new TracerWrapper();
const result = extractTraceContext(nestedStepFunctionEvent, tracerWrapper);
expect(result).not.toBeNull();
});
it("extracts context from legacy lambda StepFunctionContext", () => {
const event = {
Payload: {
Execution: {
Id: "arn:aws:states:sa-east-1:425362996713:express:logs-to-traces-sequential:85a9933e-9e11-83dc-6a61-b92367b6c3be:3f7ef5c7-c8b8-4c88-90a1-d54aa7e7e2bf",
Input: {
MyInput: "MyValue",
},
Name: "85a9933e-9e11-83dc-6a61-b92367b6c3be",
RoleArn:
"arn:aws:iam::425362996713:role/service-role/StepFunctions-logs-to-traces-sequential-role-ccd69c03",
RedriveCount: 0,
StartTime: "2022-12-08T21:08:17.924Z",
},
State: {
Name: "step-one",
EnteredTime: "2022-12-08T21:08:19.224Z",
RetryCount: 2,
},
StateMachine: {
Id: "arn:aws:states:sa-east-1:425362996713:stateMachine:logs-to-traces-sequential",
Name: "my-state-machine",
},
},
};
const tracerWrapper = new TracerWrapper();
const result = extractTraceContext(event, tracerWrapper);
expect(result).not.toBeNull();
});
});
describe("extractFromAWSTraceHeader", () => {
it("returns null when AWS trace header is invalid", () => {
const invalidHeader = "invalid-header";
const eventType = "SQS";
const result = extractFromAWSTraceHeader(invalidHeader, eventType);
expect(result).toBeNull();
});
});
});