diff --git a/metapackages/auto-instrumentations-node/README.md b/metapackages/auto-instrumentations-node/README.md index cc45a14dbe..5a7ed1aec1 100644 --- a/metapackages/auto-instrumentations-node/README.md +++ b/metapackages/auto-instrumentations-node/README.md @@ -75,6 +75,21 @@ 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_ENABLED_NODE_INSTRUMENTATIONS` to enable only certain instrumentations +by providing a comma-separated list of the instrumentation package names without the `@opentelemetry/instrumentation-` prefix. + +For example, to enable only +[@opentelemetry/instrumentation-http](https://github.com/open-telemetry/opentelemetry-js/tree/main/packages/opentelemetry-instrumentation-http) +and [@opentelemetry/instrumentation-nestjs-core](https://github.com/open-telemetry/opentelemetry-js-contrib/tree/main/plugins/node/opentelemetry-instrumentation-nestjs-core) +instrumentations: + +```shell +export OTEL_ENABLED_NODE_INSTRUMENTATIONS="http,nestjs-core" +``` + + To enable logging for troubleshooting, set the log level by setting the `OTEL_LOG_LEVEL` environment variable to one of the following: - `none` diff --git a/metapackages/auto-instrumentations-node/src/utils.ts b/metapackages/auto-instrumentations-node/src/utils.ts index a6c4422ad1..265982da7a 100644 --- a/metapackages/auto-instrumentations-node/src/utils.ts +++ b/metapackages/auto-instrumentations-node/src/utils.ts @@ -136,6 +136,7 @@ export function getNodeAutoInstrumentations( inputConfigs: InstrumentationConfigMap = {} ): Instrumentation[] { checkManuallyProvidedInstrumentationNames(Object.keys(inputConfigs)); + const enabledInstrumentationsFromEnv = getEnabledInstrumentationsFromEnv(); const instrumentations: Instrumentation[] = []; @@ -146,7 +147,10 @@ export function getNodeAutoInstrumentations( // Defaults are defined by the instrumentation itself const userConfig: any = inputConfigs[name] ?? {}; - if (userConfig.enabled === false) { + if ( + userConfig.enabled === false || + !enabledInstrumentationsFromEnv.includes(name) + ) { diag.debug(`Disabling instrumentation for ${name}`); continue; } @@ -172,6 +176,24 @@ function checkManuallyProvidedInstrumentationNames( } } +/** + * Returns the list of instrumentations that are enabled based on the environment variable. + */ +function getEnabledInstrumentationsFromEnv() { + if (!process.env.OTEL_ENABLED_NODE_INSTRUMENTATIONS) { + return Object.keys(InstrumentationMap); + } + + const instrumentationsFromEnv = process.env.OTEL_ENABLED_NODE_INSTRUMENTATIONS.split( + ',' + ).map( + instrumentationPkgSuffix => + `@opentelemetry/instrumentation-${instrumentationPkgSuffix.trim()}` + ); + checkManuallyProvidedInstrumentationNames(instrumentationsFromEnv); + return instrumentationsFromEnv; +} + export function getResourceDetectorsFromEnv(): Array { const resourceDetectors = new Map< string, diff --git a/metapackages/auto-instrumentations-node/test/utils.test.ts b/metapackages/auto-instrumentations-node/test/utils.test.ts index f295668d4f..1f161a7be3 100644 --- a/metapackages/auto-instrumentations-node/test/utils.test.ts +++ b/metapackages/auto-instrumentations-node/test/utils.test.ts @@ -70,6 +70,24 @@ describe('utils', () => { assert.strictEqual(instrumentation, undefined); }); + it('should return only instrumentations enabled via OTEL_ENABLED_NODE_INSTRUMENTATIONS environment variable', () => { + process.env.OTEL_ENABLED_NODE_INSTRUMENTATIONS = 'http,aws-sdk, nestjs-core'; // separator with and without whitespaces should be allowed + try { + const instrumentations = getNodeAutoInstrumentations(); + + assert.deepStrictEqual( + new Set(instrumentations.map(i => i.instrumentationName)), + new Set([ + '@opentelemetry/instrumentation-http', + '@opentelemetry/instrumentation-aws-sdk', + '@opentelemetry/instrumentation-nestjs-core', + ]) + ); + } finally { + delete process.env.OTEL_ENABLED_NODE_INSTRUMENTATIONS; + } + }); + it('should show error for none existing instrumentation', () => { const spy = sinon.stub(diag, 'error'); const name = '@opentelemetry/instrumentation-http2';