Skip to content

fix(aws-lambda): ensured tracer is properly disabled when configured #1593

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 4, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion packages/aws-lambda/src/wrapper.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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.');
Expand Down Expand Up @@ -282,6 +290,8 @@ function init(event, arnInfo, _config) {
metrics.init(config);
metrics.activate();
tracing.activate();

return true;
}

function registerTimeoutDetection(context, entrySpan) {
Expand Down
39 changes: 39 additions & 0 deletions packages/aws-lambda/test/integration_test/test_definition.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down Expand Up @@ -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 () {
Expand Down