From 1af0c68d967416380888733edf199995d092a97b Mon Sep 17 00:00:00 2001 From: Trent Mick Date: Wed, 24 Jan 2024 12:05:15 -0800 Subject: [PATCH] fix: restore OTEL_LOG_LEVEL after starting NodeSDK (#24) In case there is user code that uses the envvar. It is removed from the env temporarily when creating the NodeSDK so it doesn't use the value and override the 'diag' set by ElasticNodeSDK --- package-lock.json | 2 + packages/opentelemetry-node/lib/logging.js | 2 - packages/opentelemetry-node/lib/sdk.js | 13 +++- packages/opentelemetry-node/package.json | 1 + .../test/OTEL_LOG_LEVEL.test.js | 65 +++++++++++++++++++ .../test/fixtures/use-diag.js | 9 +++ 6 files changed, 89 insertions(+), 3 deletions(-) create mode 100644 packages/opentelemetry-node/test/OTEL_LOG_LEVEL.test.js create mode 100644 packages/opentelemetry-node/test/fixtures/use-diag.js diff --git a/package-lock.json b/package-lock.json index 9454161e..674873ab 100644 --- a/package-lock.json +++ b/package-lock.json @@ -4059,6 +4059,7 @@ "devDependencies": { "@elastic/mockotlpserver": "*", "@types/tape": "^5.6.4", + "@opentelemetry/api": "^1.7.0", "module-details-from-path": "^1.0.3", "semver": "^7.5.4", "tape": "^5.7.2" @@ -4489,6 +4490,7 @@ "version": "file:packages/opentelemetry-node", "requires": { "@elastic/mockotlpserver": "*", + "@opentelemetry/api": "^1.7.0", "@opentelemetry/exporter-metrics-otlp-proto": "^0.47.0", "@opentelemetry/host-metrics": "^0.34.1", "@opentelemetry/instrumentation-http": "^0.47.0", diff --git a/packages/opentelemetry-node/lib/logging.js b/packages/opentelemetry-node/lib/logging.js index 73475cd2..74d58e2b 100644 --- a/packages/opentelemetry-node/lib/logging.js +++ b/packages/opentelemetry-node/lib/logging.js @@ -35,8 +35,6 @@ function setupLogger() { const otelLogLevel = process.env.OTEL_LOG_LEVEL.toUpperCase(); level = luggiteLevelFromOtelLogLevel(otelLogLevel); diagLevel = otelLogLevel; - // Make sure NodeSDK doesn't see this envvar and overwrite our diag logger. - delete process.env.OTEL_LOG_LEVEL; } if (!level) { level = 'info'; // default level diff --git a/packages/opentelemetry-node/lib/sdk.js b/packages/opentelemetry-node/lib/sdk.js index 152c5938..fd0800c4 100644 --- a/packages/opentelemetry-node/lib/sdk.js +++ b/packages/opentelemetry-node/lib/sdk.js @@ -24,8 +24,15 @@ class ElasticNodeSDK extends NodeSDK { // Ensure this envvar is set to avoid a diag.warn() in NodeSDK. process.env.OTEL_TRACES_EXPORTER = 'otlp'; } + const envToRestore = {}; + if ('OTEL_LOG_LEVEL' in process.env) { + envToRestore['OTEL_LOG_LEVEL'] = process.env.OTEL_LOG_LEVEL; + // Make sure NodeSDK doesn't see this envvar and overwrite our diag + // logger. It is restored below. + delete process.env.OTEL_LOG_LEVEL; + } - // TODO accept serviceName, detect service name + // TODO detect service name // - NodeSDK defaults to `TracerProviderWithEnvExporters` if neither // `spanProcessor` nor `traceExporter` are passed in. @@ -69,6 +76,10 @@ class ElasticNodeSDK extends NodeSDK { const configuration = Object.assign(defaultConfig, opts); super(configuration); + Object.keys(envToRestore).forEach((k) => { + process.env[k] = envToRestore[k]; + }); + this._metricsDisabled = metricsDisabled; this._log = log; } diff --git a/packages/opentelemetry-node/package.json b/packages/opentelemetry-node/package.json index 86c36547..f732b95d 100644 --- a/packages/opentelemetry-node/package.json +++ b/packages/opentelemetry-node/package.json @@ -42,6 +42,7 @@ "devDependencies": { "@elastic/mockotlpserver": "*", "@types/tape": "^5.6.4", + "@opentelemetry/api": "^1.3.0", "module-details-from-path": "^1.0.3", "semver": "^7.5.4", "tape": "^5.7.2" diff --git a/packages/opentelemetry-node/test/OTEL_LOG_LEVEL.test.js b/packages/opentelemetry-node/test/OTEL_LOG_LEVEL.test.js new file mode 100644 index 00000000..dbdb7f78 --- /dev/null +++ b/packages/opentelemetry-node/test/OTEL_LOG_LEVEL.test.js @@ -0,0 +1,65 @@ +// Test that usage of `OTEL_LOG_LEVEL` works as expected. + +const {test} = require('tape'); +const {runTestFixtures} = require('./testutils'); + +const testFixtures = [ + { + name: 'diag default', + args: ['./fixtures/use-diag.js'], + cwd: __dirname, + env: { + NODE_OPTIONS: '--require=../start.js', + }, + // verbose: true, + checkResult: (t, err, stdout, stderr) => { + t.error(err); + t.ok(/hi at info/.test(stdout), 'info'); + t.ok(/hi at warn/.test(stdout), 'warn'); + t.ok(/hi at error/.test(stdout), 'error'); + t.ok(/OTEL_LOG_LEVEL: undefined/.test(stdout), 'envvar'); + }, + }, + { + name: 'diag OTEL_LOG_LEVEL=debug', + args: ['./fixtures/use-diag.js'], + cwd: __dirname, + env: { + NODE_OPTIONS: '--require=../start.js', + OTEL_LOG_LEVEL: 'debug', + }, + // verbose: true, + checkResult: (t, err, stdout, stderr) => { + t.error(err); + t.ok(/hi at debug/.test(stdout), 'debug'); + t.ok(/hi at info/.test(stdout), 'info'); + t.ok(/hi at warn/.test(stdout), 'warn'); + t.ok(/hi at error/.test(stdout), 'error'); + t.ok(/OTEL_LOG_LEVEL: debug/.test(stdout), 'envvar'); + }, + }, + { + name: 'diag OTEL_LOG_LEVEL=VerBoSe (allow any case)', + args: ['./fixtures/use-diag.js'], + cwd: __dirname, + env: { + NODE_OPTIONS: '--require=../start.js', + OTEL_LOG_LEVEL: 'VerBoSe', + }, + // verbose: true, + checkResult: (t, err, stdout, stderr) => { + t.error(err); + t.ok(/hi at verbose/.test(stdout), 'verbose'); + t.ok(/hi at debug/.test(stdout), 'debug'); + t.ok(/hi at info/.test(stdout), 'info'); + t.ok(/hi at warn/.test(stdout), 'warn'); + t.ok(/hi at error/.test(stdout), 'error'); + t.ok(/OTEL_LOG_LEVEL: VerBoSe/.test(stdout), 'envvar'); + }, + }, +]; + +test('OTEL_LOG_LEVEL', (suite) => { + runTestFixtures(suite, testFixtures); + suite.end(); +}); diff --git a/packages/opentelemetry-node/test/fixtures/use-diag.js b/packages/opentelemetry-node/test/fixtures/use-diag.js new file mode 100644 index 00000000..3f17db37 --- /dev/null +++ b/packages/opentelemetry-node/test/fixtures/use-diag.js @@ -0,0 +1,9 @@ +const {diag} = require('@opentelemetry/api'); + +diag.verbose('hi at verbose'); +diag.debug('hi at debug'); +diag.info('hi at info'); +diag.warn('hi at warn'); +diag.error('hi at error'); + +console.log('OTEL_LOG_LEVEL:', process.env.OTEL_LOG_LEVEL);