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

feat!: return boolean success value from setGlobalXXX methods #32

Merged
merged 2 commits into from
Apr 8, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 5 additions & 6 deletions src/api/context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

/**
Expand Down
12 changes: 8 additions & 4 deletions src/api/diag.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 = () => {
Expand All @@ -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;
Expand Down
9 changes: 5 additions & 4 deletions src/api/propagation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

/**
Expand Down
9 changes: 5 additions & 4 deletions src/api/trace.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

/**
Expand Down
7 changes: 4 additions & 3 deletions src/internal/global-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ export function registerGlobal<Type extends keyof OTelGlobalAPI>(
type: Type,
instance: OTelGlobalAPI[Type],
allowOverride = false
): void {
): boolean {
_global[GLOBAL_OPENTELEMETRY_API_KEY] = _global[
GLOBAL_OPENTELEMETRY_API_KEY
] ?? {
Expand All @@ -48,7 +48,7 @@ export function registerGlobal<Type extends keyof OTelGlobalAPI>(
`@opentelemetry/api: Attempted duplicate registration of API: ${type}`
);
diag.error(err.stack || err.message);
return;
return false;
}

if (api.version !== VERSION) {
Expand All @@ -57,10 +57,11 @@ export function registerGlobal<Type extends keyof OTelGlobalAPI>(
'@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<Type extends keyof OTelGlobalAPI>(
Expand Down