-
Notifications
You must be signed in to change notification settings - Fork 38
/
aws-sdk.ts
458 lines (404 loc) · 19.8 KB
/
aws-sdk.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
/*
For the request to be sent, one of the two conditions has to be true:
a callback was passed to the request
a promise was retrieved from the request
Number of times "onComplete" event is fired:
| w/o promise() | w/ promise()
no callback | 0 | 1
callback | 1 | 2
*/
import { Span, SpanKind, context, trace, Context, diag, SpanStatusCode } from '@opentelemetry/api';
import { suppressTracing } from '@opentelemetry/core';
import type AWS from 'aws-sdk';
import { AttributeNames } from './enums';
import { ServicesExtensions } from './services';
import { AwsSdkInstrumentationConfig, NormalizedRequest, NormalizedResponse } from './types';
import { VERSION } from './version';
import {
InstrumentationBase,
InstrumentationModuleDefinition,
InstrumentationNodeModuleDefinition,
InstrumentationNodeModuleFile,
isWrapped,
safeExecuteInTheMiddle,
} from '@opentelemetry/instrumentation';
import type {
MiddlewareStack,
HandlerExecutionContext,
Command as AwsV3Command,
Handler as AwsV3MiddlewareHandler,
} from '@aws-sdk/types';
import {
bindPromise,
extractAttributesFromNormalizedRequest,
normalizeV2Request,
normalizeV3Request,
removeSuffixFromStringIfExists,
} from './utils';
import { RequestMetadata } from './services/ServiceExtension';
const storedV3ClientConfig = Symbol('opentelemetry.aws-sdk.client.config');
export class AwsInstrumentation extends InstrumentationBase<typeof AWS> {
static readonly component = 'aws-sdk';
protected override _config!: AwsSdkInstrumentationConfig;
private REQUEST_SPAN_KEY = Symbol('opentelemetry.instrumentation.aws-sdk.span');
private servicesExtensions: ServicesExtensions = new ServicesExtensions();
constructor(config: AwsSdkInstrumentationConfig = {}) {
super('opentelemetry-instrumentation-aws-sdk', VERSION, Object.assign({}, config));
}
override setConfig(config: AwsSdkInstrumentationConfig = {}) {
this._config = Object.assign({}, config);
}
protected init(): InstrumentationModuleDefinition<typeof AWS>[] {
const v3MiddlewareStackFile = new InstrumentationNodeModuleFile(
`@aws-sdk/middleware-stack/dist/cjs/MiddlewareStack.js`,
['^3.0.0'],
this.patchV3ConstructStack.bind(this),
this.unpatchV3ConstructStack.bind(this)
);
// as for aws-sdk v3.13.1, constructStack is exported from @aws-sdk/middleware-stack as
// getter instead of function, which fails shimmer.
// so we are patching the MiddlewareStack.js file directly to get around it.
const v3MiddlewareStack = new InstrumentationNodeModuleDefinition<typeof AWS>(
'@aws-sdk/middleware-stack',
['^3.0.0'],
undefined,
undefined,
[v3MiddlewareStackFile]
);
const v3SmithyClient = new InstrumentationNodeModuleDefinition<typeof AWS>(
'@aws-sdk/smithy-client',
['^3.0.0'],
this.patchV3SmithyClient.bind(this),
this.unpatchV3SmithyClient.bind(this)
);
const v2Request = new InstrumentationNodeModuleFile<typeof AWS.Request>(
'aws-sdk/lib/core.js',
['^2.17.0'],
this.patchV2.bind(this),
this.unpatchV2.bind(this)
);
const v2Module = new InstrumentationNodeModuleDefinition<typeof AWS>(
'aws-sdk',
['^2.17.0'],
undefined,
undefined,
[v2Request]
);
return [v2Module, v3MiddlewareStack, v3SmithyClient];
}
protected patchV3ConstructStack(moduleExports, moduleVersion: string) {
diag.debug(`aws-sdk instrumentation: applying patch to aws-sdk v3 constructStack`);
this._wrap(moduleExports, 'constructStack', this._getV3ConstructStackPatch.bind(this, moduleVersion));
return moduleExports;
}
protected unpatchV3ConstructStack(moduleExports) {
diag.debug(`aws-sdk instrumentation: applying unpatch to aws-sdk v3 constructStack`);
this._unwrap(moduleExports, 'constructStack');
return moduleExports;
}
protected patchV3SmithyClient(moduleExports) {
diag.debug(`aws-sdk instrumentation: applying patch to aws-sdk v3 client send`);
this._wrap(moduleExports.Client.prototype, 'send', this._getV3SmithyClientSendPatch.bind(this));
return moduleExports;
}
protected unpatchV3SmithyClient(moduleExports) {
diag.debug(`aws-sdk instrumentation: applying patch to aws-sdk v3 constructStack`);
this._unwrap(moduleExports.Client.prototype, 'send');
return moduleExports;
}
protected patchV2(moduleExports: typeof AWS, moduleVersion: string) {
diag.debug(`aws-sdk instrumentation: applying patch to ${AwsInstrumentation.component}`);
this.unpatchV2(moduleExports);
this._wrap(moduleExports?.Request.prototype, 'send', this._getRequestSendPatch.bind(this, moduleVersion));
this._wrap(moduleExports?.Request.prototype, 'promise', this._getRequestPromisePatch.bind(this, moduleVersion));
return moduleExports;
}
protected unpatchV2(moduleExports: typeof AWS) {
if (isWrapped(moduleExports?.Request.prototype.send)) {
this._unwrap(moduleExports.Request.prototype, 'send');
}
if (isWrapped(moduleExports?.Request.prototype.promise)) {
this._unwrap(moduleExports.Request.prototype, 'promise');
}
}
private _startAwsV3Span(
normalizedRequest: NormalizedRequest,
metadata: RequestMetadata,
moduleVersion: string
): Span {
const name = metadata.spanName ?? `${normalizedRequest.serviceName}.${normalizedRequest.commandName}`;
const newSpan = this.tracer.startSpan(name, {
kind: metadata.spanKind,
attributes: {
...extractAttributesFromNormalizedRequest(normalizedRequest),
...metadata.spanAttributes,
},
});
if (this._config.moduleVersionAttributeName) {
newSpan.setAttribute(this._config.moduleVersionAttributeName, moduleVersion);
}
return newSpan;
}
private _startAwsV2Span(
request: AWS.Request<any, any>,
metadata: RequestMetadata,
normalizedRequest: NormalizedRequest,
moduleVersion: string
): Span {
const operation = (request as any).operation;
const service = (request as any).service;
const serviceIdentifier = service?.serviceIdentifier;
const name = metadata.spanName ?? this._getSpanName(serviceIdentifier, operation);
const newSpan = this.tracer.startSpan(name, {
kind: metadata.spanKind ?? SpanKind.CLIENT,
attributes: {
[AttributeNames.AWS_OPERATION]: operation,
[AttributeNames.AWS_SIGNATURE_VERSION]: service?.config?.signatureVersion,
[AttributeNames.AWS_SERVICE_API]: service?.api?.className,
[AttributeNames.AWS_SERVICE_IDENTIFIER]: serviceIdentifier,
[AttributeNames.AWS_SERVICE_NAME]: service?.api?.abbreviation,
...extractAttributesFromNormalizedRequest(normalizedRequest),
...metadata.spanAttributes,
},
});
if (this._config.moduleVersionAttributeName) {
newSpan.setAttribute(this._config.moduleVersionAttributeName, moduleVersion);
}
return newSpan;
}
private _callUserPreRequestHook(span: Span, request: NormalizedRequest) {
if (this._config?.preRequestHook) {
safeExecuteInTheMiddle(
() => this._config.preRequestHook(span, request),
(e: Error) => {
if (e) diag.error(`${AwsInstrumentation.component} instrumentation: preRequestHook error`, e);
},
true
);
}
}
private _callUserResponseHook(span: Span, response: NormalizedResponse) {
const responseHook = this._config?.responseHook;
if (!responseHook) return;
safeExecuteInTheMiddle(
() => responseHook(span, response),
(e: Error) => {
if (e) diag.error(`${AwsInstrumentation.component} instrumentation: responseHook error`, e);
},
true
);
}
private _registerV2CompletedEvent(
span: Span,
v2Request: AWS.Request<any, any>,
normalizedRequest: NormalizedRequest,
completedEventContext: Context
) {
const self = this;
v2Request.on('complete', (response) => {
// read issue https://github.com/aspecto-io/opentelemetry-ext-js/issues/60
context.with(completedEventContext, () => {
if (!v2Request[self.REQUEST_SPAN_KEY]) {
return;
}
delete v2Request[self.REQUEST_SPAN_KEY];
const normalizedResponse: NormalizedResponse = {
data: response.data,
request: normalizedRequest,
};
self._callUserResponseHook(span, normalizedResponse);
if (response.error) {
span.setAttribute(AttributeNames.AWS_ERROR, response.error);
} else {
this.servicesExtensions.responseHook(normalizedResponse, span, self.tracer, self._config);
}
span.setAttribute(AttributeNames.AWS_REQUEST_ID, response.requestId);
span.end();
});
});
}
private _getV3ConstructStackPatch(
moduleVersion: string,
original: (...args: unknown[]) => MiddlewareStack<any, any>
) {
const self = this;
return function constructStack(...args: unknown[]): MiddlewareStack<any, any> {
const stack: MiddlewareStack<any, any> = original.apply(this, args);
self.patchV3MiddlewareStack(moduleVersion, stack);
return stack;
};
}
private _getV3SmithyClientSendPatch(original: (...args: unknown[]) => Promise<any>) {
const self = this;
return function send(command: AwsV3Command<any, any, any, any, any>): Promise<any> {
command[storedV3ClientConfig] = this.config;
return original.apply(this, arguments);
};
}
private patchV3MiddlewareStack(moduleVersion: string, middlewareStackToPatch: MiddlewareStack<any, any>) {
if (!isWrapped(middlewareStackToPatch.resolve)) {
this._wrap(
middlewareStackToPatch,
'resolve',
this._getV3MiddlewareStackResolvePatch.bind(this, moduleVersion)
);
}
// 'clone' and 'concat' functions are internally calling 'constructStack' which is in same
// module, thus not patched, and we need to take care of it specifically.
this._wrap(middlewareStackToPatch, 'clone', this._getV3MiddlewareStackClonePatch.bind(this, moduleVersion));
this._wrap(middlewareStackToPatch, 'concat', this._getV3MiddlewareStackClonePatch.bind(this, moduleVersion));
}
private _getV3MiddlewareStackClonePatch(
moduleVersion: string,
original: (...args: unknown[]) => MiddlewareStack<any, any>
) {
const self = this;
return function (...args: unknown[]) {
const newStack = original.apply(this, arguments);
self.patchV3MiddlewareStack(moduleVersion, newStack);
return newStack;
};
}
private _getV3MiddlewareStackResolvePatch(
moduleVersion: string,
original: (_handler: unknown, context: HandlerExecutionContext) => AwsV3MiddlewareHandler<any, any>
) {
const self = this;
return function (
_handler: unknown,
awsExecutionContext: HandlerExecutionContext
): AwsV3MiddlewareHandler<any, any> {
const origHandler = original.apply(this, arguments);
const patchedHandler = function (command: AwsV3Command<any, any, any, any, any>): Promise<any> {
const clientConfig = command[storedV3ClientConfig];
const regionPromise = clientConfig?.region?.();
const serviceName =
clientConfig.serviceId ?? removeSuffixFromStringIfExists(awsExecutionContext.clientName, 'Client');
const commandName = awsExecutionContext.commandName ?? command.constructor?.name;
const normalizedRequest = normalizeV3Request(serviceName, commandName, command.input, undefined);
const requestMetadata = self.servicesExtensions.requestPreSpanHook(normalizedRequest);
const span = self._startAwsV3Span(normalizedRequest, requestMetadata, moduleVersion);
const activeContextWithSpan = trace.setSpan(context.active(), span);
const handlerPromise = new Promise(async (resolve, reject) => {
try {
const resolvedRegion = await Promise.resolve(regionPromise);
normalizedRequest.region = resolvedRegion;
span.setAttribute(AttributeNames.AWS_REGION, resolvedRegion);
} catch (e) {
// there is nothing much we can do in this case.
// we'll just continue without region
diag.debug(
`${AwsInstrumentation.component} instrumentation: failed to extract region from async function`,
e
);
}
self._callUserPreRequestHook(span, normalizedRequest);
const resultPromise = context.with(activeContextWithSpan, () => {
self.servicesExtensions.requestPostSpanHook(normalizedRequest);
return self._callOriginalFunction(() => origHandler.apply(this, arguments));
});
const promiseWithResponseLogic = resultPromise
.then((response) => {
const requestId = response.output?.$metadata?.requestId;
if (requestId) {
span.setAttribute(AttributeNames.AWS_REQUEST_ID, requestId);
}
const extendedRequestId = response.output?.$metadata?.extendedRequestId;
if (extendedRequestId) {
span.setAttribute(AttributeNames.AWS_REQUEST_EXTENDED_ID, extendedRequestId);
}
const normalizedResponse: NormalizedResponse = {
data: response.output,
request: normalizedRequest,
};
self.servicesExtensions.responseHook(normalizedResponse, span, self.tracer, self._config);
self._callUserResponseHook(span, normalizedResponse);
return response;
})
.catch((err) => {
const requestId = err?.RequestId;
if (requestId) {
span.setAttribute(AttributeNames.AWS_REQUEST_ID, requestId);
}
const extendedRequestId = err?.extendedRequestId;
if (extendedRequestId) {
span.setAttribute(AttributeNames.AWS_REQUEST_EXTENDED_ID, extendedRequestId);
}
span.setStatus({ code: SpanStatusCode.ERROR, message: err.message });
span.recordException(err);
throw err;
})
.finally(() => {
span.end();
});
promiseWithResponseLogic
.then((res) => {
resolve(res);
})
.catch((err) => reject(err));
});
return requestMetadata.isIncoming
? bindPromise(handlerPromise, activeContextWithSpan, 2)
: handlerPromise;
};
return patchedHandler;
};
}
private _getRequestSendPatch(moduleVersion: string, original: (callback?: (err: any, data: any) => void) => void) {
const self = this;
return function (callback?: (err: any, data: any) => void) {
const awsV2Request: AWS.Request<any, any> = this;
/*
if the span was already started, we don't want to start a new one
when Request.promise() is called
*/
if (this._asm.currentState === 'complete' || awsV2Request[self.REQUEST_SPAN_KEY]) {
return original.apply(this, arguments);
}
const normalizedRequest = normalizeV2Request(awsV2Request);
const requestMetadata = self.servicesExtensions.requestPreSpanHook(normalizedRequest);
const span = self._startAwsV2Span(awsV2Request, requestMetadata, normalizedRequest, moduleVersion);
awsV2Request[self.REQUEST_SPAN_KEY] = span;
const activeContextWithSpan = trace.setSpan(context.active(), span);
const callbackWithContext = context.bind(activeContextWithSpan, callback);
self._callUserPreRequestHook(span, normalizedRequest);
self._registerV2CompletedEvent(span, awsV2Request, normalizedRequest, activeContextWithSpan);
return context.with(activeContextWithSpan, () => {
self.servicesExtensions.requestPostSpanHook(normalizedRequest);
return self._callOriginalFunction(() => original.call(awsV2Request, callbackWithContext));
});
};
}
private _getRequestPromisePatch(moduleVersion: string, original: () => Promise<any>) {
const self = this;
return function (): Promise<any> {
const awsV2Request: AWS.Request<any, any> = this;
// if the span was already started, we don't want to start a new one when Request.promise() is called
if (this._asm.currentState === 'complete' || awsV2Request[self.REQUEST_SPAN_KEY]) {
return original.apply(this, arguments);
}
const normalizedRequest = normalizeV2Request(awsV2Request);
const requestMetadata = self.servicesExtensions.requestPreSpanHook(normalizedRequest);
const span = self._startAwsV2Span(awsV2Request, requestMetadata, normalizedRequest, moduleVersion);
awsV2Request[self.REQUEST_SPAN_KEY] = span;
const activeContextWithSpan = trace.setSpan(context.active(), span);
self._callUserPreRequestHook(span, normalizedRequest);
self._registerV2CompletedEvent(span, awsV2Request, normalizedRequest, activeContextWithSpan);
const origPromise: Promise<any> = context.with(activeContextWithSpan, () => {
self.servicesExtensions.requestPostSpanHook(normalizedRequest);
return self._callOriginalFunction(() => original.call(awsV2Request, arguments));
});
return requestMetadata.isIncoming ? bindPromise(origPromise, activeContextWithSpan) : origPromise;
};
}
private _getSpanName = (serviceIdentifier: string, operation: string) => {
return `aws.${serviceIdentifier ?? 'request'}.${operation}`;
};
private _callOriginalFunction<T>(originalFunction: (...args: any[]) => T): T {
if (this._config?.suppressInternalInstrumentation) {
return context.with(suppressTracing(context.active()), originalFunction);
} else {
return originalFunction();
}
}
}