Skip to content

Commit

Permalink
fix: restore OTEL_LOG_LEVEL after starting NodeSDK (#24)
Browse files Browse the repository at this point in the history
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
  • Loading branch information
trentm authored Jan 24, 2024
1 parent df6c0ab commit 1af0c68
Show file tree
Hide file tree
Showing 6 changed files with 89 additions and 3 deletions.
2 changes: 2 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 0 additions & 2 deletions packages/opentelemetry-node/lib/logging.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
13 changes: 12 additions & 1 deletion packages/opentelemetry-node/lib/sdk.js
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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;
}
Expand Down
1 change: 1 addition & 0 deletions packages/opentelemetry-node/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
65 changes: 65 additions & 0 deletions packages/opentelemetry-node/test/OTEL_LOG_LEVEL.test.js
Original file line number Diff line number Diff line change
@@ -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();
});
9 changes: 9 additions & 0 deletions packages/opentelemetry-node/test/fixtures/use-diag.js
Original file line number Diff line number Diff line change
@@ -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);

0 comments on commit 1af0c68

Please sign in to comment.