Skip to content

Commit c3afab7

Browse files
feat(auto-instrumentations-node): disabling instrumentations via env var (#2174)
* feat(auto-instr-node): disabling instrumentations via env var * create new func to reduce code duplication * fix Object is possibly undefined --------- Co-authored-by: Amir Blum <[email protected]>
1 parent 72e3f66 commit c3afab7

File tree

3 files changed

+70
-9
lines changed

3 files changed

+70
-9
lines changed

metapackages/auto-instrumentations-node/README.md

+9-2
Original file line numberDiff line numberDiff line change
@@ -77,8 +77,9 @@ For example, to enable only the `env`, `host` detectors:
7777
export OTEL_NODE_RESOURCE_DETECTORS="env,host"
7878
```
7979

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

8485
For example, to enable only
@@ -90,6 +91,12 @@ instrumentations:
9091
export OTEL_NODE_ENABLED_INSTRUMENTATIONS="http,nestjs-core"
9192
```
9293

94+
To disable only [@opentelemetry/instrumentation-fs](https://github.com/open-telemetry/opentelemetry-js-contrib/tree/main/plugins/node/instrumentation-fs):
95+
96+
```shell
97+
export OTEL_NODE_DISABLED_INSTRUMENTATIONS="fs"
98+
```
99+
93100
To enable logging for troubleshooting, set the log level by setting the `OTEL_LOG_LEVEL` environment variable to one of the following:
94101

95102
- `none`

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

+36-7
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,7 @@ export function getNodeAutoInstrumentations(
147147
): Instrumentation[] {
148148
checkManuallyProvidedInstrumentationNames(Object.keys(inputConfigs));
149149
const enabledInstrumentationsFromEnv = getEnabledInstrumentationsFromEnv();
150+
const disabledInstrumentationsFromEnv = getDisabledInstrumentationsFromEnv();
150151

151152
const instrumentations: Instrumentation[] = [];
152153

@@ -159,7 +160,8 @@ export function getNodeAutoInstrumentations(
159160

160161
if (
161162
userConfig.enabled === false ||
162-
!enabledInstrumentationsFromEnv.includes(name)
163+
!enabledInstrumentationsFromEnv.includes(name) ||
164+
disabledInstrumentationsFromEnv.includes(name)
163165
) {
164166
diag.debug(`Disabling instrumentation for ${name}`);
165167
continue;
@@ -186,6 +188,22 @@ function checkManuallyProvidedInstrumentationNames(
186188
}
187189
}
188190

191+
function getInstrumentationsFromEnv(envVar: string): string[] {
192+
const envVarValue = process.env[envVar];
193+
if (envVarValue == null) {
194+
return [];
195+
}
196+
197+
const instrumentationsFromEnv = envVarValue
198+
?.split(',')
199+
.map(
200+
instrumentationPkgSuffix =>
201+
`@opentelemetry/instrumentation-${instrumentationPkgSuffix.trim()}`
202+
);
203+
checkManuallyProvidedInstrumentationNames(instrumentationsFromEnv);
204+
return instrumentationsFromEnv;
205+
}
206+
189207
/**
190208
* Returns the list of instrumentations that are enabled based on the environment variable.
191209
*/
@@ -194,12 +212,23 @@ function getEnabledInstrumentationsFromEnv() {
194212
return Object.keys(InstrumentationMap);
195213
}
196214

197-
const instrumentationsFromEnv =
198-
process.env.OTEL_NODE_ENABLED_INSTRUMENTATIONS.split(',').map(
199-
instrumentationPkgSuffix =>
200-
`@opentelemetry/instrumentation-${instrumentationPkgSuffix.trim()}`
201-
);
202-
checkManuallyProvidedInstrumentationNames(instrumentationsFromEnv);
215+
const instrumentationsFromEnv = getInstrumentationsFromEnv(
216+
'OTEL_NODE_ENABLED_INSTRUMENTATIONS'
217+
);
218+
return instrumentationsFromEnv;
219+
}
220+
221+
/**
222+
* Returns the list of instrumentations that are disabled based on the environment variable.
223+
*/
224+
function getDisabledInstrumentationsFromEnv() {
225+
if (!process.env.OTEL_NODE_DISABLED_INSTRUMENTATIONS) {
226+
return [];
227+
}
228+
229+
const instrumentationsFromEnv = getInstrumentationsFromEnv(
230+
'OTEL_NODE_DISABLED_INSTRUMENTATIONS'
231+
);
203232
return instrumentationsFromEnv;
204233
}
205234

metapackages/auto-instrumentations-node/test/utils.test.ts

+25
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,31 @@ describe('utils', () => {
8989
}
9090
});
9191

92+
it('should include all instrumentations except those disabled via OTEL_NODE_DISABLED_INSTRUMENTATIONS environment variable', () => {
93+
process.env.OTEL_NODE_DISABLED_INSTRUMENTATIONS =
94+
'fs,aws-sdk, aws-lambda'; // separator with and without whitespaces should be allowed
95+
try {
96+
const instrumentations = getNodeAutoInstrumentations();
97+
const disabledInstrumentations = new Set([
98+
'@opentelemetry/instrumentation-fs',
99+
'@opentelemetry/instrumentation-aws-sdk',
100+
'@opentelemetry/instrumentation-aws-lambda',
101+
]);
102+
const enabledInstrumentationNames = new Set(
103+
instrumentations.map(i => i.instrumentationName)
104+
);
105+
106+
for (const disabledInstrumentation of disabledInstrumentations) {
107+
assert.strictEqual(
108+
enabledInstrumentationNames.has(disabledInstrumentation),
109+
false
110+
);
111+
}
112+
} finally {
113+
delete process.env.OTEL_NODE_DISABLED_INSTRUMENTATIONS;
114+
}
115+
});
116+
92117
it('should show error for none existing instrumentation', () => {
93118
const spy = sinon.stub(diag, 'error');
94119
const name = '@opentelemetry/instrumentation-http2';

0 commit comments

Comments
 (0)