Skip to content
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

feat(auto-instrumentations-node): disabling instrumentations via env var #2174

Merged
11 changes: 9 additions & 2 deletions metapackages/auto-instrumentations-node/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -77,8 +77,9 @@ For example, to enable only the `env`, `host` detectors:
export OTEL_NODE_RESOURCE_DETECTORS="env,host"
```

By default, all [Supported Instrumentations](#supported-instrumentations) are enabled,
but you can use the environment variable `OTEL_NODE_ENABLED_INSTRUMENTATIONS` to enable only certain instrumentations
By default, all [Supported Instrumentations](#supported-instrumentations) are enabled.
You can use the environment variable `OTEL_NODE_ENABLED_INSTRUMENTATIONS` to enable only certain instrumentations,
OR the environment variable `OTEL_NODE_DISABLED_INSTRUMENTATIONS` to disable only certain instrumentations,
by providing a comma-separated list of the instrumentation package names without the `@opentelemetry/instrumentation-` prefix.

For example, to enable only
Expand All @@ -90,6 +91,12 @@ instrumentations:
export OTEL_NODE_ENABLED_INSTRUMENTATIONS="http,nestjs-core"
```

To disable only [@opentelemetry/instrumentation-fs](https://github.com/open-telemetry/opentelemetry-js-contrib/tree/main/plugins/node/instrumentation-fs):

```shell
export OTEL_NODE_DISABLED_INSTRUMENTATIONS="fs"
```

To enable logging for troubleshooting, set the log level by setting the `OTEL_LOG_LEVEL` environment variable to one of the following:

- `none`
Expand Down
43 changes: 36 additions & 7 deletions metapackages/auto-instrumentations-node/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,7 @@
): Instrumentation[] {
checkManuallyProvidedInstrumentationNames(Object.keys(inputConfigs));
const enabledInstrumentationsFromEnv = getEnabledInstrumentationsFromEnv();
const disabledInstrumentationsFromEnv = getDisabledInstrumentationsFromEnv();

const instrumentations: Instrumentation[] = [];

Expand All @@ -159,7 +160,8 @@

if (
userConfig.enabled === false ||
!enabledInstrumentationsFromEnv.includes(name)
!enabledInstrumentationsFromEnv.includes(name) ||
disabledInstrumentationsFromEnv.includes(name)
) {
diag.debug(`Disabling instrumentation for ${name}`);
continue;
Expand All @@ -186,6 +188,22 @@
}
}

function getInstrumentationsFromEnv(envVar: string): string[] {
const envVarValue = process.env[envVar];
if (envVarValue == null) {
return [];

Check warning on line 194 in metapackages/auto-instrumentations-node/src/utils.ts

View check run for this annotation

Codecov / codecov/patch

metapackages/auto-instrumentations-node/src/utils.ts#L194

Added line #L194 was not covered by tests
}

const instrumentationsFromEnv = envVarValue
?.split(',')
.map(
instrumentationPkgSuffix =>
`@opentelemetry/instrumentation-${instrumentationPkgSuffix.trim()}`
);
checkManuallyProvidedInstrumentationNames(instrumentationsFromEnv);
return instrumentationsFromEnv;
}

/**
* Returns the list of instrumentations that are enabled based on the environment variable.
*/
Expand All @@ -194,12 +212,23 @@
return Object.keys(InstrumentationMap);
}

const instrumentationsFromEnv =
process.env.OTEL_NODE_ENABLED_INSTRUMENTATIONS.split(',').map(
instrumentationPkgSuffix =>
`@opentelemetry/instrumentation-${instrumentationPkgSuffix.trim()}`
);
checkManuallyProvidedInstrumentationNames(instrumentationsFromEnv);
const instrumentationsFromEnv = getInstrumentationsFromEnv(
'OTEL_NODE_ENABLED_INSTRUMENTATIONS'
);
return instrumentationsFromEnv;
}

/**
* Returns the list of instrumentations that are disabled based on the environment variable.
*/
function getDisabledInstrumentationsFromEnv() {
if (!process.env.OTEL_NODE_DISABLED_INSTRUMENTATIONS) {
return [];
}

const instrumentationsFromEnv = getInstrumentationsFromEnv(
'OTEL_NODE_DISABLED_INSTRUMENTATIONS'
);
return instrumentationsFromEnv;
}

Expand Down
25 changes: 25 additions & 0 deletions metapackages/auto-instrumentations-node/test/utils.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,31 @@ describe('utils', () => {
}
});

it('should include all instrumentations except those disabled via OTEL_NODE_DISABLED_INSTRUMENTATIONS environment variable', () => {
process.env.OTEL_NODE_DISABLED_INSTRUMENTATIONS =
'fs,aws-sdk, aws-lambda'; // separator with and without whitespaces should be allowed
try {
const instrumentations = getNodeAutoInstrumentations();
const disabledInstrumentations = new Set([
'@opentelemetry/instrumentation-fs',
'@opentelemetry/instrumentation-aws-sdk',
'@opentelemetry/instrumentation-aws-lambda',
]);
const enabledInstrumentationNames = new Set(
instrumentations.map(i => i.instrumentationName)
);

for (const disabledInstrumentation of disabledInstrumentations) {
assert.strictEqual(
enabledInstrumentationNames.has(disabledInstrumentation),
false
);
}
} finally {
delete process.env.OTEL_NODE_DISABLED_INSTRUMENTATIONS;
}
});

it('should show error for none existing instrumentation', () => {
const spy = sinon.stub(diag, 'error');
const name = '@opentelemetry/instrumentation-http2';
Expand Down