Skip to content

Commit 7ae51db

Browse files
committed
fix(otel): only warn for a customer-set OTel flag missing the api peer
The guard that disables DD_LOGS_OTEL_ENABLED and DD_METRICS_OTEL_ENABLED when @opentelemetry/api is absent warned unconditionally. If a later release defaults one of these flags on, every app without the optional peer would get a warning for a feature it never opted into. Warn only when the flag's origin is the customer (tracked in trackedConfigOrigins); disable a default-on flag silently. The config test harness gains a defaultsOverride hook so the default-on path runs through the real #applyDefaults (which sets defaults untracked) instead of poking internal state. Refs: #9077 (comment)
1 parent c3a5d80 commit 7ae51db

2 files changed

Lines changed: 40 additions & 7 deletions

File tree

packages/dd-trace/src/config/index.js

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -344,15 +344,17 @@ class Config extends ConfigBase {
344344
}
345345
// Both OTel pipelines require the optional @opentelemetry/api peer dep. Disable them before
346346
// the log-injection mutual exclusion below, so a missing module leaves DD log injection on.
347+
// Warn only when the customer turned the flag on; a default-on flag is disabled silently so
348+
// changing a default later does not warn every app that never opted in.
347349
if ((this.DD_LOGS_OTEL_ENABLED || this.DD_METRICS_OTEL_ENABLED) &&
348350
!require('../opentelemetry/api').isAvailable()) {
349-
if (this.DD_LOGS_OTEL_ENABLED) {
350-
log.warn('@opentelemetry/api is not installed; disabling DD_LOGS_OTEL_ENABLED')
351-
setAndTrack(this, 'DD_LOGS_OTEL_ENABLED', false)
352-
}
353-
if (this.DD_METRICS_OTEL_ENABLED) {
354-
log.warn('@opentelemetry/api is not installed; disabling DD_METRICS_OTEL_ENABLED')
355-
setAndTrack(this, 'DD_METRICS_OTEL_ENABLED', false)
351+
for (const name of ['DD_LOGS_OTEL_ENABLED', 'DD_METRICS_OTEL_ENABLED']) {
352+
if (this[name]) {
353+
if (trackedConfigOrigins.has(name)) {
354+
log.warn('@opentelemetry/api is not installed; disabling %s', name)
355+
}
356+
setAndTrack(this, name, false)
357+
}
356358
}
357359
}
358360
// Disable log injection when OTEL logs are enabled

packages/dd-trace/test/config/index.spec.js

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,10 @@ describe('Config', () => {
5858
const {
5959
ddMajor = DD_MAJOR,
6060
otelApi = require('../../src/opentelemetry/api'),
61+
// Merged into the resolved defaults to exercise a default-on flag (the "if we
62+
// change a default later" path) through the real #applyDefaults, which sets
63+
// defaults untracked.
64+
defaultsOverride,
6165
} = overrides
6266

6367
log = proxyquire('../../src/log', {})
@@ -72,6 +76,7 @@ describe('Config', () => {
7276
'./parsers': parsers,
7377
'../../../../version': { DD_MAJOR: ddMajor },
7478
})
79+
if (defaultsOverride) Object.assign(configDefaults.defaults, defaultsOverride)
7580
const configHelper = proxyquire.noPreserveCache()('../../src/config/helper', {
7681
'./supported-configurations.json': supportedConfigurations,
7782
})
@@ -411,6 +416,32 @@ describe('Config', () => {
411416
assert.strictEqual(config.DD_LOGS_OTEL_ENABLED, true)
412417
})
413418

419+
it('silently disables a default-on OTel flag when @opentelemetry/api is not installed', () => {
420+
const config = getConfig({}, {
421+
otelApi: { isAvailable: () => false },
422+
defaultsOverride: { DD_METRICS_OTEL_ENABLED: true, DD_LOGS_OTEL_ENABLED: true },
423+
})
424+
425+
assert.strictEqual(config.DD_METRICS_OTEL_ENABLED, false)
426+
assert.strictEqual(config.DD_LOGS_OTEL_ENABLED, false)
427+
assert.ok(log.warn.getCalls().every((call) => !/@opentelemetry\/api is not installed/.test(call.args[0])))
428+
})
429+
430+
it('warns only for the customer-set flag when a sibling is default-on', () => {
431+
process.env.DD_LOGS_OTEL_ENABLED = 'true'
432+
433+
const config = getConfig({}, {
434+
otelApi: { isAvailable: () => false },
435+
defaultsOverride: { DD_METRICS_OTEL_ENABLED: true },
436+
})
437+
438+
assert.strictEqual(config.DD_LOGS_OTEL_ENABLED, false)
439+
assert.strictEqual(config.DD_METRICS_OTEL_ENABLED, false)
440+
const warnings = log.warn.getCalls().filter((call) => /@opentelemetry\/api is not installed/.test(call.args[0]))
441+
assert.strictEqual(warnings.length, 1)
442+
assert.ok(warnings[0].args.includes('DD_LOGS_OTEL_ENABLED'))
443+
})
444+
414445
it('should initialize with OTEL environment variables when DD env vars are not set', () => {
415446
process.env.OTEL_SERVICE_NAME = 'otel_service'
416447
process.env.OTEL_LOG_LEVEL = 'debug'

0 commit comments

Comments
 (0)