diff --git a/packages/aws-lambda/src/wrapper.js b/packages/aws-lambda/src/wrapper.js index 5caa081219..8fc6cb4749 100644 --- a/packages/aws-lambda/src/wrapper.js +++ b/packages/aws-lambda/src/wrapper.js @@ -68,7 +68,11 @@ function shimmedHandler(originalHandler, originalThis, originalArgs, _config) { const lambdaCallback = originalArgs[2]; const arnInfo = arnParser(context); - init(event, arnInfo, _config); + 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 @@ -248,6 +252,10 @@ function init(event, arnInfo, _config) { // - we always renormalize unconditionally to ensure safety. config = normalizeConfig(config, logger); + 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.'); @@ -282,6 +290,8 @@ function init(event, arnInfo, _config) { metrics.init(config); metrics.activate(); tracing.activate(); + + return true; } function registerTimeoutDetection(context, entrySpan) { diff --git a/packages/aws-lambda/test/integration_test/test_definition.js b/packages/aws-lambda/test/integration_test/test_definition.js index 28e84a063d..a271d98dd6 100644 --- a/packages/aws-lambda/test/integration_test/test_definition.js +++ b/packages/aws-lambda/test/integration_test/test_definition.js @@ -57,6 +57,9 @@ function prelude(opts) { if (opts.instanaEndpointUrlMissing) { env.INSTANA_ENDPOINT_URL = ''; } + if (opts.instanaTracingDisabled) { + env.INSTANA_DISABLE_TRACING = 'true'; + } if (opts.instanaAgentKey) { env.INSTANA_AGENT_KEY = opts.instanaAgentKey; } @@ -988,6 +991,42 @@ function registerTests(handlerDefinitionPath, reduced) { }); }); + describeOrSkipIfReduced()('when INSTANA_DISABLE_TRACING is set', function () { + // - INSTANA_ENDPOINT_URL is missing + // - lambda function ends with success + const env = prelude.bind(this)({ + handlerDefinitionPath, + instanaAgentKey, + instanaTracingDisabled: true + }); + + let control; + + before(async () => { + control = new Control({ + faasRuntimePath: path.join(__dirname, '../runtime_mock'), + handlerDefinitionPath, + startBackend: true, + env + }); + + await control.start(); + }); + + beforeEach(async () => { + await control.reset(); + await control.resetBackendSpansAndMetrics(); + }); + + after(async () => { + await control.stop(); + }); + + it('expect no tracing data', () => { + return verify(control, { error: false, expectMetrics: false, expectSpans: false }); + }); + }); + describeOrSkipIfReduced(reduced)( 'when the back end becomes responsive again after a timeout in a previous handler run', function () {