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

Commit 7441fea

Browse files
authored
chore: remove circular dependency (#96)
1 parent 25cf8a4 commit 7441fea

File tree

5 files changed

+18
-10
lines changed

5 files changed

+18
-10
lines changed

src/api/context.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import {
2121
registerGlobal,
2222
unregisterGlobal,
2323
} from '../internal/global-utils';
24+
import { DiagAPI } from './diag';
2425

2526
const API_NAME = 'context';
2627
const NOOP_CONTEXT_MANAGER = new NoopContextManager();
@@ -49,7 +50,7 @@ export class ContextAPI {
4950
* @returns true if the context manager was successfully registered, else false
5051
*/
5152
public setGlobalContextManager(contextManager: ContextManager): boolean {
52-
return registerGlobal(API_NAME, contextManager);
53+
return registerGlobal(API_NAME, contextManager, DiagAPI.instance());
5354
}
5455

5556
/**
@@ -93,6 +94,6 @@ export class ContextAPI {
9394
/** Disable and remove the global context manager */
9495
public disable() {
9596
this._getContextManager().disable();
96-
unregisterGlobal(API_NAME);
97+
unregisterGlobal(API_NAME, DiagAPI.instance());
9798
}
9899
}

src/api/diag.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -96,11 +96,11 @@ export class DiagAPI implements DiagLogger {
9696
);
9797
}
9898

99-
return registerGlobal('diag', newLogger, true);
99+
return registerGlobal('diag', newLogger, self, true);
100100
};
101101

102102
self.disable = () => {
103-
unregisterGlobal(API_NAME);
103+
unregisterGlobal(API_NAME, self);
104104
};
105105

106106
self.createComponentLogger = (options: ComponentLoggerOptions) => {

src/api/propagation.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ import {
3434
deleteBaggage,
3535
} from '../baggage/context-helpers';
3636
import { createBaggage } from '../baggage/utils';
37+
import { DiagAPI } from './diag';
3738

3839
const API_NAME = 'propagation';
3940
const NOOP_TEXT_MAP_PROPAGATOR = new NoopTextMapPropagator();
@@ -62,7 +63,7 @@ export class PropagationAPI {
6263
* @returns true if the propagator was successfully registered, else false
6364
*/
6465
public setGlobalPropagator(propagator: TextMapPropagator): boolean {
65-
return registerGlobal(API_NAME, propagator);
66+
return registerGlobal(API_NAME, propagator, DiagAPI.instance());
6667
}
6768

6869
/**
@@ -104,7 +105,7 @@ export class PropagationAPI {
104105

105106
/** Remove the global propagator */
106107
public disable() {
107-
unregisterGlobal(API_NAME);
108+
unregisterGlobal(API_NAME, DiagAPI.instance());
108109
}
109110

110111
public createBaggage = createBaggage;

src/api/trace.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ import {
3333
setSpan,
3434
setSpanContext,
3535
} from '../trace/context-utils';
36+
import { DiagAPI } from './diag';
3637

3738
const API_NAME = 'trace';
3839

@@ -63,7 +64,11 @@ export class TraceAPI {
6364
*/
6465
public setGlobalTracerProvider(provider: TracerProvider): boolean {
6566
this._proxyTracerProvider.setDelegate(provider);
66-
return registerGlobal(API_NAME, this._proxyTracerProvider);
67+
return registerGlobal(
68+
API_NAME,
69+
this._proxyTracerProvider,
70+
DiagAPI.instance()
71+
);
6772
}
6873

6974
/**
@@ -82,7 +87,7 @@ export class TraceAPI {
8287

8388
/** Remove the global tracer provider */
8489
public disable() {
85-
unregisterGlobal(API_NAME);
90+
unregisterGlobal(API_NAME, DiagAPI.instance());
8691
this._proxyTracerProvider = new ProxyTracerProvider();
8792
}
8893

src/internal/global-utils.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
* limitations under the License.
1515
*/
1616

17-
import { diag } from '..';
17+
import type { DiagAPI } from '../api/diag';
1818
import { ContextManager } from '../context/types';
1919
import { DiagLogger } from '../diag';
2020
import { _globalThis } from '../platform';
@@ -33,6 +33,7 @@ const _global = _globalThis as OTelGlobal;
3333
export function registerGlobal<Type extends keyof OTelGlobalAPI>(
3434
type: Type,
3535
instance: OTelGlobalAPI[Type],
36+
diag: DiagAPI,
3637
allowOverride = false
3738
): boolean {
3839
const api = (_global[GLOBAL_OPENTELEMETRY_API_KEY] = _global[
@@ -77,7 +78,7 @@ export function getGlobal<Type extends keyof OTelGlobalAPI>(
7778
return _global[GLOBAL_OPENTELEMETRY_API_KEY]?.[type];
7879
}
7980

80-
export function unregisterGlobal(type: keyof OTelGlobalAPI) {
81+
export function unregisterGlobal(type: keyof OTelGlobalAPI, diag: DiagAPI) {
8182
diag.debug(
8283
`@opentelemetry/api: Unregistering a global for ${type} v${VERSION}.`
8384
);

0 commit comments

Comments
 (0)