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
21 changes: 12 additions & 9 deletions experimental/packages/sdk-logs/src/LoggerProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import { NOOP_LOGGER } from '@opentelemetry/api-logs';
import { IResource, Resource } from '@opentelemetry/resources';
import { BindOnceFuture, merge } from '@opentelemetry/core';

import type { LoggerProviderConfig } from './types';
import type { LoggerConfig, LoggerProviderConfig } from './types';
import type { LogRecordProcessor } from './LogRecordProcessor';
import { Logger } from './Logger';
import { loadDefaultConfig, reconfigureLimits } from './config';
Expand Down Expand Up @@ -64,8 +64,8 @@ export class LoggerProvider implements logsAPI.LoggerProvider {
*/
public getLogger(
name: string,
version?: string,
options?: logsAPI.LoggerOptions
version = '',
options: logsAPI.LoggerOptions = {}
): logsAPI.Logger {
if (this._shutdownOnce.isCalled) {
diag.warn('A shutdown LoggerProvider cannot provide a Logger');
Expand All @@ -76,16 +76,19 @@ export class LoggerProvider implements logsAPI.LoggerProvider {
diag.warn('Logger requested without instrumentation scope name.');
}
const loggerName = name || DEFAULT_LOGGER_NAME;
const key = `${loggerName}@${version || ''}:${options?.schemaUrl || ''}`;
const key = `${loggerName}@${version || ''}:${options.schemaUrl || ''}`;
if (!this._loggers.has(key)) {
const loggerConfig: LoggerConfig = {
logRecordLimits: this._config.logRecordLimits,
};
if (options.includeTraceContext) {
loggerConfig.includeTraceContext = options.includeTraceContext;
}
this._loggers.set(
key,
new Logger(
{ name: loggerName, version, schemaUrl: options?.schemaUrl },
{
logRecordLimits: this._config.logRecordLimits,
includeTraceContext: options?.includeTraceContext,
},
{ name: loggerName, version, schemaUrl: options.schemaUrl },
loggerConfig,
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
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,15 @@ describe('LoggerProvider', () => {
assert.ok(logger2 instanceof Logger);
assert.ok(logger1 === logger2);
});

it('should create a logger and default configuration should be preserved if custom one is not provided', () => {
const provider = new LoggerProvider();
const logger = provider.getLogger('') as Logger;
assert.ok(
logger['_loggerConfig'].includeTraceContext ===
loadDefaultConfig().includeTraceContext
);
});
});

describe('addLogRecordProcessor', () => {
Expand Down