-
Notifications
You must be signed in to change notification settings - Fork 154
/
Copy pathxray-traces-utils.ts
365 lines (321 loc) · 10.2 KB
/
xray-traces-utils.ts
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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
import {
BatchGetTracesCommand,
GetTraceSummariesCommand,
type Trace,
XRayClient,
} from '@aws-sdk/client-xray';
import promiseRetry from 'promise-retry';
import type {
EnrichedXRayTraceDocumentParsed,
GetXRayTraceDetailsOptions,
GetXRayTraceIdsOptions,
XRaySegmentParsed,
XRayTraceDocumentParsed,
XRayTraceParsed,
} from './types.js';
const retryOptions = {
retries: 20,
minTimeout: 5_000,
maxTimeout: 10_000,
factor: 1.25,
};
const xrayClient = new XRayClient({});
/**
* Get the trace IDs for a given resource name from the AWS X-Ray API
*
* @param options - The options to get trace IDs, including the start time, resource name, and expected traces count
*/
const getTraceIds = async (
options: GetXRayTraceIdsOptions
): Promise<string[]> => {
const { startTime, resourceName, expectedTracesCount } = options;
const endTime = new Date();
const response = await xrayClient.send(
new GetTraceSummariesCommand({
StartTime: startTime,
EndTime: endTime,
FilterExpression: `resource.arn ENDSWITH ":function:${resourceName}"`,
})
);
const summaries = response.TraceSummaries;
if (summaries === undefined || summaries.length !== expectedTracesCount) {
throw new Error(
`Expected ${expectedTracesCount} trace summaries, got ${summaries ? summaries.length : 0} for ${resourceName}`
);
}
const ids: string[] = [];
for (const summary of summaries) {
if (summary.Id === undefined) {
throw new Error(
`Expected all trace summaries to have an ID for ${resourceName}`
);
}
ids.push(summary.Id);
}
return ids;
};
/**
* Retriable version of {@link getTraceIds}
*
* @param options - The options to get trace IDs, including the start time, resource name, and expected traces count
*/
const retriableGetTraceIds = (options: GetXRayTraceIdsOptions) =>
promiseRetry(async (retry, attempt) => {
try {
return await getTraceIds(options);
} catch (error) {
if (attempt === retryOptions.retries) {
const endTime = new Date();
console.log(
`Manual query: aws xray get-trace-summaries --start-time ${Math.floor(
options.startTime.getTime() / 1000
)} --end-time ${Math.floor(
endTime.getTime() / 1000
)} --filter-expression 'resource.arn ENDSWITH ":function:${options.resourceName}"'`
);
throw new Error(
`Failed to get trace IDs after ${retryOptions.retries} retries`,
{ cause: error }
);
}
retry(error);
}
}, retryOptions);
/**
* Find the main Powertools subsegment in the trace
*
* A main Powertools subsegment is identified by the `## index.` suffix. Depending on the
* runtime, it may also be identified by the `Invocation` name.
*
* @param trace - The trace to find the main Powertools subsegment
* @param functionName - The function name to find the main Powertools subsegment
*/
const findMainPowertoolsSubsegment = (
trace: XRayTraceDocumentParsed,
functionName: string
) => {
const maybePowertoolsSubsegment = trace.subsegments?.find(
(subsegment) =>
subsegment.name.startsWith('## index.') ||
subsegment.name === 'Invocation'
);
if (!maybePowertoolsSubsegment) {
throw new Error(`Main subsegment not found for ${functionName} segment`);
}
if (maybePowertoolsSubsegment.name === 'Invocation') {
const powertoolsSubsegment = maybePowertoolsSubsegment.subsegments?.find(
(subsegment) => subsegment.name.startsWith('## index.')
);
if (!powertoolsSubsegment) {
throw new Error(`Main subsegment not found for ${functionName} segment`);
}
return powertoolsSubsegment;
}
return maybePowertoolsSubsegment;
};
/**
* Parse and sort the trace segments by start time
*
* @param trace - The trace to parse and sort
* @param expectedSegmentsCount - The expected segments count for the trace
* @param functionName - The function name to find the main Powertools subsegment
*/
const parseAndSortTrace = (
trace: Trace,
expectedSegmentsCount: number,
functionName: string
) => {
const { Id: id, Segments: segments } = trace;
if (segments === undefined || segments.length !== expectedSegmentsCount) {
throw new Error(
`Expected ${expectedSegmentsCount} segments, got ${segments ? segments.length : 0} for traceId ${trace.Id}`
);
}
const parsedSegments: XRaySegmentParsed[] = [];
for (const segment of segments) {
const { Id, Document } = segment;
if (Document === undefined || Id === undefined) {
throw new Error(
`Segment document or id are missing for traceId ${trace.Id}`
);
}
const parsedDocument = JSON.parse(Document) as XRayTraceDocumentParsed;
if (parsedDocument.origin === 'AWS::Lambda::Function') {
findMainPowertoolsSubsegment(parsedDocument, functionName);
}
parsedSegments.push({
Id,
Document: parsedDocument,
});
}
return {
Id: id as string,
Segments: [...parsedSegments].sort(
(a, b) => a.Document.start_time - b.Document.start_time
),
};
};
/**
* Get the trace details for a given trace ID from the AWS X-Ray API.
*
* When the trace is returned, the segments are parsed, since the document is returned
* stringified, and then sorted by start time.
*
* @param options - The options to get trace details, including the trace IDs and expected segments count
*/
const getTraceDetails = async (
options: GetXRayTraceDetailsOptions
): Promise<XRayTraceParsed[]> => {
const { traceIds, expectedSegmentsCount, functionName } = options;
const response = await xrayClient.send(
new BatchGetTracesCommand({
TraceIds: traceIds,
})
);
const { Traces: traces } = response;
if (traces === undefined || traces.length !== traceIds.length) {
throw new Error(
`Expected ${traceIds.length} traces, got ${traces ? traces.length : 0}`
);
}
const parsedAndSortedTraces: XRayTraceParsed[] = [];
for (const trace of traces) {
parsedAndSortedTraces.push(
parseAndSortTrace(trace, expectedSegmentsCount, functionName)
);
}
return parsedAndSortedTraces.sort(
(a, b) =>
a.Segments[0].Document.start_time - b.Segments[0].Document.start_time
);
};
/**
* Retriable version of {@link getTraceDetails}
*
* @param options - The options to get trace details, including the trace IDs and expected segments count
*/
const retriableGetTraceDetails = (options: GetXRayTraceDetailsOptions) =>
promiseRetry(async (retry, attempt) => {
try {
return await getTraceDetails(options);
} catch (error) {
if (attempt === retryOptions.retries) {
console.log(
`Manual query: aws xray batch-get-traces --trace-ids ${
options.traceIds
}`
);
throw new Error(
`Failed to get trace details after ${retryOptions.retries} retries`,
{ cause: error }
);
}
retry(error);
}
}, retryOptions);
/**
* Find the main function segment within the `AWS::Lambda::Function` segment
*/
const findPowertoolsFunctionSegment = (
trace: XRayTraceParsed,
functionName: string
): XRayTraceDocumentParsed => {
const functionSegment = trace.Segments.find(
(segment) => segment.Document.origin === 'AWS::Lambda::Function'
);
if (!functionSegment) {
throw new Error(
`AWS::Lambda::Function segment not found for ${functionName}`
);
}
const document = functionSegment.Document;
return findMainPowertoolsSubsegment(document, functionName);
};
/**
* Parse the subsegments of a segment by name.
*
* The subsegments are split into a map where the key is the name of the subsegment
* and the value is an array of subsegments with that name.
*
* This is useful to more easily assert the presence of specific subsegments in a segment.
*
* @param subsegments - The subsegments to parse
* @param expectedNames - The expected names to map the subsegments with
*/
const parseSubsegmentsByName = (
subsegments: XRayTraceDocumentParsed[]
): Map<string, XRayTraceDocumentParsed> => {
const subsegmentMap = new Map<string, XRayTraceDocumentParsed>();
for (const subsegment of subsegments) {
subsegmentMap.set(subsegment.name, subsegment);
}
return subsegmentMap;
};
/**
* Get the X-Ray trace data for a given resource name.
*
* @param options - The options to get the X-Ray trace data, including the start time, resource name, expected traces count, and expected segments count
*/
const getXRayTraceData = async (
options: GetXRayTraceIdsOptions & Omit<GetXRayTraceDetailsOptions, 'traceIds'>
) => {
const {
startTime,
resourceName,
expectedTracesCount,
expectedSegmentsCount,
} = options;
const traceIds = await retriableGetTraceIds({
startTime,
resourceName,
expectedTracesCount,
});
if (!traceIds) {
throw new Error(`No trace IDs found for ${resourceName}`);
}
const traces = await retriableGetTraceDetails({
traceIds,
expectedSegmentsCount,
functionName: resourceName,
});
if (!traces) {
throw new Error(`No traces found for ${resourceName}`);
}
return traces;
};
/**
* Get the X-Ray trace data for a given resource name and parse the main subsegments.
*
* @param options - The options to get the X-Ray trace data, including the start time, resource name, expected traces count, and expected segments count
*/
const getTraces = async (
options: GetXRayTraceIdsOptions &
Omit<GetXRayTraceDetailsOptions, 'traceIds' | 'functionName'> & {
resourceName: string;
}
): Promise<EnrichedXRayTraceDocumentParsed[]> => {
const traces = await getXRayTraceData({
...options,
functionName: options.resourceName,
});
const { resourceName } = options;
const mainSubsegments: EnrichedXRayTraceDocumentParsed[] = [];
for (const trace of traces) {
const mainSubsegment = findPowertoolsFunctionSegment(trace, resourceName);
const enrichedMainSubsegment = {
...mainSubsegment,
subsegments: parseSubsegmentsByName(mainSubsegment.subsegments ?? []),
};
mainSubsegments.push(enrichedMainSubsegment);
}
return mainSubsegments;
};
export {
getTraceIds,
retriableGetTraceIds,
getTraceDetails,
retriableGetTraceDetails,
findPowertoolsFunctionSegment,
getTraces,
parseSubsegmentsByName,
};