Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[sdk-logs] remove includeTraceContext configuration and use LogRecord context when available #3817

Merged
merged 9 commits into from
May 23, 2023
1 change: 1 addition & 0 deletions experimental/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,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): logger includeTraceContext setting not defaulting to true [#3817](https://github.com/open-telemetry/opentelemetry-js/pull/3817) @hectorhdzg
hectorhdzg marked this conversation as resolved.
Show resolved Hide resolved

### :books: (Refine Doc)

Expand Down
4 changes: 1 addition & 3 deletions experimental/packages/sdk-logs/src/Logger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
1 change: 0 additions & 1 deletion experimental/packages/sdk-logs/src/LoggerProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,6 @@ export class LoggerProvider implements logsAPI.LoggerProvider {
{ name: loggerName, version, schemaUrl: options?.schemaUrl },
{
logRecordLimits: this._config.logRecordLimits,
includeTraceContext: options?.includeTraceContext,
},
this
)
Expand Down
1 change: 1 addition & 0 deletions experimental/packages/sdk-logs/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

export {
LoggerConfig,
LoggerProviderConfig,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unrelated change?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry about that, I can send a different PR for this if needed, trying to avoid a long import for the config
import { LoggerProviderConfig } from "@opentelemetry/sdk-logs/build/src/types";

Copy link
Contributor

@MSNev MSNev May 18, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should not really be importing from specific folders in a package "@opentelemetry/sdk-logs/build/src/types" as this makes it very fragile. And I have had issues from some teams where this didn't work for them (it was a loooong time ago) when we accidently had this in our internal code.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree we should be avoiding it

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't see it actually used anywhere in this PR though?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh end users. Ignore me

LogRecordLimits,
BufferConfig,
BatchLogRecordProcessorBrowserConfig,
Expand Down
3 changes: 0 additions & 3 deletions experimental/packages/sdk-logs/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,6 @@ export interface LoggerProviderConfig {
export interface LoggerConfig {
/** Log Record Limits*/
logRecordLimits?: LogRecordLimits;

/** include Trace Context */
includeTraceContext?: boolean;
}

export interface LogRecordLimits {
Expand Down
36 changes: 18 additions & 18 deletions experimental/packages/sdk-logs/test/common/Logger.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 * as logsAPI from '@opentelemetry/api-logs';
hectorhdzg marked this conversation as resolved.
Show resolved Hide resolved

const setup = (loggerConfig: LoggerConfig = {}) => {
const logger = new Logger(
Expand All @@ -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', () => {
Expand All @@ -69,22 +61,30 @@ 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',
});
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 specifie in LogRecord', () => {
hectorhdzg marked this conversation as resolved.
Show resolved Hide resolved
const { logger } = setup({});
const spanContext = {
traceId: 'd4cda95b652f4a1592b449d5929fda1b',
spanId: '6e0c63257de34c92',
traceFlags: TraceFlags.SAMPLED,
};
const activeContext = trace.setSpanContext(ROOT_CONTEXT, spanContext);
const logRecordData: logsAPI.LogRecord = {
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));
});
});
});