-
Notifications
You must be signed in to change notification settings - Fork 40
Expand file tree
/
Copy pathwrapper.js
More file actions
590 lines (519 loc) · 21.2 KB
/
wrapper.js
File metadata and controls
590 lines (519 loc) · 21.2 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
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
/*
* (c) Copyright IBM Corp. 2021
* (c) Copyright Instana Inc. and contributors 2019
*/
'use strict';
// eslint-disable-next-line instana/no-unsafe-require
const semver = require('semver');
const instanaCore = require('@instana/core');
const { backendConnector, consoleLogger: serverlessLogger, environment } = require('@instana/serverless');
const arnParser = require('./arn');
const identityProvider = require('./identity_provider');
const metrics = require('./metrics');
const ssm = require('./ssm');
const triggers = require('./triggers');
const processResult = require('./process_result');
const captureHeaders = require('./capture_headers');
const { tracing, coreConfig } = instanaCore;
const { tracingHeaders, constants, spanBuffer } = tracing;
const lambdaConfigDefaults = {
// Preload OpenTelemetry deps to avoid lazy loading overhead
// See https://jsw.ibm.com/browse/INSTA-71262
preloadOpentelemetry: true,
tracing: {
forceTransmissionStartingAt: 25,
transmissionDelay: 100,
initialTransmissionDelay: 100
}
};
// Node.js 24+ removed support for callback-based handlers (3 parameters).
const latestRuntime = semver.gte(process.version, '24.0.0');
const logger = serverlessLogger.init();
coreConfig.init(logger);
let config = coreConfig.normalize({ defaultsOverride: lambdaConfigDefaults });
let coldStart = true;
// Initialize instrumentations early to allow for require statements after our
// package has been required but before the actual instana.wrap(...) call.
instanaCore.preInit(config);
/**
* Wraps an AWS Lambda handler so that metrics and traces are reported to Instana. This function will figure out if the
* Lambda handler uses the callback style or promise/async function style by inspecting the number of function arguments
* the function receives.
*/
exports.wrap = function wrap(_config, originalHandler) {
/* eslint-disable no-unused-vars */
if (arguments.length === 1) {
originalHandler = _config;
_config = null;
}
switch (originalHandler.length) {
case 0:
return function handler0() {
return shimmedHandler(originalHandler, this, arguments, _config);
};
case 1:
return function handler1(event) {
return shimmedHandler(originalHandler, this, arguments, _config);
};
case 2:
return function handler2(event, context) {
return shimmedHandler(originalHandler, this, arguments, _config);
};
default: {
if (latestRuntime) {
// Required for Node.js 24+: callback is not allowed
return function handlerAsync(event, context) {
return shimmedHandler(originalHandler, this, arguments, _config);
};
}
// For Node.js < 24, allow callback-based handlers
return function handlerCallback(event, context, callback) {
return shimmedHandler(originalHandler, this, arguments, _config);
};
}
}
};
function shimmedHandler(originalHandler, originalThis, originalArgs, _config) {
const event = originalArgs[0];
const context = originalArgs[1];
const lambdaCallback = originalArgs[2];
// For Node.js 24+, if handler expects callback but runtime doesn't provide one,
// skip wrapping and return handler directly
const handlerExpectsCallback = originalHandler?.length >= 3;
if (latestRuntime && handlerExpectsCallback && !lambdaCallback) {
// eslint-disable-next-line no-console
logger.warn(
`Callback-based Lambda handlers are not supported in Node.js ${process.version}. ` +
'Skipping Instana instrumentation. Please migrate to async/await or promise-based handlers.'
);
return originalHandler.apply(originalThis, originalArgs);
}
const arnInfo = arnParser(context);
const tracingEnabled = init(event, arnInfo, _config);
if (!tracingEnabled) {
return originalHandler.apply(originalThis, originalArgs);
}
// The AWS lambda runtime does not seem to inspect the number of arguments the handler function expects. Instead, it
// always call the handler with three arguments (event, context, callback), no matter if the handler will use the
// callback or not. If the handler returns a promise, the runtime uses the that promise's value when it resolves as
// the result. If the handler calls the callback, that value is used as the result. If the handler does both
// (return a promise and resolve it _and_ call the callback), it depends on the timing. Whichever happens first
// dictates the result of the lambda invocation, the later result is ignored. To match this behaviour, we always
// wrap the given callback _and_ return an instrumented promise.
//
// Note: In Node.js 24+, the runtime only passes 2 parameters (event, context) and doesn't provide a callback.
let handlerHasFinished = false;
return tracing.getCls().ns.runPromiseOrRunAndReturn(() => {
const traceCorrelationData = triggers.readTraceCorrelationData(event, context);
const tracingSuppressed = traceCorrelationData.level === '0';
const w3cTraceContext = traceCorrelationData.w3cTraceContext;
let entrySpan;
if (w3cTraceContext) {
// Ususally we commit the W3C trace context to CLS in start span, but in some cases (e.g. when suppressed),
// we don't call startSpan, so we write to CLS here unconditionally. If we also write an updated trace context
// later, the one written here will be overwritten.
tracing.getCls().setW3cTraceContext(w3cTraceContext);
}
if (tracingSuppressed) {
tracing.getCls().setTracingLevel('0');
if (w3cTraceContext) {
w3cTraceContext.disableSampling();
}
} else {
entrySpan = tracing.getCls().startSpan({
spanName: 'aws.lambda.entry',
kind: constants.ENTRY,
traceId: traceCorrelationData.traceId,
parentSpanId: traceCorrelationData.parentId,
w3cTraceContext: w3cTraceContext
});
tracingHeaders.setSpanAttributes(entrySpan, traceCorrelationData);
const { arn, alias } = arnInfo;
entrySpan.data.lambda = {
arn,
alias,
runtime: 'nodejs',
functionName: context.functionName,
functionVersion: context.functionVersion,
reqId: context.awsRequestId
};
if (coldStart) {
entrySpan.data.lambda.coldStart = true;
coldStart = false;
}
triggers.enrichSpanWithTriggerData(event, context, entrySpan);
}
originalArgs[2] = function wrapper(originalError, originalResult) {
if (handlerHasFinished) {
lambdaCallback(originalError, originalResult);
return;
}
handlerHasFinished = true;
postHandler(entrySpan, originalError, originalResult, () => {
lambdaCallback(originalError, originalResult);
});
};
// The functions context.done, context.succeed, and context.fail constitute a deprecated legacy Lambda API from the
// very first incarnations of the Node.js Lambda execution environment from ca. 2016. Although it is not documented
// anymore in the AWS Lambda docs, it still works (and is also used by some customers). See
// eslint-disable-next-line max-len
// https://web.archive.org/web/20161216092320/https://docs.aws.amazon.com/lambda/latest/dg/nodejs-prog-model-using-old-runtime.html
// for information about it.
const originalDone = context.done;
context.done = (originalError, originalResult) => {
if (handlerHasFinished) {
originalDone(originalError, originalResult);
return;
}
handlerHasFinished = true;
postHandler(entrySpan, originalError, originalResult, () => {
originalDone(originalError, originalResult);
});
};
const originalSucceed = context.succeed;
context.succeed = originalResult => {
if (handlerHasFinished) {
originalSucceed(originalResult);
return;
}
handlerHasFinished = true;
postHandler(entrySpan, undefined, originalResult, () => {
originalSucceed(originalResult);
});
};
const originalFail = context.fail;
context.fail = originalError => {
if (handlerHasFinished) {
originalFail(originalError);
return;
}
handlerHasFinished = true;
postHandler(entrySpan, originalError, undefined, () => {
originalFail(originalError);
});
};
/**
* We offer the customer to enable the timeout detection
* But its not recommended to use it on production, only for debugging purposes.
* See https://github.com/instana/nodejs/pull/668.
*/
if (
process.env.INSTANA_ENABLE_LAMBDA_TIMEOUT_DETECTION &&
process.env.INSTANA_ENABLE_LAMBDA_TIMEOUT_DETECTION === 'true'
) {
logger.debug('Heuristical timeout detection enabled. Please only use for debugging purposes.');
registerTimeoutDetection(context, entrySpan);
}
let handlerPromise;
try {
handlerPromise = originalHandler.apply(originalThis, originalArgs);
if (handlerPromise && typeof handlerPromise.then === 'function') {
return handlerPromise.then(
value => {
if (handlerHasFinished) {
return Promise.resolve(value);
}
handlerHasFinished = true;
return postPromise(entrySpan, null, value);
},
error => {
if (handlerHasFinished) {
return Promise.reject(error);
}
handlerHasFinished = true;
return postPromise(entrySpan, error);
}
);
} else {
return handlerPromise;
}
} catch (e) {
// A synchronous exception occured in the original handler.
handlerHasFinished = true;
// eslint-disable-next-line no-console
console.error(
// eslint-disable-next-line max-len
'Your Lambda handler threw a synchronous exception. To report this call (including the error) to Instana, we need to convert this synchronous failure into an asynchronous failure.',
e
);
postHandler(entrySpan, e, undefined, () => {
// rethrow original exception
throw e;
});
return handlerPromise;
}
});
}
/**
* Initialize the wrapper.
*/
function init(event, arnInfo, _config) {
const userConfig = _config || {};
// CASE: customer provides a custom logger or custom level
if (userConfig.logger || userConfig.level) {
serverlessLogger.init(userConfig);
}
// NOTE: We SHOULD renormalize because of:
// - in-code _config object
// - late env variables (less likely)
// - custom logger
// - we always renormalize unconditionally to ensure safety.
config = coreConfig.normalize({ userConfig, defaultsOverride: lambdaConfigDefaults });
if (!config.tracing.enabled) {
return false;
}
const useLambdaExtension = shouldUseLambdaExtension();
if (useLambdaExtension) {
logger.info('@instana/aws-lambda will use the Instana Lambda extension to send data to the Instana back end.');
} else {
logger.info(
'@instana/aws-lambda will not use the Instana Lambda extension, but instead send data to the Instana back end ' +
'directly.'
);
}
identityProvider.init(arnInfo);
triggers.init(config);
backendConnector.init({
config,
identityProvider,
defaultTimeout: 500,
useLambdaExtension,
isLambdaRequest: true,
// NOTE: We only retry for the extension, because if the extenion is not used, the time to transmit
// the data to the serverless acceptor directly takes too long.
retries: !!useLambdaExtension
});
instanaCore.init(config, backendConnector, identityProvider);
// After core init, because ssm requires require('@aws-sdk/client-ssm'), which triggers
// the requireHook + shimmer. Any module which requires another external module has to be
// initialized after the core.
ssm.init(config, coldStart);
spanBuffer.setIsFaaS(true);
captureHeaders.init(config);
metrics.init(config);
metrics.activate();
tracing.activate();
return true;
}
function registerTimeoutDetection(context, entrySpan) {
// We register the timeout detection directly at the start so getRemainingTimeInMillis basically gives us the
// configured timeout for this Lambda function, minus roughly 50 - 100 ms that is spent in bootstrapping.
const initialRemainingMillis = getRemainingTimeInMillis(context);
if (typeof initialRemainingMillis !== 'number') {
return;
}
const minimumTimeoutInMs = process.env.INSTANA_MINIMUM_LAMBDA_TIMEOUT_FOR_TIMEOUT_DETECTION_IN_MS
? Number(process.env.INSTANA_MINIMUM_LAMBDA_TIMEOUT_FOR_TIMEOUT_DETECTION_IN_MS)
: 2000;
if (initialRemainingMillis <= minimumTimeoutInMs) {
logger.debug(
'Heuristical timeout detection will be disabled for Lambda functions with a short timeout ' +
'(2 seconds and smaller).'
);
return;
}
let triggerTimeoutHandlingAfter;
if (initialRemainingMillis <= 4000) {
// For Lambdas configured with a timeout of 3 or 4 seconds we heuristically assume a timeout when only
// 10% of time is remaining.
triggerTimeoutHandlingAfter = initialRemainingMillis * 0.9;
} else {
// For Lambdas configured with a timeout of 5 seconds or more we heuristically assume a timeout when only 400 ms of
// time are remaining.
triggerTimeoutHandlingAfter = initialRemainingMillis - 400;
}
logger.debug(
`Registering heuristical timeout detection to be triggered in ${triggerTimeoutHandlingAfter} milliseconds.`
);
setTimeout(() => {
postHandlerForTimeout(entrySpan, getRemainingTimeInMillis(context));
}, triggerTimeoutHandlingAfter).unref();
}
function getRemainingTimeInMillis(context) {
if (context && typeof context.getRemainingTimeInMillis === 'function') {
return context.getRemainingTimeInMillis();
} else {
logger.warn('context.getRemainingTimeInMillis() is not available, timeout detection will be disabled.');
return null;
}
}
// NOTE: This function only "guesses" whether the Lambda extension should be used or not.
// TODO: Figure out how we can reliably determine whether the Lambda extension should be
// used or not e.g. by checking the lambda handler name if that is possible.
function shouldUseLambdaExtension() {
if (process.env.INSTANA_DISABLE_LAMBDA_EXTENSION) {
logger.info('INSTANA_DISABLE_LAMBDA_EXTENSION is set, not using the Lambda extension.');
return false;
} else {
// Note: We could also use context.memoryLimitInMB here instead of the env var AWS_LAMBDA_FUNCTION_MEMORY_SIZE (both
// should always yield the same value), but this behaviour needs to be in sync with what the Lambda extension does.
// The context object is not available to the extension, so we prefer the env var over the value from the context.
const memorySetting = process.env.AWS_LAMBDA_FUNCTION_MEMORY_SIZE;
if (!memorySetting) {
logger.debug(
'The environment variable AWS_LAMBDA_FUNCTION_MEMORY_SIZE is not present, cannot determine memory settings.'
);
return true;
}
const memorySize = parseInt(memorySetting, 10);
if (isNaN(memorySize)) {
logger.debug(
`Could not parse the value of the environment variable AWS_LAMBDA_FUNCTION_MEMORY_SIZE: "${memorySetting}", ` +
'cannot determine memory settings, not using the Lambda extension.'
);
return false;
}
if (memorySize < 256) {
let logFn = logger.debug;
// CASE: We try to determine if the customer has the extension installed. We need to put a warning
// because the extension is **not** working and might block the lambda extension when
// its not used correctly e.g. slow startup of extension or waiting for invokes or incoming spans
// from the tracer.
if (process.env._HANDLER?.includes('instana-aws-lambda-auto-wrap')) {
logFn = logger.warn;
}
logFn(
'The Lambda function is configured with less than 256 MB of memory according to the value of ' +
`AWS_LAMBDA_FUNCTION_MEMORY_SIZE: ${memorySetting}. The Lambda extension does ` +
'not work with 256mb reliably with low memory settings. ' +
'As the extension is already running, it might ' +
'block the lambda execution which can result in larger execution times. Please configure at least ' +
'256 MB of memory for your Lambda function.'
);
return false;
}
return true;
}
}
/**
* A wrapper for post handler for promise based Lambdas (including async style Lambdas), to be executed after the
* promise returned by the original handler has completed.
*/
function postPromise(entrySpan, error, value) {
return new Promise((resolve, reject) => {
postHandler(entrySpan, error, value, () => {
if (error) {
reject(error);
} else {
resolve(value);
}
});
});
}
function sendToBackend({ spans, metricsPayload, finalLambdaRequest, callback }) {
const runBackendConnector = () => {
return backendConnector.sendBundle({ spans, metrics: metricsPayload }, finalLambdaRequest, callback);
};
// CASE: Customer uses process.env.INSTANA_AGENT_KEY
if (!ssm.isUsed()) {
return runBackendConnector();
}
return ssm.waitAndGetInstanaKey((err, value) => {
if (err) {
logger.debug(err);
return callback();
}
environment.setInstanaAgentKey(value);
return runBackendConnector();
});
}
/**
* When the original handler has completed, the postHandler will finish the entry span that represents the Lambda
* invocation and makes sure the final batch of data (including the Lambda entry span) is sent to the back end before
* letting the Lambda finish (that is, before letting the AWS Lambda runtime process the next invocation or freeze the
* current process).
*/
function postHandler(entrySpan, error, result, postHandlerDone) {
// entrySpan is null when tracing is suppressed due to X-Instana-L
if (entrySpan) {
if (entrySpan.transmitted) {
// The only possible reason for the entry span to already have been transmitted is when the timeout detection
// kicked in and finished the entry span prematurely. If that happened, we also have already triggered sending
// spans to the back end. We do not need to keep the Lambda waiting for another transmission, so we immediately
// let it finish.
postHandlerDone();
return;
}
if (error) {
entrySpan.ec = 1;
if (error.message) {
if (typeof error.message === 'string') {
entrySpan.data.lambda.error = error.message;
} else {
entrySpan.data.lambda.error = JSON.stringify(error.message);
}
} else {
entrySpan.data.lambda.error = error.toString();
}
}
processResult(result, entrySpan);
entrySpan.d = Date.now() - entrySpan.ts;
entrySpan.transmit();
}
const spans = spanBuffer.getAndResetSpans();
// We want that all upcoming spans are send immediately to the BE.
// Span collection happens all the time, but for AWS Lambda sending spans early via spanBuffer
// is disabled because we cannot use `setTimeout` on AWS Lambda.
// When the Lambda handler finishes we send all spans via `sendBundle`.
// If there is any span collected afterwards (async operations), we send them out
// directly and that's why we set `setTransmitImmediate` to true.
// We need to rework the default behavior via https://jsw.ibm.com/browse/INSTA-13498
spanBuffer.setTransmitImmediate(true);
const metricsData = metrics.gatherData();
const metricsPayload = {
plugins: [{ name: 'com.instana.plugin.aws.lambda', entityId: identityProvider.getEntityId(), data: metricsData }]
};
sendToBackend({
spans,
metricsPayload,
finalLambdaRequest: true,
callback: () => {
// We don't process or care if there is an error returned from the backend connector right now.
postHandlerDone();
}
});
}
/**
* When the timeout heuristic detects an imminent timeout, we finish the entry span prematurely and send it to the
* back end.
*/
function postHandlerForTimeout(entrySpan, remainingMillis) {
/**
* context.getRemainingTimeInMillis(context) can return negative values
* That just means that the lambda was already closed.
* `setTimeout` is not 100% reliable
*/
if (remainingMillis < 200) {
logger.debug('Skipping heuristical timeout detection because lambda timeout exceeded already.');
return;
}
if (entrySpan) {
// CASE: Timeout not needed, we already send the data to the backend successfully
if (entrySpan.transmitted) {
logger.debug('Skipping heuristical timeout detection because BE data was sent already.');
return;
}
entrySpan.ec = 1;
entrySpan.data.lambda.msleft = remainingMillis;
entrySpan.data.lambda.error = `Possible Lambda timeout with only ${remainingMillis} ms left.`;
entrySpan.d = Date.now() - entrySpan.ts;
entrySpan.transmit();
}
logger.debug(`Heuristical timeout detection was triggered with ${remainingMillis} milliseconds left.`);
// deliberately not gathering metrics but only sending spans.
const spans = spanBuffer.getAndResetSpans();
sendToBackend({
spans,
metricsPayload: {},
finalLambdaRequest: true,
callback: () => {}
});
}
exports.currentSpan = function getHandleForCurrentSpan() {
return tracing.getHandleForCurrentSpan();
};
exports.sdk = tracing.sdk;
exports.setLogger = function setLogger(_logger) {
serverlessLogger.init({ logger: _logger });
};
exports.opentracing = tracing.opentracing;