Skip to content

Commit cf80246

Browse files
YangJonghundavid-lunamaryliag
authored
fix: enable programmatic config when environment variables are unset (#2947)
Co-authored-by: David Luna <[email protected]> Co-authored-by: Marylia Gutierrez <[email protected]>
1 parent 510d091 commit cf80246

File tree

2 files changed

+138
-6
lines changed

2 files changed

+138
-6
lines changed

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

Lines changed: 52 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,50 @@ export type InstrumentationConfigMap = {
152152
>;
153153
};
154154

155+
function shouldDisableInstrumentation(
156+
name: string,
157+
userConfig: any,
158+
enabledInstrumentationsFromEnv: string[],
159+
disabledInstrumentationsFromEnv: string[]
160+
): boolean {
161+
// Priority 1: Programmatic config
162+
if (userConfig.enabled === false) {
163+
diag.debug(
164+
`Disabling instrumentation for ${name} - disabled by user config`
165+
);
166+
return true;
167+
}
168+
169+
if (userConfig.enabled === true) {
170+
diag.debug(
171+
`Enabling instrumentation for ${name} - explicitly enabled by user config`
172+
);
173+
return false;
174+
}
175+
176+
// Priority 2: Environment variables
177+
if (disabledInstrumentationsFromEnv.includes(name)) {
178+
diag.debug(`Disabling instrumentation for ${name} - disabled by env var`);
179+
return true;
180+
}
181+
182+
const isEnabledEnvSet = !!process.env.OTEL_NODE_ENABLED_INSTRUMENTATIONS;
183+
if (isEnabledEnvSet && !enabledInstrumentationsFromEnv.includes(name)) {
184+
diag.debug(
185+
`Disabling instrumentation for ${name} - not in enabled env var list`
186+
);
187+
return true;
188+
}
189+
190+
// Priority 3: Default exclusions
191+
if (!isEnabledEnvSet && defaultExcludedInstrumentations.includes(name)) {
192+
diag.debug(`Disabling instrumentation for ${name} - excluded by default`);
193+
return true;
194+
}
195+
196+
return false;
197+
}
198+
155199
export function getNodeAutoInstrumentations(
156200
inputConfigs: InstrumentationConfigMap = {}
157201
): Instrumentation[] {
@@ -168,12 +212,14 @@ export function getNodeAutoInstrumentations(
168212
// Defaults are defined by the instrumentation itself
169213
const userConfig: any = inputConfigs[name] ?? {};
170214

171-
if (
172-
userConfig.enabled === false ||
173-
!enabledInstrumentationsFromEnv.includes(name) ||
174-
disabledInstrumentationsFromEnv.includes(name)
175-
) {
176-
diag.debug(`Disabling instrumentation for ${name}`);
215+
const shouldDisable = shouldDisableInstrumentation(
216+
name,
217+
userConfig,
218+
enabledInstrumentationsFromEnv,
219+
disabledInstrumentationsFromEnv
220+
);
221+
222+
if (shouldDisable) {
177223
continue;
178224
}
179225

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

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,92 @@ describe('utils', () => {
172172

173173
spy.restore();
174174
});
175+
176+
it('should enable fs instrumentation when explicitly enabled and OTEL_NODE_ENABLED_INSTRUMENTATIONS is not set', () => {
177+
const instrumentations = getNodeAutoInstrumentations({
178+
'@opentelemetry/instrumentation-fs': {
179+
enabled: true,
180+
},
181+
});
182+
const fsInstrumentation = instrumentations.find(
183+
instr =>
184+
instr.instrumentationName === '@opentelemetry/instrumentation-fs'
185+
);
186+
assert.notStrictEqual(
187+
fsInstrumentation,
188+
undefined,
189+
'fs instrumentation should be included when explicitly enabled in config and env var is not set'
190+
);
191+
});
192+
193+
it('should respect programmatic config over OTEL_NODE_ENABLED_INSTRUMENTATIONS - user config overrides env var', () => {
194+
process.env.OTEL_NODE_ENABLED_INSTRUMENTATIONS = 'fs,http';
195+
try {
196+
const instrumentations = getNodeAutoInstrumentations({
197+
'@opentelemetry/instrumentation-fs': {
198+
enabled: false, // User explicitly disables - should override env var
199+
},
200+
'@opentelemetry/instrumentation-express': {
201+
enabled: true, // User explicitly enables - should override env var
202+
},
203+
});
204+
205+
const fsInstrumentation = instrumentations.find(
206+
instr =>
207+
instr.instrumentationName === '@opentelemetry/instrumentation-fs'
208+
);
209+
const httpInstrumentation = instrumentations.find(
210+
instr =>
211+
instr.instrumentationName === '@opentelemetry/instrumentation-http'
212+
);
213+
const expressInstrumentation = instrumentations.find(
214+
instr =>
215+
instr.instrumentationName ===
216+
'@opentelemetry/instrumentation-express'
217+
);
218+
219+
assert.strictEqual(
220+
fsInstrumentation,
221+
undefined,
222+
'fs should be disabled by user config despite env var'
223+
);
224+
assert.notStrictEqual(
225+
httpInstrumentation,
226+
undefined,
227+
'http should be enabled by env var (no user override)'
228+
);
229+
assert.notStrictEqual(
230+
expressInstrumentation,
231+
undefined,
232+
'express should be enabled by user config despite not being in env var list'
233+
);
234+
} finally {
235+
delete process.env.OTEL_NODE_ENABLED_INSTRUMENTATIONS;
236+
}
237+
});
238+
239+
it('should respect programmatic config over OTEL_NODE_DISABLED_INSTRUMENTATIONS - user config overrides env var', () => {
240+
process.env.OTEL_NODE_DISABLED_INSTRUMENTATIONS = 'http';
241+
try {
242+
const instrumentations = getNodeAutoInstrumentations({
243+
'@opentelemetry/instrumentation-http': {
244+
enabled: true, // User explicitly enables - should override env var
245+
},
246+
});
247+
const httpInstrumentation = instrumentations.find(
248+
instr =>
249+
instr.instrumentationName === '@opentelemetry/instrumentation-http'
250+
);
251+
252+
assert.notStrictEqual(
253+
httpInstrumentation,
254+
undefined,
255+
'http instrumentation should be enabled by user config despite OTEL_NODE_DISABLED_INSTRUMENTATIONS'
256+
);
257+
} finally {
258+
delete process.env.OTEL_NODE_DISABLED_INSTRUMENTATIONS;
259+
}
260+
});
175261
});
176262

177263
describe('getResourceDetectorsFromEnv', () => {

0 commit comments

Comments
 (0)