-
Notifications
You must be signed in to change notification settings - Fork 538
/
instrumentation.ts
499 lines (449 loc) · 15 KB
/
instrumentation.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
/*
* Copyright The OpenTelemetry Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import * as path from 'path';
import * as fs from 'fs';
import {
InstrumentationBase,
InstrumentationNodeModuleDefinition,
InstrumentationNodeModuleFile,
isWrapped,
safeExecuteInTheMiddle,
} from '@opentelemetry/instrumentation';
import {
Context as OtelContext,
context as otelContext,
diag,
trace,
propagation,
MeterProvider,
Span,
SpanKind,
SpanStatusCode,
TextMapGetter,
TracerProvider,
ROOT_CONTEXT,
Attributes,
} from '@opentelemetry/api';
import {
ATTR_URL_FULL,
SEMATTRS_FAAS_EXECUTION,
SEMRESATTRS_CLOUD_ACCOUNT_ID,
SEMRESATTRS_FAAS_ID,
} from '@opentelemetry/semantic-conventions';
import { ATTR_FAAS_COLDSTART } from '@opentelemetry/semantic-conventions/incubating';
import {
APIGatewayProxyEventHeaders,
Callback,
Context,
Handler,
} from 'aws-lambda';
import { AwsLambdaInstrumentationConfig, EventContextExtractor } from './types';
/** @knipignore */
import { PACKAGE_NAME, PACKAGE_VERSION } from './version';
import { LambdaModule } from './internal-types';
const headerGetter: TextMapGetter<APIGatewayProxyEventHeaders> = {
keys(carrier): string[] {
return Object.keys(carrier);
},
get(carrier, key: string) {
return carrier[key];
},
};
export const lambdaMaxInitInMilliseconds = 10_000;
export class AwsLambdaInstrumentation extends InstrumentationBase<AwsLambdaInstrumentationConfig> {
private _traceForceFlusher?: () => Promise<void>;
private _metricForceFlusher?: () => Promise<void>;
constructor(config: AwsLambdaInstrumentationConfig = {}) {
super(PACKAGE_NAME, PACKAGE_VERSION, config);
}
init() {
const taskRoot = process.env.LAMBDA_TASK_ROOT;
const handlerDef = this.getConfig().lambdaHandler ?? process.env._HANDLER;
// _HANDLER and LAMBDA_TASK_ROOT are always defined in Lambda but guard bail out if in the future this changes.
if (!taskRoot || !handlerDef) {
this._diag.debug(
'Skipping lambda instrumentation: no _HANDLER/lambdaHandler or LAMBDA_TASK_ROOT.',
{ taskRoot, handlerDef }
);
return [];
}
const handler = path.basename(handlerDef);
const moduleRoot = handlerDef.substring(
0,
handlerDef.length - handler.length
);
const [module, functionName] = handler.split('.', 2);
// Lambda loads user function using an absolute path.
let filename = path.resolve(taskRoot, moduleRoot, module);
if (!filename.endsWith('.js')) {
// It's impossible to know in advance if the user has a js, mjs or cjs file.
// Check that the .js file exists otherwise fallback to the next known possibilities (.mjs, .cjs).
try {
fs.statSync(`${filename}.js`);
filename += '.js';
} catch (e) {
try {
fs.statSync(`${filename}.mjs`);
// fallback to .mjs (ESM)
filename += '.mjs';
} catch (e2) {
try {
fs.statSync(`${filename}.cjs`);
// fallback to .cjs (CommonJS)
filename += '.cjs';
} catch (e3) {
this._diag.warn(
'No handler file was able to resolved with one of the known extensions for the file',
filename
);
}
}
}
}
diag.debug('Instrumenting lambda handler', {
taskRoot,
handlerDef,
handler,
moduleRoot,
module,
filename,
functionName,
});
const lambdaStartTime =
this.getConfig().lambdaStartTime ||
Date.now() - Math.floor(1000 * process.uptime());
return [
new InstrumentationNodeModuleDefinition(
// NB: The patching infrastructure seems to match names backwards, this must be the filename, while
// InstrumentationNodeModuleFile must be the module name.
filename,
['*'],
undefined,
undefined,
[
new InstrumentationNodeModuleFile(
module,
['*'],
(moduleExports: LambdaModule) => {
if (isWrapped(moduleExports[functionName])) {
this._unwrap(moduleExports, functionName);
}
this._wrap(
moduleExports,
functionName,
this._getHandler(lambdaStartTime)
);
return moduleExports;
},
(moduleExports?: LambdaModule) => {
if (moduleExports == null) return;
this._unwrap(moduleExports, functionName);
}
),
]
),
];
}
private _getHandler(handlerLoadStartTime: number) {
return (original: Handler) => {
return this._getPatchHandler(original, handlerLoadStartTime);
};
}
private _getPatchHandler(original: Handler, lambdaStartTime: number) {
diag.debug('patch handler function');
const plugin = this;
let requestHandledBefore = false;
let requestIsColdStart = true;
function _onRequest(): void {
if (requestHandledBefore) {
// Non-first requests cannot be coldstart.
requestIsColdStart = false;
} else {
if (
process.env.AWS_LAMBDA_INITIALIZATION_TYPE ===
'provisioned-concurrency'
) {
// If sandbox environment is initialized with provisioned concurrency,
// even the first requests should not be considered as coldstart.
requestIsColdStart = false;
} else {
// Check whether it is proactive initialization or not:
// https://aaronstuyvenberg.com/posts/understanding-proactive-initialization
const passedTimeSinceHandlerLoad: number =
Date.now() - lambdaStartTime;
const proactiveInitialization: boolean =
passedTimeSinceHandlerLoad > lambdaMaxInitInMilliseconds;
// If sandbox has been initialized proactively before the actual request,
// even the first requests should not be considered as coldstart.
requestIsColdStart = !proactiveInitialization;
}
requestHandledBefore = true;
}
}
return function patchedHandler(
this: never,
// The event can be a user type, it truly is any.
// eslint-disable-next-line @typescript-eslint/no-explicit-any
event: any,
context: Context,
callback: Callback
) {
_onRequest();
const config = plugin.getConfig();
const parent = AwsLambdaInstrumentation._determineParent(
event,
context,
config.eventContextExtractor ||
AwsLambdaInstrumentation._defaultEventContextExtractor
);
const name = context.functionName;
const span = plugin.tracer.startSpan(
name,
{
kind: SpanKind.SERVER,
attributes: {
[SEMATTRS_FAAS_EXECUTION]: context.awsRequestId,
[SEMRESATTRS_FAAS_ID]: context.invokedFunctionArn,
[SEMRESATTRS_CLOUD_ACCOUNT_ID]:
AwsLambdaInstrumentation._extractAccountId(
context.invokedFunctionArn
),
[ATTR_FAAS_COLDSTART]: requestIsColdStart,
...AwsLambdaInstrumentation._extractOtherEventFields(event),
},
},
parent
);
const { requestHook } = config;
if (requestHook) {
safeExecuteInTheMiddle(
() => requestHook(span, { event, context }),
e => {
if (e)
diag.error('aws-lambda instrumentation: requestHook error', e);
},
true
);
}
return otelContext.with(trace.setSpan(parent, span), () => {
// Lambda seems to pass a callback even if handler is of Promise form, so we wrap all the time before calling
// the handler and see if the result is a Promise or not. In such a case, the callback is usually ignored. If
// the handler happened to both call the callback and complete a returned Promise, whichever happens first will
// win and the latter will be ignored.
const wrappedCallback = plugin._wrapCallback(callback, span);
const maybePromise = safeExecuteInTheMiddle(
() => original.apply(this, [event, context, wrappedCallback]),
error => {
if (error != null) {
// Exception thrown synchronously before resolving callback / promise.
plugin._applyResponseHook(span, error);
plugin._endSpan(span, error, () => {});
}
}
) as Promise<{}> | undefined;
if (typeof maybePromise?.then === 'function') {
return maybePromise.then(
value => {
plugin._applyResponseHook(span, null, value);
return new Promise(resolve =>
plugin._endSpan(span, undefined, () => resolve(value))
);
},
(err: Error | string) => {
plugin._applyResponseHook(span, err);
return new Promise((resolve, reject) =>
plugin._endSpan(span, err, () => reject(err))
);
}
);
}
return maybePromise;
});
};
}
override setTracerProvider(tracerProvider: TracerProvider) {
super.setTracerProvider(tracerProvider);
this._traceForceFlusher = this._traceForceFlush(tracerProvider);
}
private _traceForceFlush(tracerProvider: TracerProvider) {
if (!tracerProvider) return undefined;
// eslint-disable-next-line @typescript-eslint/no-explicit-any
let currentProvider: any = tracerProvider;
if (typeof currentProvider.getDelegate === 'function') {
currentProvider = currentProvider.getDelegate();
}
if (typeof currentProvider.forceFlush === 'function') {
return currentProvider.forceFlush.bind(currentProvider);
}
return undefined;
}
override setMeterProvider(meterProvider: MeterProvider) {
super.setMeterProvider(meterProvider);
this._metricForceFlusher = this._metricForceFlush(meterProvider);
}
private _metricForceFlush(meterProvider: MeterProvider) {
if (!meterProvider) return undefined;
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const currentProvider: any = meterProvider;
if (typeof currentProvider.forceFlush === 'function') {
return currentProvider.forceFlush.bind(currentProvider);
}
return undefined;
}
private _wrapCallback(original: Callback, span: Span): Callback {
const plugin = this;
return function wrappedCallback(this: never, err, res) {
diag.debug('executing wrapped lookup callback function');
plugin._applyResponseHook(span, err, res);
plugin._endSpan(span, err, () => {
diag.debug('executing original lookup callback function');
return original.apply(this, [err, res]);
});
};
}
private _endSpan(
span: Span,
err: string | Error | null | undefined,
callback: () => void
) {
if (err) {
span.recordException(err);
}
let errMessage;
if (typeof err === 'string') {
errMessage = err;
} else if (err) {
errMessage = err.message;
}
if (errMessage) {
span.setStatus({
code: SpanStatusCode.ERROR,
message: errMessage,
});
}
span.end();
const flushers = [];
if (this._traceForceFlusher) {
flushers.push(this._traceForceFlusher());
} else {
diag.error(
'Spans may not be exported for the lambda function because we are not force flushing before callback.'
);
}
if (this._metricForceFlusher) {
flushers.push(this._metricForceFlusher());
} else {
diag.error(
'Metrics may not be exported for the lambda function because we are not force flushing before callback.'
);
}
Promise.all(flushers).then(callback, callback);
}
private _applyResponseHook(
span: Span,
err?: Error | string | null,
res?: any
) {
const { responseHook } = this.getConfig();
if (responseHook) {
safeExecuteInTheMiddle(
() => responseHook(span, { err, res }),
e => {
if (e)
diag.error('aws-lambda instrumentation: responseHook error', e);
},
true
);
}
}
private static _extractAccountId(arn: string): string | undefined {
const parts = arn.split(':');
if (parts.length >= 5) {
return parts[4];
}
return undefined;
}
private static _defaultEventContextExtractor(event: any): OtelContext {
// The default extractor tries to get sampled trace header from HTTP headers.
const httpHeaders = event.headers || {};
return propagation.extract(otelContext.active(), httpHeaders, headerGetter);
}
private static _extractOtherEventFields(event: any): Attributes {
const answer: Attributes = {};
const fullUrl = this._extractFullUrl(event);
if (fullUrl) {
answer[ATTR_URL_FULL] = fullUrl;
}
return answer;
}
private static _extractFullUrl(event: any): string | undefined {
// API gateway encodes a lot of url information in various places to recompute this
if (!event.headers) {
return undefined;
}
// Helper function to deal with case variations (instead of making a tolower() copy of the headers)
function findAny(
event: any,
key1: string,
key2: string
): string | undefined {
return event.headers[key1] ?? event.headers[key2];
}
const host = findAny(event, 'host', 'Host');
const proto = findAny(event, 'x-forwarded-proto', 'X-Forwarded-Proto');
const port = findAny(event, 'x-forwarded-port', 'X-Forwarded-Port');
if (!(proto && host && (event.path || event.rawPath))) {
return undefined;
}
let answer = proto + '://' + host;
if (port) {
answer += ':' + port;
}
answer += event.path ?? event.rawPath;
if (event.queryStringParameters) {
let first = true;
for (const key in event.queryStringParameters) {
answer += first ? '?' : '&';
answer += encodeURIComponent(key);
answer += '=';
answer += encodeURIComponent(event.queryStringParameters[key]);
first = false;
}
}
return answer;
}
private static _determineParent(
event: any,
context: Context,
eventContextExtractor: EventContextExtractor
): OtelContext {
const extractedContext = safeExecuteInTheMiddle(
() => eventContextExtractor(event, context),
e => {
if (e)
diag.error(
'aws-lambda instrumentation: eventContextExtractor error',
e
);
},
true
);
if (trace.getSpan(extractedContext)?.spanContext()) {
return extractedContext;
}
return ROOT_CONTEXT;
}
}