diff --git a/experimental/CHANGELOG.md b/experimental/CHANGELOG.md index 5f14e88c50..c8bddd915c 100644 --- a/experimental/CHANGELOG.md +++ b/experimental/CHANGELOG.md @@ -9,6 +9,7 @@ All notable changes to experimental packages in this project will be documented ### :rocket: (Enhancement) * feat: add HTTP_ROUTE attribute to http incoming metrics if present [#3581](https://github.com/open-telemetry/opentelemetry-js/pull/3581) @hermogenes +* feat(sdk-node): install diag logger with OTEL_LOG_LEVEL [#3627](https://github.com/open-telemetry/opentelemetry-js/pull/3627) @legendecas ### :bug: (Bug Fix) diff --git a/experimental/packages/opentelemetry-sdk-node/README.md b/experimental/packages/opentelemetry-sdk-node/README.md index 0bac781143..693f91ae74 100644 --- a/experimental/packages/opentelemetry-sdk-node/README.md +++ b/experimental/packages/opentelemetry-sdk-node/README.md @@ -143,7 +143,21 @@ Configure the [service name](https://github.com/open-telemetry/opentelemetry-spe Disable the SDK by setting the `OTEL_SDK_DISABLED` environment variable to `true`. -## Configure Trace Exporter from Environment +## Configure log level from the environment + +Set the log level by setting the `OTEL_LOG_LEVEL` environment variable to enums: + +- `NONE`, +- `ERROR`, +- `WARN`, +- `INFO`, +- `DEBUG`, +- `VERBOSE`, +- `ALL`. + +The default level is `INFO`. + +## Configure Trace Exporter from environment This is an alternative to programmatically configuring an exporter or span processor. This package will auto setup the default `otlp` exporter with `http/protobuf` protocol if `traceExporter` or `spanProcessor` hasn't been passed into the `NodeSDK` constructor. diff --git a/experimental/packages/opentelemetry-sdk-node/src/sdk.ts b/experimental/packages/opentelemetry-sdk-node/src/sdk.ts index fa14043cd3..faae12dc7d 100644 --- a/experimental/packages/opentelemetry-sdk-node/src/sdk.ts +++ b/experimental/packages/opentelemetry-sdk-node/src/sdk.ts @@ -14,7 +14,13 @@ * limitations under the License. */ -import { ContextManager, TextMapPropagator, metrics } from '@opentelemetry/api'; +import { + ContextManager, + TextMapPropagator, + metrics, + diag, + DiagConsoleLogger, +} from '@opentelemetry/api'; import { InstrumentationOption, registerInstrumentations, @@ -80,11 +86,17 @@ export class NodeSDK { * Create a new NodeJS SDK instance */ public constructor(configuration: Partial = {}) { - if (getEnv().OTEL_SDK_DISABLED) { + const env = getEnv(); + 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) { + diag.setLogger(new DiagConsoleLogger(), { + logLevel: env.OTEL_LOG_LEVEL, + }); + } this._resource = configuration.resource ?? new Resource({}); this._resourceDetectors = configuration.resourceDetectors ?? [ diff --git a/experimental/packages/opentelemetry-sdk-node/test/sdk.test.ts b/experimental/packages/opentelemetry-sdk-node/test/sdk.test.ts index 60b493435b..fb43901710 100644 --- a/experimental/packages/opentelemetry-sdk-node/test/sdk.test.ts +++ b/experimental/packages/opentelemetry-sdk-node/test/sdk.test.ts @@ -22,6 +22,7 @@ import { diag, DiagLogLevel, metrics, + DiagConsoleLogger, } from '@opentelemetry/api'; import { AsyncHooksContextManager, @@ -68,6 +69,7 @@ describe('Node SDK', () => { let delegate: any; beforeEach(() => { + diag.disable(); context.disable(); trace.disable(); propagation.disable(); @@ -78,6 +80,10 @@ describe('Node SDK', () => { delegate = (trace.getTracerProvider() as ProxyTracerProvider).getDelegate(); }); + afterEach(() => { + Sinon.restore(); + }); + describe('Basic Registration', () => { it('should not register any unconfigured SDK components', async () => { // need to set OTEL_TRACES_EXPORTER to none since default value is otlp @@ -108,6 +114,25 @@ describe('Node SDK', () => { delete env.OTEL_TRACES_EXPORTER; }); + it('should register a diag logger with OTEL_LOG_LEVEL', () => { + env.OTEL_LOG_LEVEL = 'ERROR'; + + const spy = Sinon.spy(diag, 'setLogger'); + const sdk = new NodeSDK({ + autoDetectResources: false, + }); + + sdk.start(); + + assert.strictEqual(spy.callCount, 1); + assert.ok(spy.args[0][0] instanceof DiagConsoleLogger); + assert.deepStrictEqual(spy.args[0][1], { + logLevel: DiagLogLevel.ERROR, + }); + + delete env.OTEL_LOG_LEVEL; + }); + it('should register a tracer provider if an exporter is provided', async () => { const sdk = new NodeSDK({ traceExporter: new ConsoleSpanExporter(),