From 2a31fb02734c19f5b305f01472102329e358a085 Mon Sep 17 00:00:00 2001 From: Jamie Danielson Date: Thu, 2 May 2024 17:55:31 -0400 Subject: [PATCH 1/3] feat(auto-instr-node): disabling instrumentations via env var --- .../auto-instrumentations-node/README.md | 11 ++++++-- .../auto-instrumentations-node/src/utils.ts | 21 +++++++++++++++- .../test/utils.test.ts | 25 +++++++++++++++++++ 3 files changed, 54 insertions(+), 3 deletions(-) diff --git a/metapackages/auto-instrumentations-node/README.md b/metapackages/auto-instrumentations-node/README.md index 181d920386..d9c31fad7e 100644 --- a/metapackages/auto-instrumentations-node/README.md +++ b/metapackages/auto-instrumentations-node/README.md @@ -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 @@ -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` diff --git a/metapackages/auto-instrumentations-node/src/utils.ts b/metapackages/auto-instrumentations-node/src/utils.ts index 04f4c1ed50..54c9f9d276 100644 --- a/metapackages/auto-instrumentations-node/src/utils.ts +++ b/metapackages/auto-instrumentations-node/src/utils.ts @@ -147,6 +147,7 @@ export function getNodeAutoInstrumentations( ): Instrumentation[] { checkManuallyProvidedInstrumentationNames(Object.keys(inputConfigs)); const enabledInstrumentationsFromEnv = getEnabledInstrumentationsFromEnv(); + const disabledInstrumentationsFromEnv = getDisabledInstrumentationsFromEnv(); const instrumentations: Instrumentation[] = []; @@ -159,7 +160,8 @@ export function getNodeAutoInstrumentations( if ( userConfig.enabled === false || - !enabledInstrumentationsFromEnv.includes(name) + !enabledInstrumentationsFromEnv.includes(name) || + disabledInstrumentationsFromEnv.includes(name) ) { diag.debug(`Disabling instrumentation for ${name}`); continue; @@ -203,6 +205,23 @@ function getEnabledInstrumentationsFromEnv() { 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 = + process.env.OTEL_NODE_DISABLED_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 0622022f36..ceea3262a8 100644 --- a/metapackages/auto-instrumentations-node/test/utils.test.ts +++ b/metapackages/auto-instrumentations-node/test/utils.test.ts @@ -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'; From bed3d234d865f74886e96d4c995b45ff520a65f1 Mon Sep 17 00:00:00 2001 From: Jamie Danielson Date: Mon, 6 May 2024 18:38:11 -0400 Subject: [PATCH 2/3] create new func to reduce code duplication --- .../auto-instrumentations-node/src/utils.ts | 33 ++++++++++++------- 1 file changed, 21 insertions(+), 12 deletions(-) diff --git a/metapackages/auto-instrumentations-node/src/utils.ts b/metapackages/auto-instrumentations-node/src/utils.ts index 54c9f9d276..4c082aca4c 100644 --- a/metapackages/auto-instrumentations-node/src/utils.ts +++ b/metapackages/auto-instrumentations-node/src/utils.ts @@ -188,6 +188,21 @@ function checkManuallyProvidedInstrumentationNames( } } +function getInstrumentationsFromEnv(envVar: string): string[] { + if (!process.env[envVar]) { + return []; + } + + const instrumentationsFromEnv = process.env[envVar] + .split(',') + .map( + instrumentationPkgSuffix => + `@opentelemetry/instrumentation-${instrumentationPkgSuffix.trim()}` + ); + checkManuallyProvidedInstrumentationNames(instrumentationsFromEnv); + return instrumentationsFromEnv; +} + /** * Returns the list of instrumentations that are enabled based on the environment variable. */ @@ -196,12 +211,9 @@ function getEnabledInstrumentationsFromEnv() { 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; } @@ -213,12 +225,9 @@ function getDisabledInstrumentationsFromEnv() { return []; } - const instrumentationsFromEnv = - process.env.OTEL_NODE_DISABLED_INSTRUMENTATIONS.split(',').map( - instrumentationPkgSuffix => - `@opentelemetry/instrumentation-${instrumentationPkgSuffix.trim()}` - ); - checkManuallyProvidedInstrumentationNames(instrumentationsFromEnv); + const instrumentationsFromEnv = getInstrumentationsFromEnv( + 'OTEL_NODE_DISABLED_INSTRUMENTATIONS' + ); return instrumentationsFromEnv; } From a5186730defc97d0b8ec3fe986728e0243c50fb3 Mon Sep 17 00:00:00 2001 From: Jamie Danielson Date: Mon, 6 May 2024 18:59:19 -0400 Subject: [PATCH 3/3] fix Object is possibly undefined --- metapackages/auto-instrumentations-node/src/utils.ts | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/metapackages/auto-instrumentations-node/src/utils.ts b/metapackages/auto-instrumentations-node/src/utils.ts index 4c082aca4c..c258dfc597 100644 --- a/metapackages/auto-instrumentations-node/src/utils.ts +++ b/metapackages/auto-instrumentations-node/src/utils.ts @@ -189,12 +189,13 @@ function checkManuallyProvidedInstrumentationNames( } function getInstrumentationsFromEnv(envVar: string): string[] { - if (!process.env[envVar]) { + const envVarValue = process.env[envVar]; + if (envVarValue == null) { return []; } - const instrumentationsFromEnv = process.env[envVar] - .split(',') + const instrumentationsFromEnv = envVarValue + ?.split(',') .map( instrumentationPkgSuffix => `@opentelemetry/instrumentation-${instrumentationPkgSuffix.trim()}`