@@ -16,37 +16,38 @@ const layerExtensionHostname = 'localhost';
16
16
const layerExtensionPort = process . env . INSTANA_LAYER_EXTENSION_PORT
17
17
? Number ( process . env . INSTANA_LAYER_EXTENSION_PORT )
18
18
: 7365 ;
19
- let useLambdaExtension = false ;
20
19
21
20
const timeoutEnvVar = 'INSTANA_TIMEOUT' ;
22
- let defaultTimeout = 500 ;
23
21
const layerExtensionTimeout = process . env . INSTANA_LAMBDA_EXTENSION_TIMEOUT_IN_MS
24
22
? Number ( process . env . INSTANA_LAMBDA_EXTENSION_TIMEOUT_IN_MS )
25
23
: 500 ;
26
- let backendTimeout = defaultTimeout ;
27
24
28
25
const proxyEnvVar = 'INSTANA_ENDPOINT_PROXY' ;
29
26
let proxyAgent ;
30
27
31
- let stopSendingOnFailure = true ;
32
- let propagateErrorsUpstream = false ;
28
+ const disableCaCheckEnvVar = 'INSTANA_DISABLE_CA_CHECK' ;
29
+ const disableCaCheck = process . env [ disableCaCheckEnvVar ] === 'true' ;
30
+
33
31
let requestHasFailed = false ;
34
32
let warningsHaveBeenLogged = false ;
35
33
36
- const disableCaCheckEnvVar = 'INSTANA_DISABLE_CA_CHECK' ;
37
- const disableCaCheck = process . env [ disableCaCheckEnvVar ] === 'true' ;
34
+ const defaults = {
35
+ config : { } ,
36
+ identityProvider : null ,
37
+ stopSendingOnFailure : true ,
38
+ propagateErrorsUpstream : false ,
39
+ defaultTimeout : 500 ,
40
+ backendTimeout : 500 ,
41
+ useLambdaExtension : false
42
+ } ;
38
43
44
+ let options ;
39
45
let hostHeader ;
40
46
41
- exports . init = function init (
42
- config ,
43
- identityProvider ,
44
- _stopSendingOnFailure ,
45
- _propagateErrorsUpstream ,
46
- _defaultTimeout ,
47
- _useLambdaExtension
48
- ) {
49
- logger = config . logger ;
47
+ exports . init = function init ( opts ) {
48
+ options = Object . assign ( defaults , opts ) ;
49
+
50
+ logger = options . config . logger ;
50
51
51
52
if ( process . env [ proxyEnvVar ] && ! environmentUtil . sendUnencrypted ) {
52
53
const proxyUrl = process . env [ proxyEnvVar ] ;
@@ -65,39 +66,33 @@ exports.init = function init(
65
66
) ;
66
67
}
67
68
68
- stopSendingOnFailure = _stopSendingOnFailure == null ? true : _stopSendingOnFailure ;
69
- propagateErrorsUpstream = _propagateErrorsUpstream == null ? false : _propagateErrorsUpstream ;
70
- defaultTimeout = _defaultTimeout == null ? defaultTimeout : _defaultTimeout ;
71
- useLambdaExtension = _useLambdaExtension ;
72
- backendTimeout = defaultTimeout ;
73
-
74
69
if ( process . env [ timeoutEnvVar ] ) {
75
- backendTimeout = parseInt ( process . env [ timeoutEnvVar ] , 10 ) ;
76
- if ( isNaN ( backendTimeout ) || backendTimeout < 0 ) {
70
+ options . backendTimeout = parseInt ( process . env [ timeoutEnvVar ] , 10 ) ;
71
+
72
+ if ( isNaN ( options . backendTimeout ) || options . backendTimeout < 0 ) {
77
73
logger . warn (
78
74
`The value of ${ timeoutEnvVar } (${ process . env [ timeoutEnvVar ] } ) cannot be parsed to a valid numerical value. ` +
79
- `Will fall back to the default timeout (${ defaultTimeout } ms).`
75
+ `Will fall back to the default timeout (${ options . defaultTimeout } ms).`
80
76
) ;
81
- backendTimeout = defaultTimeout ;
77
+
78
+ options . backendTimeout = 500 ;
82
79
}
83
80
}
84
81
85
- if ( identityProvider ) {
86
- hostHeader = identityProvider . getHostHeader ( ) ;
82
+ if ( options . identityProvider ) {
83
+ hostHeader = options . identityProvider . getHostHeader ( ) ;
87
84
if ( hostHeader == null ) {
88
85
hostHeader = 'nodejs-serverless' ;
89
86
}
90
87
} else {
91
88
hostHeader = 'nodejs-serverless' ;
92
89
}
93
90
94
- requestHasFailed = false ;
95
-
96
91
// Heartbeat is only for the AWS Lambda extension
97
92
// IMPORTANT: the @instana /aws-lambda package will not
98
93
// send data once. It can happen all the time till the Lambda handler dies!
99
94
// SpanBuffer sends data asap and when the handler is finished the rest is sent.
100
- if ( useLambdaExtension ) {
95
+ if ( options . useLambdaExtension ) {
101
96
scheduleLambdaExtensionHeartbeatRequest ( ) ;
102
97
}
103
98
} ;
@@ -178,7 +173,7 @@ function scheduleLambdaExtensionHeartbeatRequest() {
178
173
179
174
function handleHeartbeatError ( e ) {
180
175
// Make sure we do not try to talk to the Lambda extension again.
181
- useLambdaExtension = false ;
176
+ options . useLambdaExtension = false ;
182
177
clearInterval ( heartbeatInterval ) ;
183
178
184
179
logger . debug (
@@ -231,7 +226,7 @@ function getTransport(localUseLambdaExtension) {
231
226
}
232
227
233
228
function getBackendTimeout ( localUseLambdaExtension ) {
234
- return localUseLambdaExtension ? layerExtensionTimeout : backendTimeout ;
229
+ return localUseLambdaExtension ? layerExtensionTimeout : options . backendTimeout ;
235
230
}
236
231
237
232
function send ( resourcePath , payload , finalLambdaRequest , callback ) {
@@ -245,9 +240,9 @@ function send(resourcePath, payload, finalLambdaRequest, callback) {
245
240
// We need a local copy of the global useLambdaExtension variable, otherwise it might be changed concurrently by
246
241
// scheduleLambdaExtensionHeartbeatRequest. But we need to remember the value at the time we _started_ the request to
247
242
// decide whether to fall back to sending to the back end directly or give up sending data completely.
248
- let localUseLambdaExtension = useLambdaExtension ;
243
+ let localUseLambdaExtension = options . useLambdaExtension ;
249
244
250
- if ( requestHasFailed && stopSendingOnFailure ) {
245
+ if ( requestHasFailed && options . stopSendingOnFailure ) {
251
246
logger . info (
252
247
`Not attempting to send data to ${ resourcePath } as a previous request has already timed out or failed.`
253
248
) ;
@@ -286,7 +281,7 @@ function send(resourcePath, payload, finalLambdaRequest, callback) {
286
281
// serialize the payload object
287
282
const serializedPayload = JSON . stringify ( payload ) ;
288
283
289
- const options = {
284
+ const reqOptions = {
290
285
hostname : localUseLambdaExtension ? layerExtensionHostname : environmentUtil . getBackendHost ( ) ,
291
286
port : localUseLambdaExtension ? layerExtensionPort : environmentUtil . getBackendPort ( ) ,
292
287
path : requestPath ,
@@ -300,10 +295,10 @@ function send(resourcePath, payload, finalLambdaRequest, callback) {
300
295
rejectUnauthorized : ! disableCaCheck
301
296
} ;
302
297
303
- options . timeout = getBackendTimeout ( localUseLambdaExtension ) ;
298
+ reqOptions . timeout = getBackendTimeout ( localUseLambdaExtension ) ;
304
299
305
300
if ( proxyAgent && ! localUseLambdaExtension ) {
306
- options . agent = proxyAgent ;
301
+ reqOptions . agent = proxyAgent ;
307
302
}
308
303
309
304
let req ;
@@ -319,7 +314,7 @@ function send(resourcePath, payload, finalLambdaRequest, callback) {
319
314
// report metrics and traces quite a bit. The (acceptable) downside is that we do not get to examine the response
320
315
// for HTTP status codes.
321
316
322
- req = transport . request ( options ) ;
317
+ req = transport . request ( reqOptions ) ;
323
318
} else {
324
319
// If (a) our Lambda extension is available, or if (b) a user-provided proxy is in use, we do *not* apply the
325
320
// optimization outlined above. Instead, we opt for the more traditional workflow of waiting until the HTTP response
@@ -329,7 +324,7 @@ function send(resourcePath, payload, finalLambdaRequest, callback) {
329
324
//
330
325
// See the req.end(serializedPayload) call below, too. In the no-extension/no-proxy case, that call has the callback
331
326
// to end the processing. Otherwise, the callback is provided here to http.request().
332
- req = transport . request ( options , ( ) => {
327
+ req = transport . request ( reqOptions , ( ) => {
333
328
// When the Node.js process is frozen while the request is pending, and then thawed later,
334
329
// this can trigger a stale, bogus timeout event (because from the perspective of the freshly thawed Node.js
335
330
// runtime, the request has been pending and inactive since a long time). To avoid that, we remove all listeners
@@ -383,7 +378,7 @@ function send(resourcePath, payload, finalLambdaRequest, callback) {
383
378
) ;
384
379
385
380
// Make sure we do not try to talk to the Lambda extension again.
386
- useLambdaExtension = localUseLambdaExtension = false ;
381
+ options . useLambdaExtension = localUseLambdaExtension = false ;
387
382
clearInterval ( heartbeatInterval ) ;
388
383
389
384
// Retry the request immediately, this time sending it to serverless-acceptor directly.
@@ -394,7 +389,7 @@ function send(resourcePath, payload, finalLambdaRequest, callback) {
394
389
// (or a user-provided proxy).
395
390
requestHasFailed = true ;
396
391
397
- if ( ! propagateErrorsUpstream ) {
392
+ if ( ! options . propagateErrorsUpstream ) {
398
393
if ( proxyAgent ) {
399
394
logger . warn (
400
395
'Could not send traces and metrics to Instana. Could not connect to the configured proxy ' +
@@ -409,14 +404,14 @@ function send(resourcePath, payload, finalLambdaRequest, callback) {
409
404
}
410
405
}
411
406
412
- handleCallback ( propagateErrorsUpstream ? e : undefined ) ;
407
+ handleCallback ( options . propagateErrorsUpstream ? e : undefined ) ;
413
408
}
414
409
} ) ;
415
410
416
411
req . on ( 'finish' , ( ) => {
417
412
logger . debug ( `Sent data to Instana (${ requestPath } ).` ) ;
418
413
419
- if ( useLambdaExtension && finalLambdaRequest ) {
414
+ if ( options . useLambdaExtension && finalLambdaRequest ) {
420
415
clearInterval ( heartbeatInterval ) ;
421
416
}
422
417
} ) ;
@@ -465,7 +460,7 @@ function onTimeout(localUseLambdaExtension, req, resourcePath, payload, finalLam
465
460
) ;
466
461
467
462
// Make sure we do not try to talk to the Lambda extension again.
468
- useLambdaExtension = localUseLambdaExtension = false ;
463
+ options . useLambdaExtension = localUseLambdaExtension = false ;
469
464
clearInterval ( heartbeatInterval ) ;
470
465
471
466
if ( req && ! req . destroyed ) {
@@ -498,14 +493,16 @@ function onTimeout(localUseLambdaExtension, req, resourcePath, payload, finalLam
498
493
}
499
494
500
495
const message =
501
- 'Could not send traces and metrics to Instana. The Instana back end did not respond in the configured timeout ' +
502
- `of ${ backendTimeout } ms. The timeout can be configured by setting the environment variable ${ timeoutEnvVar } .` ;
496
+ 'Could not send traces and metrics to Instana. The Instana back end did not respond ' +
497
+ 'in the configured timeout ' +
498
+ `of ${ options . backendTimeout } ms. The timeout can be configured by ` +
499
+ `setting the environment variable ${ timeoutEnvVar } .` ;
503
500
504
- if ( ! propagateErrorsUpstream ) {
501
+ if ( ! options . propagateErrorsUpstream ) {
505
502
logger . warn ( message ) ;
506
503
}
507
504
508
- handleCallback ( propagateErrorsUpstream ? new Error ( message ) : undefined ) ;
505
+ handleCallback ( options . propagateErrorsUpstream ? new Error ( message ) : undefined ) ;
509
506
}
510
507
}
511
508
0 commit comments