Skip to content
This repository has been archived by the owner on Nov 10, 2022. It is now read-only.

Commit

Permalink
feat: debug log global registrations and logger overwrites
Browse files Browse the repository at this point in the history
  • Loading branch information
rauno56 committed May 11, 2021
1 parent 9b3f9c7 commit e987ff2
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 8 deletions.
14 changes: 9 additions & 5 deletions src/api/diag.ts
Expand Up @@ -79,11 +79,15 @@ export class DiagAPI implements DiagLogger {
return false;
}

return registerGlobal(
'diag',
createLogLevelDiagLogger(logLevel, logger),
true
);
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) {
oldLogger.warn('Current logger will be overwritten');
newLogger.warn('Current logger will overwrite one already registered');
}

return registerGlobal('diag', newLogger, true);
};

self.disable = () => {
Expand Down
12 changes: 9 additions & 3 deletions src/internal/global-utils.ts
Expand Up @@ -35,13 +35,16 @@ export function registerGlobal<Type extends keyof OTelGlobalAPI>(
instance: OTelGlobalAPI[Type],
allowOverride = false
): boolean {
_global[GLOBAL_OPENTELEMETRY_API_KEY] = _global[
diag.debug(
`@opentelemetry/api: Registering a global for ${type} v${VERSION}.`
);

const api = (_global[GLOBAL_OPENTELEMETRY_API_KEY] = _global[
GLOBAL_OPENTELEMETRY_API_KEY
] ?? {
version: VERSION,
};
});

const api = _global[GLOBAL_OPENTELEMETRY_API_KEY]!;
if (!allowOverride && api[type]) {
// already registered an API of this type
const err = new Error(
Expand Down Expand Up @@ -75,6 +78,9 @@ export function getGlobal<Type extends keyof OTelGlobalAPI>(
}

export function unregisterGlobal(type: keyof OTelGlobalAPI) {
diag.debug(
`@opentelemetry/api: Unregistering a global for ${type} v${VERSION}.`
);
const api = _global[GLOBAL_OPENTELEMETRY_API_KEY];

if (api) {
Expand Down

0 comments on commit e987ff2

Please sign in to comment.