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

chore: remove circular dependency #96

Merged
merged 1 commit into from
Jun 17, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
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
5 changes: 3 additions & 2 deletions src/api/context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import {
registerGlobal,
unregisterGlobal,
} from '../internal/global-utils';
import { DiagAPI } from './diag';

const API_NAME = 'context';
const NOOP_CONTEXT_MANAGER = new NoopContextManager();
Expand Down Expand Up @@ -49,7 +50,7 @@ export class ContextAPI {
* @returns true if the context manager was successfully registered, else false
*/
public setGlobalContextManager(contextManager: ContextManager): boolean {
return registerGlobal(API_NAME, contextManager);
return registerGlobal(API_NAME, contextManager, DiagAPI.instance());
}

/**
Expand Down Expand Up @@ -93,6 +94,6 @@ export class ContextAPI {
/** Disable and remove the global context manager */
public disable() {
this._getContextManager().disable();
unregisterGlobal(API_NAME);
unregisterGlobal(API_NAME, DiagAPI.instance());
}
}
4 changes: 2 additions & 2 deletions src/api/diag.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,11 +96,11 @@ export class DiagAPI implements DiagLogger {
);
}

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

self.disable = () => {
unregisterGlobal(API_NAME);
unregisterGlobal(API_NAME, self);
};

self.createComponentLogger = (options: ComponentLoggerOptions) => {
Expand Down
5 changes: 3 additions & 2 deletions src/api/propagation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ import {
deleteBaggage,
} from '../baggage/context-helpers';
import { createBaggage } from '../baggage/utils';
import { DiagAPI } from './diag';

const API_NAME = 'propagation';
const NOOP_TEXT_MAP_PROPAGATOR = new NoopTextMapPropagator();
Expand Down Expand Up @@ -62,7 +63,7 @@ export class PropagationAPI {
* @returns true if the propagator was successfully registered, else false
*/
public setGlobalPropagator(propagator: TextMapPropagator): boolean {
return registerGlobal(API_NAME, propagator);
return registerGlobal(API_NAME, propagator, DiagAPI.instance());
}

/**
Expand Down Expand Up @@ -104,7 +105,7 @@ export class PropagationAPI {

/** Remove the global propagator */
public disable() {
unregisterGlobal(API_NAME);
unregisterGlobal(API_NAME, DiagAPI.instance());
}

public createBaggage = createBaggage;
Expand Down
9 changes: 7 additions & 2 deletions src/api/trace.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ import {
setSpan,
setSpanContext,
} from '../trace/context-utils';
import { DiagAPI } from './diag';

const API_NAME = 'trace';

Expand Down Expand Up @@ -63,7 +64,11 @@ export class TraceAPI {
*/
public setGlobalTracerProvider(provider: TracerProvider): boolean {
this._proxyTracerProvider.setDelegate(provider);
return registerGlobal(API_NAME, this._proxyTracerProvider);
return registerGlobal(
API_NAME,
this._proxyTracerProvider,
DiagAPI.instance()
);
}

/**
Expand All @@ -82,7 +87,7 @@ export class TraceAPI {

/** Remove the global tracer provider */
public disable() {
unregisterGlobal(API_NAME);
unregisterGlobal(API_NAME, DiagAPI.instance());
this._proxyTracerProvider = new ProxyTracerProvider();
}

Expand Down
5 changes: 3 additions & 2 deletions src/internal/global-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
* limitations under the License.
*/

import { diag } from '..';
import type { DiagAPI } from '../api/diag';
import { ContextManager } from '../context/types';
import { DiagLogger } from '../diag';
import { _globalThis } from '../platform';
Expand All @@ -33,6 +33,7 @@ const _global = _globalThis as OTelGlobal;
export function registerGlobal<Type extends keyof OTelGlobalAPI>(
type: Type,
instance: OTelGlobalAPI[Type],
diag: DiagAPI,
allowOverride = false
): boolean {
const api = (_global[GLOBAL_OPENTELEMETRY_API_KEY] = _global[
Expand Down Expand Up @@ -77,7 +78,7 @@ export function getGlobal<Type extends keyof OTelGlobalAPI>(
return _global[GLOBAL_OPENTELEMETRY_API_KEY]?.[type];
}

export function unregisterGlobal(type: keyof OTelGlobalAPI) {
export function unregisterGlobal(type: keyof OTelGlobalAPI, diag: DiagAPI) {
diag.debug(
`@opentelemetry/api: Unregistering a global for ${type} v${VERSION}.`
);
Expand Down