diff --git a/metapackages/auto-instrumentations-node/README.md b/metapackages/auto-instrumentations-node/README.md index 2674dfd4fe..b41c31fa3c 100644 --- a/metapackages/auto-instrumentations-node/README.md +++ b/metapackages/auto-instrumentations-node/README.md @@ -77,8 +77,8 @@ 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. -You can use the environment variable `OTEL_NODE_ENABLED_INSTRUMENTATIONS` to enable only certain instrumentations, +By default, all [Supported Instrumentations](#supported-instrumentations) are enabled, unless they are annotated with "default disabled". +You can use the environment variable `OTEL_NODE_ENABLED_INSTRUMENTATIONS` to enable only certain instrumentations, including "default disabled" ones 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. @@ -91,10 +91,10 @@ 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): +To disable only [@opentelemetry/instrumentation-net](https://github.com/open-telemetry/opentelemetry-js-contrib/tree/main/plugins/node/instrumentation-net): ```shell -export OTEL_NODE_DISABLED_INSTRUMENTATIONS="fs" +export OTEL_NODE_DISABLED_INSTRUMENTATIONS="net" ``` If both environment variables are set, `OTEL_NODE_ENABLED_INSTRUMENTATIONS` is applied first, and then `OTEL_NODE_DISABLED_INSTRUMENTATIONS` is applied to that list. @@ -170,6 +170,7 @@ registerInstrumentations({ - [@opentelemetry/instrumentation-dns](https://github.com/open-telemetry/opentelemetry-js-contrib/tree/main/plugins/node/opentelemetry-instrumentation-dns) - [@opentelemetry/instrumentation-express](https://github.com/open-telemetry/opentelemetry-js-contrib/tree/main/plugins/node/opentelemetry-instrumentation-express) - [@opentelemetry/instrumentation-fastify](https://github.com/open-telemetry/opentelemetry-js-contrib/tree/main/plugins/node/opentelemetry-instrumentation-fastify) +- [@opentelemetry/instrumentation-fs](https://github.com/open-telemetry/opentelemetry-js-contrib/tree/main/plugins/node/instrumentation-fs) (default disabled) - [@opentelemetry/instrumentation-generic-pool](https://github.com/open-telemetry/opentelemetry-js-contrib/tree/main/plugins/node/opentelemetry-instrumentation-generic-pool) - [@opentelemetry/instrumentation-graphql](https://github.com/open-telemetry/opentelemetry-js-contrib/tree/main/plugins/node/opentelemetry-instrumentation-graphql) - [@opentelemetry/instrumentation-grpc](https://github.com/open-telemetry/opentelemetry-js/tree/main/experimental/packages/opentelemetry-instrumentation-grpc) diff --git a/metapackages/auto-instrumentations-node/src/utils.ts b/metapackages/auto-instrumentations-node/src/utils.ts index b0417115a5..d7e6a0cd6b 100644 --- a/metapackages/auto-instrumentations-node/src/utils.ts +++ b/metapackages/auto-instrumentations-node/src/utils.ts @@ -136,6 +136,8 @@ const InstrumentationMap = { '@opentelemetry/instrumentation-winston': WinstonInstrumentation, }; +const defaultExcludedInstrumentations = ['@opentelemetry/instrumentation-fs']; + // Config types inferred automatically from the first argument of the constructor type ConfigArg = T extends new (...args: infer U) => unknown ? U[0] : never; export type InstrumentationConfigMap = { @@ -208,10 +210,14 @@ function getInstrumentationsFromEnv(envVar: string): string[] { /** * Returns the list of instrumentations that are enabled based on the environment variable. + * If the environment variable is unset, returns all instrumentation that are enabled by default. */ function getEnabledInstrumentationsFromEnv() { if (!process.env.OTEL_NODE_ENABLED_INSTRUMENTATIONS) { - return Object.keys(InstrumentationMap); + // all keys in the InstrumentationMap except for everything that is not enabled by default. + return Object.keys(InstrumentationMap).filter( + key => !defaultExcludedInstrumentations.includes(key) + ); } const instrumentationsFromEnv = getInstrumentationsFromEnv( diff --git a/metapackages/auto-instrumentations-node/test/utils.test.ts b/metapackages/auto-instrumentations-node/test/utils.test.ts index cc7cb24c68..d2959b6ddd 100644 --- a/metapackages/auto-instrumentations-node/test/utils.test.ts +++ b/metapackages/auto-instrumentations-node/test/utils.test.ts @@ -23,12 +23,15 @@ import { getResourceDetectorsFromEnv } from '../src/utils'; describe('utils', () => { describe('getNodeAutoInstrumentations', () => { - it('should include all installed instrumentations', () => { + it('should include all default instrumentations', () => { const instrumentations = getNodeAutoInstrumentations(); const installedInstrumentations = Object.keys( require('../package.json').dependencies ).filter(depName => { - return depName.startsWith('@opentelemetry/instrumentation-'); + return ( + depName.startsWith('@opentelemetry/instrumentation-') && + depName !== '@opentelemetry/instrumentation-fs' + ); }); assert.deepStrictEqual( @@ -89,6 +92,20 @@ describe('utils', () => { } }); + it('should allow enabling non-default instrumentations via OTEL_NODE_ENABLED_INSTRUMENTATIONS environment variable', () => { + process.env.OTEL_NODE_ENABLED_INSTRUMENTATIONS = 'fs'; // 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-fs']) + ); + } finally { + delete process.env.OTEL_NODE_ENABLED_INSTRUMENTATIONS; + } + }); + 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