diff --git a/experimental/CHANGELOG.md b/experimental/CHANGELOG.md index 1a5a6595cb..4ef6fff619 100644 --- a/experimental/CHANGELOG.md +++ b/experimental/CHANGELOG.md @@ -17,6 +17,7 @@ All notable changes to experimental packages in this project will be documented ### :bug: (Bug Fix) * fix(sdk-node): use resource interface instead of concrete class [#3803](https://github.com/open-telemetry/opentelemetry-js/pull/3803) @blumamir +* fix(sdk-logs): remove includeTraceContext configuration and use LogRecord context when available [#3817](https://github.com/open-telemetry/opentelemetry-js/pull/3817) @hectorhdzg ### :books: (Refine Doc) diff --git a/experimental/packages/sdk-logs/src/Logger.ts b/experimental/packages/sdk-logs/src/Logger.ts index 0e6cfbe2d9..5ea4e8f28f 100644 --- a/experimental/packages/sdk-logs/src/Logger.ts +++ b/experimental/packages/sdk-logs/src/Logger.ts @@ -39,9 +39,7 @@ export class Logger implements logsAPI.Logger { } public emit(logRecord: logsAPI.LogRecord): void { - const currentContext = this._loggerConfig.includeTraceContext - ? context.active() - : undefined; + const currentContext = logRecord.context || context.active(); /** * If a Logger was obtained with include_trace_context=true, * the LogRecords it emits MUST automatically include the Trace Context from the active Context, diff --git a/experimental/packages/sdk-logs/src/LoggerProvider.ts b/experimental/packages/sdk-logs/src/LoggerProvider.ts index a51438408a..81515dab86 100644 --- a/experimental/packages/sdk-logs/src/LoggerProvider.ts +++ b/experimental/packages/sdk-logs/src/LoggerProvider.ts @@ -84,7 +84,6 @@ export class LoggerProvider implements logsAPI.LoggerProvider { { name: loggerName, version, schemaUrl: options?.schemaUrl }, { logRecordLimits: this._config.logRecordLimits, - includeTraceContext: options?.includeTraceContext, }, this ) diff --git a/experimental/packages/sdk-logs/src/index.ts b/experimental/packages/sdk-logs/src/index.ts index 60124da1f0..e718ae069e 100644 --- a/experimental/packages/sdk-logs/src/index.ts +++ b/experimental/packages/sdk-logs/src/index.ts @@ -16,6 +16,7 @@ export { LoggerConfig, + LoggerProviderConfig, LogRecordLimits, BufferConfig, BatchLogRecordProcessorBrowserConfig, diff --git a/experimental/packages/sdk-logs/src/types.ts b/experimental/packages/sdk-logs/src/types.ts index 5bdad2a1e0..026843dc15 100644 --- a/experimental/packages/sdk-logs/src/types.ts +++ b/experimental/packages/sdk-logs/src/types.ts @@ -33,9 +33,6 @@ export interface LoggerProviderConfig { export interface LoggerConfig { /** Log Record Limits*/ logRecordLimits?: LogRecordLimits; - - /** include Trace Context */ - includeTraceContext?: boolean; } export interface LogRecordLimits { diff --git a/experimental/packages/sdk-logs/test/common/Logger.test.ts b/experimental/packages/sdk-logs/test/common/Logger.test.ts index 5df756c901..a5f690a4b7 100644 --- a/experimental/packages/sdk-logs/test/common/Logger.test.ts +++ b/experimental/packages/sdk-logs/test/common/Logger.test.ts @@ -18,8 +18,8 @@ import * as assert from 'assert'; import * as sinon from 'sinon'; import { LogRecord, Logger, LoggerConfig, LoggerProvider } from '../../src'; -import { loadDefaultConfig } from '../../src/config'; -import { context } from '@opentelemetry/api'; +import { ROOT_CONTEXT, TraceFlags, context, trace } from '@opentelemetry/api'; +import { LogRecord as ApiLogRecord } from '@opentelemetry/api-logs'; const setup = (loggerConfig: LoggerConfig = {}) => { const logger = new Logger( @@ -40,14 +40,6 @@ describe('Logger', () => { const { logger } = setup(); assert.ok(logger instanceof Logger); }); - - it('should a default value with config.includeTraceContext', () => { - const { logger } = setup(); - assert.ok( - logger['_loggerConfig'].includeTraceContext === - loadDefaultConfig().includeTraceContext - ); - }); }); describe('emit', () => { @@ -69,8 +61,8 @@ describe('Logger', () => { assert.ok(makeOnlySpy.called); }); - it('should emit with current Context when includeTraceContext is true', () => { - const { logger } = setup({ includeTraceContext: true }); + it('should emit with current Context', () => { + const { logger } = setup({}); const callSpy = sinon.spy(logger.getActiveLogRecordProcessor(), 'onEmit'); logger.emit({ body: 'test log body', @@ -78,13 +70,21 @@ describe('Logger', () => { assert.ok(callSpy.calledWith(sinon.match.any, context.active())); }); - it('should emit with empty Context when includeTraceContext is false', () => { - const { logger } = setup({ includeTraceContext: false }); + it('should emit with Context specified in LogRecord', () => { + const { logger } = setup({}); + const spanContext = { + traceId: 'd4cda95b652f4a1592b449d5929fda1b', + spanId: '6e0c63257de34c92', + traceFlags: TraceFlags.SAMPLED, + }; + const activeContext = trace.setSpanContext(ROOT_CONTEXT, spanContext); + const logRecordData: ApiLogRecord = { + context: activeContext, + }; + const callSpy = sinon.spy(logger.getActiveLogRecordProcessor(), 'onEmit'); - logger.emit({ - body: 'test log body', - }); - assert.ok(callSpy.calledWith(sinon.match.any, undefined)); + logger.emit(logRecordData); + assert.ok(callSpy.calledWith(sinon.match.any, activeContext)); }); }); });