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

feat(api): Optionally suppress warning about logger being overwritten #3366

Merged
Merged
9 changes: 7 additions & 2 deletions api/src/api/diag.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,10 @@ import {

const API_NAME = 'diag';

interface LoggerOptions {
suppressOverrideMessage?: boolean;
}

/**
* Singleton object which represents the entry point to the OpenTelemetry internal
* diagnostic API
Expand Down Expand Up @@ -67,7 +71,8 @@ export class DiagAPI implements DiagLogger {

self.setLogger = (
logger: DiagLogger,
logLevel: DiagLogLevel = DiagLogLevel.INFO
logLevel: DiagLogLevel = DiagLogLevel.INFO,
options: LoggerOptions = {}
Copy link
Member

Choose a reason for hiding this comment

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

Copy link
Member

Choose a reason for hiding this comment

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

Wonder if we want to add a third parameter or make the second parameter an overload?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

updated the type

Copy link
Contributor Author

Choose a reason for hiding this comment

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

happy to make it an overload if you want? 🙂

Copy link
Member

Choose a reason for hiding this comment

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

We are avoiding overloading API methods that need to be implemented in SDK libraries. However, this method is not part of an interface that needs to be implemented in SDKs.

So I believe it would be intuitive to be overloaded as an option bag.

Copy link
Member

@dyladan dyladan Nov 2, 2022

Choose a reason for hiding this comment

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

the self.logger use is done there intentionally to aid with code minification. what help do you need?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

if you revert the last commit, you can see the typescript errors

Copy link
Member

Choose a reason for hiding this comment

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

if you revert the last commit, you can see the typescript errors

The compilation errors can be suppressed if the two method declarations are merged into a single one, e.g.

  /**
   * Set the global DiagLogger and DiagLogLevel.
   * If a global diag logger is already set, this will override it.
   *
   * @param logger - [Optional] The DiagLogger instance to set as the default logger.
   * @param logLevelOrOptions - [Optional] And object which can contain `logLevel: DiagLogLevel` used to filter logs sent to the logger. If not provided it will default to INFO. The object may also contain `suppressOverrideMessage: boolean` which will suppress the warning normally logged when a logger is already registered.
   * @returns true if the logger was successfully registered, else false
   */
  public setLogger!: (logger: DiagLogger, logLevelOrOptions?: DiagLogLevel | LoggerOptions) => boolean;

As for why those methods need to be defined in this non-conventional way, I traced back to #1880 and didn't find an explanation. Maybe @MSNev can chime in and share more about it?

Copy link
Contributor

Choose a reason for hiding this comment

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

Ok, I've blocked this PR as we should NOT convert the property into a function as this becomes less compressible through minification.

And then will cause me to re-implement this original change again in the sandbox to have it contributed back here.

Copy link
Contributor

Choose a reason for hiding this comment

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

This pattern has major implications on the overall browser bundle sizes (depending on the module format and ES level being targetted). And this is one of the techniques that I WILL be investigating the viability and performing across the entire JS and Contrib codebases in the sandbox (when I get it going -- HOPEFULLY shortly)

) => {
if (logger === self) {
// There isn't much we can do here.
Expand All @@ -83,7 +88,7 @@ export class DiagAPI implements DiagLogger {
const oldLogger = getGlobal('diag');
const newLogger = createLogLevelDiagLogger(logLevel, logger);
// There already is an logger registered. We'll let it know before overwriting it.
if (oldLogger) {
if (oldLogger && !options.suppressOverrideMessage) {
const stack = new Error().stack ?? '<failed to generate stacktrace>';
oldLogger.warn(`Current logger will be overwritten from ${stack}`);
newLogger.warn(
Expand Down