From 8bb4948ae14c9c70c016380a5f3653b587d0b599 Mon Sep 17 00:00:00 2001 From: Marc Pichler Date: Tue, 21 Mar 2023 10:53:33 +0100 Subject: [PATCH] fix(sdk-node): only set DiagConsoleLogger when OTEL_LOG_LEVEL is set --- .../packages/opentelemetry-sdk-node/src/sdk.ts | 11 ++++++++--- .../opentelemetry-sdk-node/test/sdk.test.ts | 13 +++++++++++++ 2 files changed, 21 insertions(+), 3 deletions(-) diff --git a/experimental/packages/opentelemetry-sdk-node/src/sdk.ts b/experimental/packages/opentelemetry-sdk-node/src/sdk.ts index dc9244f3e4f..07921a88b90 100644 --- a/experimental/packages/opentelemetry-sdk-node/src/sdk.ts +++ b/experimental/packages/opentelemetry-sdk-node/src/sdk.ts @@ -47,7 +47,7 @@ import { import { SemanticResourceAttributes } from '@opentelemetry/semantic-conventions'; import { NodeSDKConfiguration } from './types'; import { TracerProviderWithEnvExporters } from './TracerProviderWithEnvExporter'; -import { getEnv } from '@opentelemetry/core'; +import { getEnv, getEnvWithoutDefaults } from '@opentelemetry/core'; import { parseInstrumentationOptions } from './utils'; /** This class represents everything needed to register a fully configured OpenTelemetry Node.js SDK */ @@ -89,14 +89,19 @@ export class NodeSDK { */ public constructor(configuration: Partial = {}) { const env = getEnv(); + const envWithoutDefaults = getEnvWithoutDefaults(); + if (env.OTEL_SDK_DISABLED) { this._disabled = true; // Functions with possible side-effects are set // to no-op via the _disabled flag } - if (env.OTEL_LOG_LEVEL) { + + // Default is INFO, use environment without defaults to check + // if the user originally set the environment variable. + if (envWithoutDefaults.OTEL_LOG_LEVEL) { diag.setLogger(new DiagConsoleLogger(), { - logLevel: env.OTEL_LOG_LEVEL, + logLevel: envWithoutDefaults.OTEL_LOG_LEVEL, }); } diff --git a/experimental/packages/opentelemetry-sdk-node/test/sdk.test.ts b/experimental/packages/opentelemetry-sdk-node/test/sdk.test.ts index 5fcdb7f94d5..7a385a7604d 100644 --- a/experimental/packages/opentelemetry-sdk-node/test/sdk.test.ts +++ b/experimental/packages/opentelemetry-sdk-node/test/sdk.test.ts @@ -134,6 +134,19 @@ describe('Node SDK', () => { delete env.OTEL_LOG_LEVEL; }); + it('should not register a diag logger with OTEL_LOG_LEVEL unset', () => { + delete env.OTEL_LOG_LEVEL; + + const spy = Sinon.spy(diag, 'setLogger'); + const sdk = new NodeSDK({ + autoDetectResources: false, + }); + + sdk.start(); + + assert.strictEqual(spy.callCount, 0); + }); + it('should register a tracer provider if an exporter is provided', async () => { const sdk = new NodeSDK({ traceExporter: new ConsoleSpanExporter(),