Skip to content

Commit 151be34

Browse files
committed
refactor: added instanaCtr dependency injection
1 parent 27e4dd3 commit 151be34

File tree

87 files changed

+953
-643
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

87 files changed

+953
-643
lines changed

packages/aws-fargate/src/activate.js

Lines changed: 35 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -6,25 +6,30 @@
66
'use strict';
77

88
const instanaCore = require('@instana/core');
9-
const { backendConnector, consoleLogger: log } = require('@instana/serverless');
9+
const { backendConnector, consoleLogger: serverlessLogger } = require('@instana/serverless');
1010

1111
const identityProvider = require('./identity_provider');
1212
const metrics = require('./metrics');
1313
const { fullyQualifiedContainerId } = require('./metrics/container/containerUtil');
14+
const { tracing, coreConfig, coreUtils } = instanaCore;
1415

15-
const { tracing, util: coreUtil } = instanaCore;
16-
const { normalizeConfig } = coreUtil;
16+
const instanaCtr = new instanaCore.InstanaCtr();
1717

18-
const logger = log.init();
19-
const config = normalizeConfig({}, logger);
18+
coreUtils.init(instanaCtr);
19+
coreConfig.init(instanaCtr);
20+
serverlessLogger.init(instanaCtr);
21+
22+
instanaCtr.set('utils', coreUtils.create());
23+
instanaCtr.set('config', coreConfig.create());
24+
instanaCtr.set('logger', serverlessLogger.create());
2025

2126
function init() {
22-
instanaCore.preInit(config);
27+
instanaCore.preInit(instanaCtr.config(), instanaCtr.utils());
2328

24-
metrics.init(config, function onReady(err, ecsContainerPayload) {
29+
metrics.init(instanaCtr.config(), function onReady(err, ecsContainerPayload) {
2530
if (err) {
26-
logger.error(
27-
`Initializing @instana/aws-fargate failed. This fargate task will not be monitored.
31+
instanaCtr.logger().error(
32+
`Initializing @instana/aws-fargate failed. This fargate task will not be monitored.
2833
${err?.message} ${err?.stack}`
2934
);
3035
metrics.deactivate();
@@ -34,45 +39,51 @@ function init() {
3439
try {
3540
const taskArn = ecsContainerPayload && ecsContainerPayload.data ? ecsContainerPayload.data.taskArn : null;
3641
if (!taskArn) {
37-
logger.error(
38-
'Initializing @instana/aws-fargate failed, the metadata did not have a task ARN. This fargate task will ' +
39-
'not be monitored.'
40-
);
42+
instanaCtr
43+
.logger()
44+
.error(
45+
'Initializing @instana/aws-fargate failed, the metadata did not have a task ARN. This fargate task will ' +
46+
'not be monitored.'
47+
);
4148
metrics.deactivate();
4249
return;
4350
}
4451
const containerId = ecsContainerPayload.data.containerName
4552
? fullyQualifiedContainerId(taskArn, ecsContainerPayload.data.containerName)
4653
: null;
4754
if (!containerId) {
48-
logger.error(
49-
'Initializing @instana/aws-fargate failed, the metadata did not have a container name. This fargate task ' +
50-
'will not be monitored.'
51-
);
55+
instanaCtr
56+
.logger()
57+
.error(
58+
'Initializing @instana/aws-fargate failed, the metadata did not have a container name. This fargate task ' +
59+
'will not be monitored.'
60+
);
5261
metrics.deactivate();
5362
return;
5463
}
5564

5665
identityProvider.init(taskArn, containerId);
5766

5867
backendConnector.init({
59-
config,
68+
config: instanaCtr.config(),
6069
identityProvider,
6170
defaultTimeout: 950
6271
});
6372

64-
instanaCore.init(config, backendConnector, identityProvider);
73+
instanaCore.init(instanaCtr.config(), instanaCtr.utils(), backendConnector, identityProvider);
6574
metrics.activate(backendConnector);
6675
tracing.activate();
6776

68-
logger.debug('@instana/aws-fargate initialized.');
77+
instanaCtr.logger().debug('@instana/aws-fargate initialized.');
6978

7079
// eslint-disable-next-line no-unused-expressions
7180
process.send && process.send('instana.aws-fargate.initialized');
7281
} catch (e) {
73-
logger.error(
74-
`Initializing @instana/aws-fargate failed. This fargate task will not be monitored. ${e?.message} ${e?.stack}`
75-
);
82+
instanaCtr
83+
.logger()
84+
.error(
85+
`Initializing @instana/aws-fargate failed. This fargate task will not be monitored. ${e?.message} ${e?.stack}`
86+
);
7687
}
7788
});
7889
}
@@ -88,7 +99,7 @@ exports.sdk = tracing.sdk;
8899

89100
// NOTE: this is the external interface for the customer. They can set a custom logger.
90101
exports.setLogger = function setLogger(_logger) {
91-
log.init({ logger: _logger });
102+
instanaCtr.logger().setLogger(_logger);
92103
};
93104

94105
exports.opentracing = tracing.opentracing;

packages/aws-fargate/test/Control.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ const config = require('@instana/core/test/config');
1313
const AbstractServerlessControl = require('../../serverless/test/util/AbstractServerlessControl');
1414
const portfinder = require('../../collector/test/test_util/portfinder');
1515
const PATH_TO_INSTANA_FARGATE_PACKAGE = path.join(__dirname, '..');
16-
const isLatestEsmSupportedVersion = require('@instana/core').util.esm.isLatestEsmSupportedVersion;
16+
const isLatestEsmSupportedVersion = require('@instana/core').coreUtils.esm.isLatestEsmSupportedVersion;
1717
let execArg;
1818

1919
function Control(opts) {

packages/aws-fargate/test/metrics/transmissionCycle_test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ describe('transmission cycle', function () {
3232
let onReadyError;
3333

3434
before(() => {
35-
const config = core.util.normalizeConfig({});
35+
const config = core.config.normalize();
3636
core.secrets.init(config);
3737

3838
const metadataMockPort = portfinder();

packages/aws-lambda/src/wrapper.js

Lines changed: 69 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
'use strict';
77

88
const instanaCore = require('@instana/core');
9-
const { backendConnector, consoleLogger: log, environment } = require('@instana/serverless');
9+
const { backendConnector, consoleLogger: serverlessLogger, environment } = require('@instana/serverless');
1010
const arnParser = require('./arn');
1111
const identityProvider = require('./identity_provider');
1212
const metrics = require('./metrics');
@@ -15,21 +15,29 @@ const triggers = require('./triggers');
1515
const processResult = require('./process_result');
1616
const captureHeaders = require('./capture_headers');
1717

18-
const { tracing, util: coreUtil } = instanaCore;
19-
const { normalizeConfig } = coreUtil;
18+
const { tracing, coreConfig, coreUtils } = instanaCore;
2019
const { tracingHeaders, constants, spanBuffer } = tracing;
2120

2221
const lambdaConfigDefaults = {
2322
tracing: { forceTransmissionStartingAt: 25, transmissionDelay: 100, initialTransmissionDelay: 100 }
2423
};
2524

26-
const logger = log.init();
27-
let config = normalizeConfig({}, logger, lambdaConfigDefaults);
2825
let coldStart = true;
2926

27+
const instanaCtr = new instanaCore.InstanaCtr();
28+
29+
coreUtils.init(instanaCtr);
30+
coreConfig.init(instanaCtr);
31+
serverlessLogger.init(instanaCtr);
32+
33+
instanaCtr.set('utils', coreUtils.create());
34+
instanaCtr.set('config', coreConfig.create({}, lambdaConfigDefaults));
35+
instanaCtr.set('logger', serverlessLogger.create());
36+
3037
// Initialize instrumentations early to allow for require statements after our
3138
// package has been required but before the actual instana.wrap(...) call.
32-
instanaCore.preInit(config);
39+
// TODO: refactor to use `instanaCtr` only!
40+
instanaCore.preInit(instanaCtr.config(), instanaCtr.utils());
3341

3442
/**
3543
* Wraps an AWS Lambda handler so that metrics and traces are reported to Instana. This function will figure out if the
@@ -191,7 +199,7 @@ function shimmedHandler(originalHandler, originalThis, originalArgs, _config) {
191199
process.env.INSTANA_ENABLE_LAMBDA_TIMEOUT_DETECTION &&
192200
process.env.INSTANA_ENABLE_LAMBDA_TIMEOUT_DETECTION === 'true'
193201
) {
194-
logger.debug('Heuristical timeout detection enabled. Please only use for debugging purposes.');
202+
instanaCtr.logger().debug('Heuristical timeout detection enabled. Please only use for debugging purposes.');
195203
registerTimeoutDetection(context, entrySpan);
196204
}
197205

@@ -245,34 +253,41 @@ function init(event, arnInfo, _config) {
245253

246254
// CASE: customer provides a custom logger or custom level
247255
if (customConfig.logger || customConfig.level) {
248-
log.init(customConfig);
256+
instanaCtr.setLogger(serverlessLogger.create(customConfig));
249257
}
250258

251-
// NOTE: We SHOULD renormalize because of:
259+
// NOTE: We SHOULD re-create the config object, because:
252260
// - in-code _config object
253261
// - late env variables (less likely)
254262
// - custom logger
255263
// - we always renormalize unconditionally to ensure safety.
256-
config = normalizeConfig(customConfig, logger, lambdaConfigDefaults);
264+
// This is a consequence of pre-initializing early.
265+
instanaCtr.set('config', coreConfig.create(customConfig, lambdaConfigDefaults));
257266

258-
if (!config.tracing.enabled) {
267+
if (!instanaCtr.config().tracing.enabled) {
259268
return false;
260269
}
261270

262271
const useLambdaExtension = shouldUseLambdaExtension();
263272
if (useLambdaExtension) {
264-
logger.info('@instana/aws-lambda will use the Instana Lambda extension to send data to the Instana back end.');
273+
instanaCtr
274+
.logger()
275+
.info('@instana/aws-lambda will use the Instana Lambda extension to send data to the Instana back end.');
265276
} else {
266-
logger.info(
267-
'@instana/aws-lambda will not use the Instana Lambda extension, but instead send data to the Instana back end ' +
268-
'directly.'
269-
);
277+
instanaCtr
278+
.logger()
279+
.info(
280+
'@instana/aws-lambda will not use the Instana Lambda extension, ' +
281+
'but instead send data to the Instana back end ' +
282+
'directly.'
283+
);
270284
}
271285

272286
identityProvider.init(arnInfo);
273-
triggers.init(config);
287+
triggers.init(instanaCtr.config());
288+
274289
backendConnector.init({
275-
config,
290+
config: instanaCtr.config(),
276291
identityProvider,
277292
defaultTimeout: 500,
278293
useLambdaExtension,
@@ -282,16 +297,16 @@ function init(event, arnInfo, _config) {
282297
retries: !!useLambdaExtension
283298
});
284299

285-
instanaCore.init(config, backendConnector, identityProvider);
300+
instanaCore.init(instanaCtr.config(), instanaCtr.utils(), backendConnector, identityProvider);
286301

287302
// After core init, because ssm requires require('@aws-sdk/client-ssm'), which triggers
288303
// the requireHook + shimmer. Any module which requires another external module has to be
289304
// initialized after the core.
290-
ssm.init(config);
305+
ssm.init(instanaCtr.config());
291306

292307
spanBuffer.setIsFaaS(true);
293-
captureHeaders.init(config);
294-
metrics.init(config);
308+
captureHeaders.init(instanaCtr.config());
309+
metrics.init(instanaCtr.config());
295310
metrics.activate();
296311
tracing.activate();
297312

@@ -311,10 +326,12 @@ function registerTimeoutDetection(context, entrySpan) {
311326
: 2000;
312327

313328
if (initialRemainingMillis <= minimumTimeoutInMs) {
314-
logger.debug(
315-
'Heuristical timeout detection will be disabled for Lambda functions with a short timeout ' +
316-
'(2 seconds and smaller).'
317-
);
329+
instanaCtr
330+
.logger()
331+
.debug(
332+
'Heuristical timeout detection will be disabled for Lambda functions with a short timeout ' +
333+
'(2 seconds and smaller).'
334+
);
318335
return;
319336
}
320337

@@ -329,9 +346,9 @@ function registerTimeoutDetection(context, entrySpan) {
329346
triggerTimeoutHandlingAfter = initialRemainingMillis - 400;
330347
}
331348

332-
logger.debug(
333-
`Registering heuristical timeout detection to be triggered in ${triggerTimeoutHandlingAfter} milliseconds.`
334-
);
349+
instanaCtr
350+
.logger()
351+
.debug(`Registering heuristical timeout detection to be triggered in ${triggerTimeoutHandlingAfter} milliseconds.`);
335352

336353
setTimeout(() => {
337354
postHandlerForTimeout(entrySpan, getRemainingTimeInMillis(context));
@@ -342,7 +359,9 @@ function getRemainingTimeInMillis(context) {
342359
if (context && typeof context.getRemainingTimeInMillis === 'function') {
343360
return context.getRemainingTimeInMillis();
344361
} else {
345-
logger.warn('context.getRemainingTimeInMillis() is not available, timeout detection will be disabled.');
362+
instanaCtr
363+
.logger()
364+
.warn('context.getRemainingTimeInMillis() is not available, timeout detection will be disabled.');
346365
return null;
347366
}
348367
}
@@ -352,36 +371,41 @@ function getRemainingTimeInMillis(context) {
352371
// used or not e.g. by checking the lambda handler name if that is possible.
353372
function shouldUseLambdaExtension() {
354373
if (process.env.INSTANA_DISABLE_LAMBDA_EXTENSION) {
355-
logger.info('INSTANA_DISABLE_LAMBDA_EXTENSION is set, not using the Lambda extension.');
374+
instanaCtr.logger().info('INSTANA_DISABLE_LAMBDA_EXTENSION is set, not using the Lambda extension.');
356375
return false;
357376
} else {
358377
// Note: We could also use context.memoryLimitInMB here instead of the env var AWS_LAMBDA_FUNCTION_MEMORY_SIZE (both
359378
// should always yield the same value), but this behaviour needs to be in sync with what the Lambda extension does.
360379
// The context object is not available to the extension, so we prefer the env var over the value from the context.
361380
const memorySetting = process.env.AWS_LAMBDA_FUNCTION_MEMORY_SIZE;
362381
if (!memorySetting) {
363-
logger.debug(
364-
'The environment variable AWS_LAMBDA_FUNCTION_MEMORY_SIZE is not present, cannot determine memory settings.'
365-
);
382+
instanaCtr
383+
.logger()
384+
.debug(
385+
'The environment variable AWS_LAMBDA_FUNCTION_MEMORY_SIZE is not present, cannot determine memory settings.'
386+
);
366387
return true;
367388
}
368389
const memorySize = parseInt(memorySetting, 10);
369390
if (isNaN(memorySize)) {
370-
logger.debug(
371-
`Could not parse the value of the environment variable AWS_LAMBDA_FUNCTION_MEMORY_SIZE: "${memorySetting}", ` +
372-
'cannot determine memory settings, not using the Lambda extension.'
373-
);
391+
instanaCtr
392+
.logger()
393+
.debug(
394+
'Could not parse the value of the environment variable ' +
395+
`AWS_LAMBDA_FUNCTION_MEMORY_SIZE: "${memorySetting}", ` +
396+
'cannot determine memory settings, not using the Lambda extension.'
397+
);
374398
return false;
375399
}
376400
if (memorySize < 256) {
377-
let logFn = logger.debug;
401+
let logFn = instanaCtr.logger().debug;
378402

379403
// CASE: We try to determine if the customer has the extension installed. We need to put a warning
380404
// because the extension is **not** working and might block the lambda extension when
381405
// its not used correctly e.g. slow startup of extension or waiting for invokes or incoming spans
382406
// from the tracer.
383407
if (process.env._HANDLER?.includes('instana-aws-lambda-auto-wrap')) {
384-
logFn = logger.warn;
408+
logFn = instanaCtr.logger().warn;
385409
}
386410

387411
logFn(
@@ -428,7 +452,7 @@ function sendToBackend({ spans, metricsPayload, finalLambdaRequest, callback })
428452

429453
return ssm.waitAndGetInstanaKey((err, value) => {
430454
if (err) {
431-
logger.debug(err);
455+
instanaCtr.logger().debug(err);
432456
return callback();
433457
}
434458

@@ -513,14 +537,14 @@ function postHandlerForTimeout(entrySpan, remainingMillis) {
513537
* `setTimeout` is not 100% reliable
514538
*/
515539
if (remainingMillis < 200) {
516-
logger.debug('Skipping heuristical timeout detection because lambda timeout exceeded already.');
540+
instanaCtr.logger().debug('Skipping heuristical timeout detection because lambda timeout exceeded already.');
517541
return;
518542
}
519543

520544
if (entrySpan) {
521545
// CASE: Timeout not needed, we already send the data to the backend successfully
522546
if (entrySpan.transmitted) {
523-
logger.debug('Skipping heuristical timeout detection because BE data was sent already.');
547+
instanaCtr.logger().debug('Skipping heuristical timeout detection because BE data was sent already.');
524548
return;
525549
}
526550

@@ -531,7 +555,7 @@ function postHandlerForTimeout(entrySpan, remainingMillis) {
531555
entrySpan.transmit();
532556
}
533557

534-
logger.debug(`Heuristical timeout detection was triggered with ${remainingMillis} milliseconds left.`);
558+
instanaCtr.logger().debug(`Heuristical timeout detection was triggered with ${remainingMillis} milliseconds left.`);
535559

536560
// deliberately not gathering metrics but only sending spans.
537561
const spans = spanBuffer.getAndResetSpans();
@@ -551,7 +575,7 @@ exports.currentSpan = function getHandleForCurrentSpan() {
551575
exports.sdk = tracing.sdk;
552576

553577
exports.setLogger = function setLogger(_logger) {
554-
log.init({ logger: _logger });
578+
instanaCtr.logger().setLogger(_logger);
555579
};
556580

557581
exports.opentracing = tracing.opentracing;

0 commit comments

Comments
 (0)