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

Commit 69ae974

Browse files
committed
chore: use semver to determine API compatibility
1 parent fb98977 commit 69ae974

File tree

10 files changed

+417
-194
lines changed

10 files changed

+417
-194
lines changed

src/api/context.ts

Lines changed: 8 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,12 @@
1717
import { NoopContextManager } from '../context/NoopContextManager';
1818
import { Context, ContextManager } from '../context/types';
1919
import {
20-
API_BACKWARDS_COMPATIBILITY_VERSION,
21-
GLOBAL_CONTEXT_MANAGER_API_KEY,
22-
makeGetter,
23-
_global,
24-
} from './global-utils';
20+
getGlobal,
21+
registerGlobal,
22+
unregisterGlobal,
23+
} from '../internal/global-utils';
2524

25+
const API_NAME = 'context';
2626
const NOOP_CONTEXT_MANAGER = new NoopContextManager();
2727

2828
/**
@@ -49,17 +49,7 @@ export class ContextAPI {
4949
public setGlobalContextManager(
5050
contextManager: ContextManager
5151
): ContextManager {
52-
if (_global[GLOBAL_CONTEXT_MANAGER_API_KEY]) {
53-
// global context manager has already been set
54-
return this._getContextManager();
55-
}
56-
57-
_global[GLOBAL_CONTEXT_MANAGER_API_KEY] = makeGetter(
58-
API_BACKWARDS_COMPATIBILITY_VERSION,
59-
contextManager,
60-
NOOP_CONTEXT_MANAGER
61-
);
62-
52+
registerGlobal(API_NAME, contextManager);
6353
return contextManager;
6454
}
6555

@@ -98,16 +88,12 @@ export class ContextAPI {
9888
}
9989

10090
private _getContextManager(): ContextManager {
101-
return (
102-
_global[GLOBAL_CONTEXT_MANAGER_API_KEY]?.(
103-
API_BACKWARDS_COMPATIBILITY_VERSION
104-
) ?? NOOP_CONTEXT_MANAGER
105-
);
91+
return getGlobal(API_NAME) || NOOP_CONTEXT_MANAGER;
10692
}
10793

10894
/** Disable and remove the global context manager */
10995
public disable() {
11096
this._getContextManager().disable();
111-
delete _global[GLOBAL_CONTEXT_MANAGER_API_KEY];
97+
unregisterGlobal(API_NAME);
11298
}
11399
}

src/api/diag.ts

Lines changed: 24 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -15,65 +15,43 @@
1515
*/
1616

1717
import { createLogLevelDiagLogger } from '../diag/internal/logLevelLogger';
18-
import { createNoopDiagLogger } from '../diag/internal/noopLogger';
1918
import { DiagLogFunction, DiagLogger, DiagLogLevel } from '../diag/types';
2019
import {
21-
API_BACKWARDS_COMPATIBILITY_VERSION,
22-
GLOBAL_DIAG_LOGGER_API_KEY,
23-
makeGetter,
24-
_global,
25-
} from './global-utils';
20+
getGlobal,
21+
registerGlobal,
22+
unregisterGlobal,
23+
} from '../internal/global-utils';
2624

27-
function nop() {}
28-
29-
/** Internal simple Noop Diag API that returns a noop logger and does not allow any changes */
30-
function noopDiagApi(): DiagAPI {
31-
return Object.assign(
32-
{ disable: nop, setLogger: nop },
33-
createNoopDiagLogger()
34-
);
35-
}
25+
const API_NAME = 'diag';
3626

3727
/**
3828
* Singleton object which represents the entry point to the OpenTelemetry internal
3929
* diagnostic API
4030
*/
4131
export class DiagAPI implements DiagLogger {
32+
private static _instance?: DiagAPI;
33+
4234
/** Get the singleton instance of the DiagAPI API */
4335
public static instance(): DiagAPI {
44-
let theInst = null;
45-
if (_global[GLOBAL_DIAG_LOGGER_API_KEY]) {
46-
// Looks like a previous instance was set, so try and fetch it
47-
theInst = _global[GLOBAL_DIAG_LOGGER_API_KEY]?.(
48-
API_BACKWARDS_COMPATIBILITY_VERSION
49-
) as DiagAPI;
36+
if (!this._instance) {
37+
this._instance = new DiagAPI();
5038
}
5139

52-
if (!theInst) {
53-
theInst = new DiagAPI();
54-
_global[GLOBAL_DIAG_LOGGER_API_KEY] = makeGetter(
55-
API_BACKWARDS_COMPATIBILITY_VERSION,
56-
theInst,
57-
noopDiagApi()
58-
);
59-
}
60-
61-
return theInst;
40+
return this._instance;
6241
}
6342

6443
/**
6544
* Private internal constructor
6645
* @private
6746
*/
6847
private constructor() {
69-
let _filteredLogger: DiagLogger | undefined;
70-
7148
function _logProxy(funcName: keyof DiagLogger): DiagLogFunction {
7249
return function () {
50+
const logger = getGlobal('diag');
7351
// shortcut if logger not set
74-
if (!_filteredLogger) return;
75-
return _filteredLogger[funcName].apply(
76-
_filteredLogger,
52+
if (!logger) return;
53+
return logger[funcName].apply(
54+
logger,
7755
// work around Function.prototype.apply types
7856
// eslint-disable-next-line @typescript-eslint/no-explicit-any
7957
arguments as any
@@ -91,24 +69,21 @@ export class DiagAPI implements DiagLogger {
9169
logLevel: DiagLogLevel = DiagLogLevel.INFO
9270
) => {
9371
if (logger === self) {
94-
if (_filteredLogger) {
95-
const err = new Error(
96-
'Cannot use diag as the logger for itself. Please use a DiagLogger implementation like ConsoleDiagLogger or a custom implementation'
97-
);
98-
_filteredLogger.error(err.stack ?? err.message);
99-
logger = _filteredLogger;
100-
} else {
101-
// There isn't much we can do here.
102-
// Logging to the console might break the user application.
103-
return;
104-
}
72+
// There isn't much we can do here.
73+
// Logging to the console might break the user application.
74+
// Try to log to self. If a logger was previously registered it will receive the log.
75+
const err = new Error(
76+
'Cannot use diag as the logger for itself. Please use a DiagLogger implementation like ConsoleDiagLogger or a custom implementation'
77+
);
78+
self.error(err.stack ?? err.message);
79+
return;
10580
}
10681

107-
_filteredLogger = createLogLevelDiagLogger(logLevel, logger);
82+
registerGlobal('diag', createLogLevelDiagLogger(logLevel, logger), true);
10883
};
10984

11085
self.disable = () => {
111-
_filteredLogger = undefined;
86+
unregisterGlobal(API_NAME);
11287
};
11388

11489
self.verbose = _logProxy('verbose');

src/api/global-utils.ts

Lines changed: 0 additions & 70 deletions
This file was deleted.

src/api/propagation.ts

Lines changed: 9 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,12 @@ import {
2424
TextMapSetter,
2525
} from '../propagation/TextMapPropagator';
2626
import {
27-
API_BACKWARDS_COMPATIBILITY_VERSION,
28-
GLOBAL_PROPAGATION_API_KEY,
29-
makeGetter,
30-
_global,
31-
} from './global-utils';
27+
getGlobal,
28+
registerGlobal,
29+
unregisterGlobal,
30+
} from '../internal/global-utils';
31+
32+
const API_NAME = 'propagation';
3233

3334
/**
3435
* Singleton object which represents the entry point to the OpenTelemetry Propagation API
@@ -52,17 +53,7 @@ export class PropagationAPI {
5253
* Set the current propagator. Returns the initialized propagator
5354
*/
5455
public setGlobalPropagator(propagator: TextMapPropagator): TextMapPropagator {
55-
if (_global[GLOBAL_PROPAGATION_API_KEY]) {
56-
// global propagator has already been set
57-
return this._getGlobalPropagator();
58-
}
59-
60-
_global[GLOBAL_PROPAGATION_API_KEY] = makeGetter(
61-
API_BACKWARDS_COMPATIBILITY_VERSION,
62-
propagator,
63-
NOOP_TEXT_MAP_PROPAGATOR
64-
);
65-
56+
registerGlobal(API_NAME, propagator);
6657
return propagator;
6758
}
6859

@@ -105,14 +96,10 @@ export class PropagationAPI {
10596

10697
/** Remove the global propagator */
10798
public disable() {
108-
delete _global[GLOBAL_PROPAGATION_API_KEY];
99+
unregisterGlobal(API_NAME);
109100
}
110101

111102
private _getGlobalPropagator(): TextMapPropagator {
112-
return (
113-
_global[GLOBAL_PROPAGATION_API_KEY]?.(
114-
API_BACKWARDS_COMPATIBILITY_VERSION
115-
) ?? NOOP_TEXT_MAP_PROPAGATOR
116-
);
103+
return getGlobal(API_NAME) || NOOP_TEXT_MAP_PROPAGATOR;
117104
}
118105
}

src/api/trace.ts

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

17-
import { NOOP_TRACER_PROVIDER } from '../trace/NoopTracerProvider';
1817
import { ProxyTracerProvider } from '../trace/ProxyTracerProvider';
1918
import { Tracer } from '../trace/tracer';
2019
import { TracerProvider } from '../trace/tracer_provider';
2120
import { isSpanContextValid } from '../trace/spancontext-utils';
2221
import {
23-
API_BACKWARDS_COMPATIBILITY_VERSION,
24-
GLOBAL_TRACE_API_KEY,
25-
makeGetter,
26-
_global,
27-
} from './global-utils';
22+
getGlobal,
23+
registerGlobal,
24+
unregisterGlobal,
25+
} from '../internal/global-utils';
26+
27+
const API_NAME = 'trace';
2828

2929
/**
3030
* Singleton object which represents the entry point to the OpenTelemetry Tracing API
@@ -50,30 +50,16 @@ export class TraceAPI {
5050
* Set the current global tracer. Returns the initialized global tracer provider
5151
*/
5252
public setGlobalTracerProvider(provider: TracerProvider): TracerProvider {
53-
if (_global[GLOBAL_TRACE_API_KEY]) {
54-
// global tracer provider has already been set
55-
return this.getTracerProvider();
56-
}
57-
5853
this._proxyTracerProvider.setDelegate(provider);
59-
60-
_global[GLOBAL_TRACE_API_KEY] = makeGetter(
61-
API_BACKWARDS_COMPATIBILITY_VERSION,
62-
this._proxyTracerProvider,
63-
NOOP_TRACER_PROVIDER
64-
);
65-
66-
return this.getTracerProvider();
54+
registerGlobal(API_NAME, this._proxyTracerProvider);
55+
return this._proxyTracerProvider;
6756
}
6857

6958
/**
7059
* Returns the global tracer provider.
7160
*/
7261
public getTracerProvider(): TracerProvider {
73-
return (
74-
_global[GLOBAL_TRACE_API_KEY]?.(API_BACKWARDS_COMPATIBILITY_VERSION) ??
75-
this._proxyTracerProvider
76-
);
62+
return getGlobal(API_NAME) || this._proxyTracerProvider;
7763
}
7864

7965
/**
@@ -85,7 +71,7 @@ export class TraceAPI {
8571

8672
/** Remove the global tracer provider */
8773
public disable() {
88-
delete _global[GLOBAL_TRACE_API_KEY];
74+
unregisterGlobal(API_NAME);
8975
this._proxyTracerProvider = new ProxyTracerProvider();
9076
}
9177

0 commit comments

Comments
 (0)