Skip to content

Commit

Permalink
fix(sdk-node): only set DiagConsoleLogger when OTEL_LOG_LEVEL is set
Browse files Browse the repository at this point in the history
  • Loading branch information
pichlermarc committed Mar 21, 2023
1 parent ca700c4 commit 8bb4948
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 3 deletions.
11 changes: 8 additions & 3 deletions experimental/packages/opentelemetry-sdk-node/src/sdk.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 */
Expand Down Expand Up @@ -89,14 +89,19 @@ export class NodeSDK {
*/
public constructor(configuration: Partial<NodeSDKConfiguration> = {}) {
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,
});
}

Expand Down
13 changes: 13 additions & 0 deletions experimental/packages/opentelemetry-sdk-node/test/sdk.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand Down

0 comments on commit 8bb4948

Please sign in to comment.