-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.ts
629 lines (576 loc) · 15.9 KB
/
index.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
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
import { AwsClient } from "aws4fetch";
/**
* Request interface (e.g. `fetch`).
*/
export type Fetch = (request: Request) => Promise<Response>;
/**
* AWS X-Ray HTTP header for passing around trace information.
*/
export const AWS_TRACE_HEADER = "X-Amzn-Trace-Id";
/**
* AWS error response wrapper.
*/
export class AwsError extends Error {
constructor(public status: number) {
super(
`AWS X-Ray responded with HTTP status ${status}. Documentation: https://docs.aws.amazon.com/xray/latest/api/API_PutTraceSegments.html`
);
}
}
/**
* AWS list of unprocessed segments.
*/
export interface UnprocessedSegments {
code?: string;
id?: string;
message?: string;
}
/**
* AWS segment definition.
*/
export interface SegmentJson extends SubsegmentJson {
/**
* A unique identifier that connects all segments and subsegments originating from a single client request.
*/
trace_id: string;
/**
* An object with information about your application.
*/
service?: {
/**
* A string that identifies the version of your application that served the request.
*/
version: string;
};
/**
* A string that identifies the user who sent the request.
*/
user?: string;
/**
* A subsegment ID you specify if the request originated from an instrumented application. The X-Ray SDK adds the parent subsegment ID to the tracing header for downstream HTTP calls. In the case of nested subsegments, a subsegment can have a segment or a subsegment as its parent.
*/
parent_id?: string;
}
/**
* AWS segment HTTP definition.
*/
export interface SegmentHttpJson {
request?: {
url?: string;
method?: string;
user_agent?: string;
client_ip?: string;
x_forwarded_for?: boolean;
traced?: boolean;
};
response?: {
status?: number;
content_length?: number;
};
}
/**
* AWS sub-segment definition.
*/
export interface SubsegmentJson {
/**
* The logical name of the service that handled the request, up to 200 characters. For example, your application's name or domain name. Names can contain Unicode letters, numbers, and whitespace, and the following symbols: _, ., :, /, %, &, #, =, +, \, -, @.
*/
name: string;
/**
* A 64-bit identifier for the segment, unique among segments in the same trace, in 16 hexadecimal digits.
*/
id: string;
/**
* That is the time the segment was created, in floating point seconds in epoch time. For example, `1480615200.010` or `1.480615200010E9`. Use as many decimal places as you need. Microsecond resolution is recommended when available.
*/
start_time: number;
/**
* That is the time the segment was closed. For example, `1480615200.090` or `1.480615200090E9`. Specify either an `end_time` or `in_progress`.
*/
end_time?: number;
/**
* Set to `true` instead of specifying an `end_time` to record that a segment is started, but is not complete. Send an in-progress segment when your application receives a request that will take a long time to serve, to trace the request receipt. When the response is sent, send the complete segment to overwrite the in-progress segment. Only send one complete segment, and one or zero in-progress segments, per request.
*/
in_progress?: boolean;
/**
* Object with key-value pairs that you want X-Ray to index for search.
*/
annotations?: Record<string, string | number | boolean>;
/**
* Object with any additional data that you want to store in the segment.
*/
metadata?: Record<string, unknown>;
/**
* Array of subsegment objects.
*/
subsegments?: SubsegmentJson[];
/**
* HTTP objects with information about the original HTTP request.
*/
http?: SegmentHttpJson;
/**
* Indicates that a client error occurred (response status code was 4XX Client Error).
*/
error?: boolean;
/**
* Indicates that a server error occurred (response status code was 5XX Server Error).
*/
fault?: boolean;
/**
* Indicates that a request was throttled (response status code was 429 Too Many Requests).
*/
throttle?: boolean;
/**
* A cause can be either a 16 character exception ID or an object.
*/
cause?: string | SegmentCauseJson;
}
/**
* HTTP segment request.
*/
export interface SegmentHttpRequest {
/**
* The full URL of the request, compiled from the protocol, hostname, and path of the request.
*/
url?: string;
/**
* The request method. For example, `GET`.
*/
method?: string;
/**
* The user agent string from the requester's client.
*/
userAgent?: string;
/**
* The IP address of the requester. Can be retrieved from the IP packet's Source Address or, for forwarded requests, from an `X-Forwarded-For` header.
*/
clientIp?: string;
/**
* Boolean indicating that the client_ip was read from an X-Forwarded-For header and is not reliable as it could have been forged (segments only).
*/
xForwardedFor?: boolean;
/**
* Boolean indicating that the downstream call is to another traced service. If this field is set to true, X-Ray considers the trace to be broken until the downstream service uploads a segment with a parent_id that matches the id of the subsegment that contains this block (subsegments only).
*/
traced?: boolean;
}
/**
* HTTP segment response.
*/
export interface SegmentHttpResponse {
/**
* Number indicating the HTTP status of the response.
*/
status?: number;
/**
* Number indicating the length of the response body in bytes.
*/
contentLength?: number;
}
export interface StackFrameJson {
/**
* The relative path to the file.
*/
path?: string;
/**
* The line in the file.
*/
line?: number;
/**
* The function or method name.
*/
label?: string;
}
export interface SegmentExceptionJson {
/**
* A 64-bit identifier for the exception, unique among segments in the same trace, in 16 hexadecimal digits.
*/
id: string;
/**
* The exception message.
*/
message?: string;
/**
* The exception type.
*/
type?: string;
/**
* Indicates that the exception was caused by an error returned by a downstream service.
*/
remote?: boolean;
/**
* Indicates the number of stack frames that are omitted from the stack.
*/
truncated?: number;
/**
* Indicates the number of exceptions that were skipped between this exception and its child, that is, the exception that it caused.
*/
skipped?: number;
/**
* Exception ID of the exception's parent, that is, the exception that caused this exception.
*/
cause?: string;
/**
* Array of `StackFrame` objects.
*/
stack?: StackFrameJson[];
}
/**
* Segment cause JSON definition.
*/
export interface SegmentCauseJson {
/**
* The full path of the working directory when the exception occurred.
*/
working_directory?: string;
/**
* The array of paths to libraries or modules in use when the exception occurred.
*/
paths?: string[];
/**
* The array of exception objects.
*/
exceptions?: SegmentExceptionJson[];
}
/**
* AWS unprocessed segments error wrapper.
*/
export class UnprocessedError extends Error {
constructor(public unprocessedSegments: UnprocessedSegments[]) {
super(`AWS failed to process all segments`);
}
}
/**
* Subsegment not ended error.
*/
export class SubsegmentNotEndedError extends TypeError {
constructor(public segment: Subsegment) {
super(`Subsegment not ended: ${segment.name}`);
}
}
/**
* Capture stack frames from an error instance.
*/
export function captureStackFrames(error: Error): StackFrameJson[] {
return getErrorStack(error).map(
(frame): StackFrameJson => ({
path: frame.getFileName() ?? undefined,
line: frame.getLineNumber() ?? undefined,
label: frame.getFunctionName() ?? undefined,
})
);
}
/**
* Create a segment exception object from an error instance.
*/
export function captureException(
error: Error,
cause?: string
): SegmentExceptionJson {
return {
id: generateId(16),
message: error.message,
cause: cause,
stack: captureStackFrames(error),
};
}
/**
* Segment `cause` wrapper.
*/
export class SegmentCause {
exceptions: SegmentExceptionJson[] = [];
constructor(public directory?: string, public paths?: string[]) {}
/**
* Capture an error and any associated `cause`.
*/
captureException(error: Error) {
let err = error;
let id: string | undefined = undefined;
while (err instanceof Error) {
const exception = captureException(error, id);
this.exceptions.push(exception);
id = exception.id;
err = (err as any).cause;
}
if (this.exceptions.length === 0) {
throw new TypeError("Invalid error, should be instance of `Error`");
}
}
toJSON(): SegmentCauseJson {
return {
working_directory: this.directory,
paths: this.paths,
exceptions: this.exceptions,
};
}
}
/**
* HTTP segment instance.
*/
export class SegmentHttp {
constructor(
public request?: SegmentHttpRequest,
public response?: SegmentHttpResponse
) {}
toJSON(): SegmentHttpJson {
return {
request: this.request
? {
url: this.request.url,
method: this.request.method,
user_agent: this.request.userAgent,
client_ip: this.request.clientIp,
x_forwarded_for: this.request.xForwardedFor,
traced: this.request.traced,
}
: undefined,
response: this.response
? {
status: this.response.status,
content_length: this.response.contentLength,
}
: undefined,
};
}
}
/**
* Subsegment wrapper.
*
* Docs: https://docs.aws.amazon.com/xray/latest/devguide/xray-api-segmentdocuments.html
*/
export class Subsegment {
id = generateId(8);
startTime = Date.now();
endTime?: number;
metadata: Record<string, unknown> = {};
annotations: Record<string, string | number | boolean> = {};
subsegments: Subsegment[] = [];
http?: SegmentHttp;
error?: boolean;
throttle?: boolean;
fault?: boolean;
cause?: string | SegmentCause;
constructor(public name: string, public traceId: string) {}
/**
* Start a new subsegment.
*/
startSegment(name: string): Subsegment {
const segment = new Subsegment(name, this.traceId);
this.subsegments.push(segment);
return segment;
}
/**
* End the current segment by setting `endTime` to now.
*/
end() {
this.endTime = Date.now();
return this;
}
/**
* Capture the error instance as the cause, enables `error`.
*/
captureException(error: Error, cwd?: string) {
this.error = true;
this.cause = new SegmentCause(cwd);
this.cause.captureException(error);
}
toJSON(): SubsegmentJson {
if (!this.endTime) throw new SubsegmentNotEndedError(this);
return {
name: this.name,
id: this.id,
start_time: this.startTime / 1000,
end_time: this.endTime / 1000,
annotations: this.annotations,
metadata: this.metadata,
subsegments: this.subsegments.map((x) => x.toJSON()),
http: this.http?.toJSON(),
error: this.error,
throttle: this.throttle,
fault: this.fault,
cause: this.cause
? typeof this.cause === "string"
? this.cause
: this.cause.toJSON()
: undefined,
};
}
}
/**
* Segment instance, wraps `Subsegment` with automatic `traceId` and additional context allowed.
*/
export class Segment extends Subsegment {
service?: { version: string };
user?: string;
constructor(name: string, public traceId = generateTraceId()) {
super(name, traceId);
}
toJSON(): SegmentJson {
return {
...super.toJSON(),
trace_id: this.traceId,
user: this.user,
service: this.service,
};
}
}
/**
* AWS X-Ray constructor options.
*/
export interface AwsXrayOptions {
region: string;
accessKeyId: string;
secretAccessKey: string;
fetch?: Fetch;
}
/**
* AWS X-Ray helper instance.
*/
export class AwsXray {
region: string;
client: AwsClient;
fetch: Fetch;
constructor(options: AwsXrayOptions) {
this.fetch = options.fetch ?? fetch.bind(null);
this.region = options.region;
this.client = new AwsClient({
accessKeyId: options.accessKeyId,
secretAccessKey: options.secretAccessKey,
});
}
/**
* Submit AWS X-Ray segment to AWS.
*/
async traceSegment(segment: Segment) {
// Docs: https://docs.aws.amazon.com/xray/latest/api/API_PutTraceSegments.html
const req = await this.client.sign(
`https://xray.${this.region}.amazonaws.com/TraceSegments`,
{
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify({
TraceSegmentDocuments: [JSON.stringify(segment)],
}),
}
);
const res = await this.fetch(req);
if (!res.ok) throw new AwsError(res.status);
const { UnprocessedTraceSegments: errors = [] } = (await res.json()) as {
UnprocessedTraceSegments?: Array<{
ErrorCode?: string;
Id?: string;
Message?: string;
}>;
};
if (errors.length) {
throw new UnprocessedError(
errors.map((x) => ({ code: x.ErrorCode, id: x.Id, message: x.Message }))
);
}
}
}
/**
* Generate a unique AWS X-Ray compatible trace ID.
*/
function generateTraceId() {
return `1-${Math.floor(Date.now() / 1000).toString(16)}-${generateId(12)}`;
}
/**
* Generate unique ID for AWS X-Ray.
*/
function generateId(length: number) {
const buffer = new Uint8Array(length);
const randomBuffer = crypto.getRandomValues(buffer);
return Array.from(randomBuffer, (b) => `00${b.toString(16)}`.slice(-2)).join(
""
);
}
/**
* Captured `fetch` type.
*/
export type CapturedFetch = (
input: RequestInfo,
init: RequestInit & { segment: Subsegment }
) => Promise<Response>;
/**
* Wrap an instance of `fetch` with AWS X-Ray tracing and optional trace header forwarding.
*/
export function captureFetch(
fetch: (input: Request) => Promise<Response>,
forwardTrace = false
): CapturedFetch {
return async (input, init) => {
const request = new Request(input, init);
const url = new URL(request.url);
const segment = init.segment.startSegment(url.hostname);
segment.http = new SegmentHttp({
url: request.url,
method: request.method,
traced: forwardTrace,
});
if (forwardTrace) {
request.headers.set(
AWS_TRACE_HEADER,
`Root=${segment.traceId};Parent=${segment.id};Sampled=1`
);
}
try {
const res = await fetch(request);
const code = ~~(res.status / 100);
const contentLength = res.headers.get("Content-Length");
segment.error = code === 4;
segment.fault = code === 5;
segment.throttle = res.status === 429;
segment.http.response = {
status: res.status,
contentLength: (contentLength && Number(contentLength)) || undefined,
};
return res;
} catch (err) {
segment.captureException(err);
throw err;
} finally {
segment.end();
}
};
}
/**
* V8 call site trace.
*
* Ref: https://v8.dev/docs/stack-trace-api
*/
interface CallSite {
getThis(): any;
getTypeName(): string | null;
getFunction(): (...args: any) => any | undefined;
getFunctionName(): string | null;
getMethodName(): string | null;
getFileName(): string | null;
getLineNumber(): number | null;
getColumnNumber(): number | null;
getEvalOrigin(): string | undefined;
isToplevel(): boolean;
isEval(): boolean;
isNative(): boolean;
isConstructor(): boolean;
isAsync(): boolean;
isPromiseAll(): boolean;
getPromiseIndex(): number | null;
}
/**
* Parse V8 call sites from error instance.
*/
function getErrorStack(error: Error): CallSite[] {
const prepareStackTrace = Error.prepareStackTrace;
let trace: CallSite[];
Error.prepareStackTrace = (error, v8Trace) => {
trace = v8Trace as CallSite[];
return prepareStackTrace?.(error, v8Trace);
};
Error.captureStackTrace(error, getErrorStack);
error.stack; // Triggers `prepareStackTrace`.
Error.prepareStackTrace = prepareStackTrace;
return trace!;
}