From 2096fdffd78b00cf80f14df13cf974152d4246a0 Mon Sep 17 00:00:00 2001 From: Daniel Dyla Date: Wed, 7 Apr 2021 16:18:08 -0400 Subject: [PATCH] feat!: return boolean success value from setGlobalXXX methods --- src/api/context.ts | 11 +++++------ src/api/diag.ts | 12 ++++++++---- src/api/propagation.ts | 9 +++++---- src/api/trace.ts | 9 +++++---- src/internal/global-utils.ts | 7 ++++--- 5 files changed, 27 insertions(+), 21 deletions(-) diff --git a/src/api/context.ts b/src/api/context.ts index d6ea2445..34a1b806 100644 --- a/src/api/context.ts +++ b/src/api/context.ts @@ -44,13 +44,12 @@ export class ContextAPI { } /** - * Set the current context manager. Returns the initialized context manager + * Set the current context manager. + * + * @returns true if the context manager was successfully registered, else false */ - public setGlobalContextManager( - contextManager: ContextManager - ): ContextManager { - registerGlobal(API_NAME, contextManager); - return contextManager; + public setGlobalContextManager(contextManager: ContextManager): boolean { + return registerGlobal(API_NAME, contextManager); } /** diff --git a/src/api/diag.ts b/src/api/diag.ts index a1ba6061..b3136c96 100644 --- a/src/api/diag.ts +++ b/src/api/diag.ts @@ -76,10 +76,14 @@ export class DiagAPI implements DiagLogger { 'Cannot use diag as the logger for itself. Please use a DiagLogger implementation like ConsoleDiagLogger or a custom implementation' ); self.error(err.stack ?? err.message); - return; + return false; } - registerGlobal('diag', createLogLevelDiagLogger(logLevel, logger), true); + return registerGlobal( + 'diag', + createLogLevelDiagLogger(logLevel, logger), + true + ); }; self.disable = () => { @@ -99,9 +103,9 @@ export class DiagAPI implements DiagLogger { * * @param logger - [Optional] The DiagLogger instance to set as the default logger. * @param logLevel - [Optional] The DiagLogLevel used to filter logs sent to the logger. If not provided it will default to INFO. - * @returns The previously registered DiagLogger + * @returns true if the logger was successfully registered, else false */ - public setLogger!: (logger: DiagLogger, logLevel?: DiagLogLevel) => void; + public setLogger!: (logger: DiagLogger, logLevel?: DiagLogLevel) => boolean; // DiagLogger implementation public verbose!: DiagLogFunction; diff --git a/src/api/propagation.ts b/src/api/propagation.ts index ee6394d2..d83d4e7d 100644 --- a/src/api/propagation.ts +++ b/src/api/propagation.ts @@ -50,11 +50,12 @@ export class PropagationAPI { } /** - * Set the current propagator. Returns the initialized propagator + * Set the current propagator. + * + * @returns true if the propagator was successfully registered, else false */ - public setGlobalPropagator(propagator: TextMapPropagator): TextMapPropagator { - registerGlobal(API_NAME, propagator); - return propagator; + public setGlobalPropagator(propagator: TextMapPropagator): boolean { + return registerGlobal(API_NAME, propagator); } /** diff --git a/src/api/trace.ts b/src/api/trace.ts index f9beba0c..f2f083af 100644 --- a/src/api/trace.ts +++ b/src/api/trace.ts @@ -47,12 +47,13 @@ export class TraceAPI { } /** - * Set the current global tracer. Returns the initialized global tracer provider + * Set the current global tracer. + * + * @returns true if the tracer provider was successfully registered, else false */ - public setGlobalTracerProvider(provider: TracerProvider): TracerProvider { + public setGlobalTracerProvider(provider: TracerProvider): boolean { this._proxyTracerProvider.setDelegate(provider); - registerGlobal(API_NAME, this._proxyTracerProvider); - return this._proxyTracerProvider; + return registerGlobal(API_NAME, this._proxyTracerProvider); } /** diff --git a/src/internal/global-utils.ts b/src/internal/global-utils.ts index 3a3579e1..5115c762 100644 --- a/src/internal/global-utils.ts +++ b/src/internal/global-utils.ts @@ -34,7 +34,7 @@ export function registerGlobal( type: Type, instance: OTelGlobalAPI[Type], allowOverride = false -): void { +): boolean { _global[GLOBAL_OPENTELEMETRY_API_KEY] = _global[ GLOBAL_OPENTELEMETRY_API_KEY ] ?? { @@ -48,7 +48,7 @@ export function registerGlobal( `@opentelemetry/api: Attempted duplicate registration of API: ${type}` ); diag.error(err.stack || err.message); - return; + return false; } if (api.version !== VERSION) { @@ -57,10 +57,11 @@ export function registerGlobal( '@opentelemetry/api: All API registration versions must match' ); diag.error(err.stack || err.message); - return; + return false; } api[type] = instance; + return true; } export function getGlobal(