Skip to content

Commit e1e4e4b

Browse files
committed
fix: make programmatic config override env vars
1 parent 07463f8 commit e1e4e4b

File tree

2 files changed

+59
-39
lines changed

2 files changed

+59
-39
lines changed

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

Lines changed: 46 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,44 @@ export type InstrumentationConfigMap = {
150150
>;
151151
};
152152

153+
function shouldDisableInstrumentation(
154+
name: string,
155+
userConfig: any,
156+
enabledInstrumentationsFromEnv: string[],
157+
disabledInstrumentationsFromEnv: string[]
158+
): boolean {
159+
// Priority 1: Programmatic config
160+
if (userConfig.enabled === false) {
161+
diag.debug(`Disabling instrumentation for ${name} - disabled by user config`);
162+
return true;
163+
}
164+
165+
if (userConfig.enabled === true) {
166+
diag.debug(`Enabling instrumentation for ${name} - explicitly enabled by user config`);
167+
return false;
168+
}
169+
170+
// Priority 2: Environment variables
171+
if (disabledInstrumentationsFromEnv.includes(name)) {
172+
diag.debug(`Disabling instrumentation for ${name} - disabled by env var`);
173+
return true;
174+
}
175+
176+
const isEnabledEnvSet = !!process.env.OTEL_NODE_ENABLED_INSTRUMENTATIONS;
177+
if (isEnabledEnvSet && !enabledInstrumentationsFromEnv.includes(name)) {
178+
diag.debug(`Disabling instrumentation for ${name} - not in enabled env var list`);
179+
return true;
180+
}
181+
182+
// Priority 3: Default exclusions
183+
if (!isEnabledEnvSet && defaultExcludedInstrumentations.includes(name)) {
184+
diag.debug(`Disabling instrumentation for ${name} - excluded by default`);
185+
return true;
186+
}
187+
188+
return false;
189+
}
190+
153191
export function getNodeAutoInstrumentations(
154192
inputConfigs: InstrumentationConfigMap = {}
155193
): Instrumentation[] {
@@ -165,36 +203,18 @@ export function getNodeAutoInstrumentations(
165203
const Instance = InstrumentationMap[name];
166204
// Defaults are defined by the instrumentation itself
167205
const userConfig: any = inputConfigs[name] ?? {};
206+
207+
const shouldDisable = shouldDisableInstrumentation(
208+
name,
209+
userConfig,
210+
enabledInstrumentationsFromEnv,
211+
disabledInstrumentationsFromEnv
212+
);
168213

169-
// Configuration priority: Environment variables > programmatic config
170-
// If both OTEL_NODE_ENABLED_INSTRUMENTATIONS and OTEL_NODE_DISABLED_INSTRUMENTATIONS are set, ENABLED is applied first, then DISABLED is applied to that list.
171-
// If the same instrumentation is in both lists, it will be disabled.
172-
// When env vars are unset, allow programmatic config to override default exclusions
173-
174-
if (disabledInstrumentationsFromEnv.includes(name)) {
175-
diag.debug(`Disabling instrumentation for ${name} - disabled by env var`);
214+
if (shouldDisable) {
176215
continue;
177216
}
178217

179-
const isEnabledEnvSet = !!process.env.OTEL_NODE_ENABLED_INSTRUMENTATIONS;
180-
if (isEnabledEnvSet) {
181-
if (!enabledInstrumentationsFromEnv.includes(name)) {
182-
diag.debug(`Disabling instrumentation for ${name} - not in enabled env var list`);
183-
continue;
184-
}
185-
} else {
186-
if (userConfig.enabled === false) {
187-
diag.debug(`Disabling instrumentation for ${name} - disabled by user config`);
188-
continue;
189-
}
190-
191-
const isDefaultExcluded = defaultExcludedInstrumentations.includes(name);
192-
if (isDefaultExcluded && userConfig.enabled !== true) {
193-
diag.debug(`Disabling instrumentation for ${name} - excluded by default and not explicitly enabled`);
194-
continue;
195-
}
196-
}
197-
198218
try {
199219
diag.debug(`Loading instrumentation for ${name}`);
200220
instrumentations.push(new Instance(userConfig));

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

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -190,18 +190,18 @@ describe('utils', () => {
190190
);
191191
});
192192

193-
it('should respect OTEL_NODE_ENABLED_INSTRUMENTATIONS - env var overrides user config', () => {
193+
it('should respect programmatic config over OTEL_NODE_ENABLED_INSTRUMENTATIONS - user config overrides env var', () => {
194194
process.env.OTEL_NODE_ENABLED_INSTRUMENTATIONS = 'fs,http';
195195
try {
196196
const instrumentations = getNodeAutoInstrumentations({
197197
'@opentelemetry/instrumentation-fs': {
198-
enabled: false, // User tries to disable but env var overrides
198+
enabled: false, // User explicitly disables - should override env var
199199
},
200200
'@opentelemetry/instrumentation-express': {
201-
enabled: true, // User tries to enable but not in env var list
201+
enabled: true, // User explicitly enables - should override env var
202202
},
203203
});
204-
204+
205205
const fsInstrumentation = instrumentations.find(
206206
instr => instr.instrumentationName === '@opentelemetry/instrumentation-fs'
207207
);
@@ -211,32 +211,32 @@ describe('utils', () => {
211211
const expressInstrumentation = instrumentations.find(
212212
instr => instr.instrumentationName === '@opentelemetry/instrumentation-express'
213213
);
214-
215-
assert.notStrictEqual(fsInstrumentation, undefined, 'fs should be enabled by env var despite user config');
216-
assert.notStrictEqual(httpInstrumentation, undefined, 'http should be enabled by env var');
217-
assert.strictEqual(expressInstrumentation, undefined, 'express should be disabled - not in env var list');
214+
215+
assert.strictEqual(fsInstrumentation, undefined, 'fs should be disabled by user config despite env var');
216+
assert.notStrictEqual(httpInstrumentation, undefined, 'http should be enabled by env var (no user override)');
217+
assert.notStrictEqual(expressInstrumentation, undefined, 'express should be enabled by user config despite not being in env var list');
218218
} finally {
219219
delete process.env.OTEL_NODE_ENABLED_INSTRUMENTATIONS;
220220
}
221221
});
222222

223-
it('should respect OTEL_NODE_DISABLED_INSTRUMENTATIONS with absolute priority', () => {
223+
it('should respect programmatic config over OTEL_NODE_DISABLED_INSTRUMENTATIONS - user config overrides env var', () => {
224224
process.env.OTEL_NODE_DISABLED_INSTRUMENTATIONS = 'http';
225225
try {
226226
const instrumentations = getNodeAutoInstrumentations({
227227
'@opentelemetry/instrumentation-http': {
228-
enabled: true, // User tries to enable but env var disables
228+
enabled: true, // User explicitly enables - should override env var
229229
},
230230
});
231231
const httpInstrumentation = instrumentations.find(
232232
instr =>
233233
instr.instrumentationName === '@opentelemetry/instrumentation-http'
234234
);
235-
236-
assert.strictEqual(
235+
236+
assert.notStrictEqual(
237237
httpInstrumentation,
238238
undefined,
239-
'http instrumentation should be disabled by OTEL_NODE_DISABLED_INSTRUMENTATIONS even when user enables it'
239+
'http instrumentation should be enabled by user config despite OTEL_NODE_DISABLED_INSTRUMENTATIONS'
240240
);
241241
} finally {
242242
delete process.env.OTEL_NODE_DISABLED_INSTRUMENTATIONS;

0 commit comments

Comments
 (0)