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

Commit dd6e41a

Browse files
authored
Merge a6ede59 into ed9ba80
2 parents ed9ba80 + a6ede59 commit dd6e41a

File tree

15 files changed

+488
-238
lines changed

15 files changed

+488
-238
lines changed

.eslintrc.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ module.exports = {
4444
"files": ["test/**/*.ts"],
4545
"rules": {
4646
"no-empty": "off",
47+
"@typescript-eslint/ban-ts-comment": "off",
4748
"@typescript-eslint/ban-ts-ignore": "off",
4849
"@typescript-eslint/no-empty-function": "off",
4950
"@typescript-eslint/no-explicit-any": "off",

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: 37 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -19,66 +19,42 @@ import {
1919
DiagLogFunction,
2020
createNoopDiagLogger,
2121
diagLoggerFunctions,
22+
FilteredDiagLogger,
2223
} from '../diag/logger';
2324
import { DiagLogLevel, createLogLevelDiagLogger } from '../diag/logLevel';
2425
import {
25-
API_BACKWARDS_COMPATIBILITY_VERSION,
26-
GLOBAL_DIAG_LOGGER_API_KEY,
27-
makeGetter,
28-
_global,
29-
} from './global-utils';
30-
31-
/** Internal simple Noop Diag API that returns a noop logger and does not allow any changes */
32-
function noopDiagApi(): DiagAPI {
33-
const noopApi = createNoopDiagLogger() as DiagAPI;
34-
35-
noopApi.getLogger = () => noopApi;
36-
noopApi.setLogger = noopApi.getLogger;
37-
noopApi.setLogLevel = () => {};
38-
39-
return noopApi;
40-
}
26+
getGlobal,
27+
registerGlobal,
28+
unregisterGlobal,
29+
} from '../internal/global-utils';
4130

4231
/**
4332
* Singleton object which represents the entry point to the OpenTelemetry internal
4433
* diagnostic API
4534
*/
4635
export class DiagAPI implements DiagLogger {
36+
private static _instance?: DiagAPI;
37+
4738
/** Get the singleton instance of the DiagAPI API */
4839
public static instance(): DiagAPI {
49-
let theInst = null;
50-
if (_global[GLOBAL_DIAG_LOGGER_API_KEY]) {
51-
// Looks like a previous instance was set, so try and fetch it
52-
theInst = _global[GLOBAL_DIAG_LOGGER_API_KEY]?.(
53-
API_BACKWARDS_COMPATIBILITY_VERSION
54-
) as DiagAPI;
55-
}
56-
57-
if (!theInst) {
58-
theInst = new DiagAPI();
59-
_global[GLOBAL_DIAG_LOGGER_API_KEY] = makeGetter(
60-
API_BACKWARDS_COMPATIBILITY_VERSION,
61-
theInst,
62-
noopDiagApi()
63-
);
40+
if (!this._instance) {
41+
this._instance = new DiagAPI();
6442
}
6543

66-
return theInst;
44+
return this._instance;
6745
}
6846

6947
/**
7048
* Private internal constructor
7149
* @private
7250
*/
7351
private constructor() {
74-
let _logLevel: DiagLogLevel = DiagLogLevel.INFO;
75-
let _filteredLogger: DiagLogger | null;
76-
let _logger: DiagLogger = createNoopDiagLogger();
52+
const _noopLogger = createNoopDiagLogger();
7753

7854
function _logProxy(funcName: keyof DiagLogger): DiagLogFunction {
7955
return function () {
8056
const orgArguments = arguments as unknown;
81-
const theLogger = _filteredLogger || _logger;
57+
const theLogger = self.getLogger();
8258
const theFunc = theLogger[funcName];
8359
if (typeof theFunc === 'function') {
8460
return theFunc.apply(
@@ -94,29 +70,23 @@ export class DiagAPI implements DiagLogger {
9470

9571
// DiagAPI specific functions
9672

97-
self.getLogger = (): DiagLogger => {
98-
// Return itself if no existing logger is defined (defaults effectively to a Noop)
99-
return _logger;
73+
self.getLogger = (): FilteredDiagLogger => {
74+
return getGlobal('diag') || _noopLogger;
10075
};
10176

102-
self.setLogger = (logger?: DiagLogger): DiagLogger => {
103-
const prevLogger = _logger;
104-
if (!logger || logger !== self) {
105-
// Simple special case to avoid any possible infinite recursion on the logging functions
106-
_logger = logger || createNoopDiagLogger();
107-
_filteredLogger = createLogLevelDiagLogger(_logLevel, _logger);
108-
}
109-
110-
return prevLogger;
77+
self.setLogger = (
78+
logger: DiagLogger,
79+
logLevel: DiagLogLevel = DiagLogLevel.INFO
80+
) => {
81+
// This is required to prevent an endless loop in the case where the diag
82+
// is used as a child of itself accidentally.
83+
logger = logger === self ? self.getLogger().getChild() : logger;
84+
logger = logger ?? _noopLogger;
85+
registerGlobal('diag', createLogLevelDiagLogger(logLevel, logger), true);
11186
};
11287

113-
self.setLogLevel = (maxLogLevel: DiagLogLevel) => {
114-
if (maxLogLevel !== _logLevel) {
115-
_logLevel = maxLogLevel;
116-
if (_logger) {
117-
_filteredLogger = createLogLevelDiagLogger(maxLogLevel, _logger);
118-
}
119-
}
88+
self.disable = () => {
89+
unregisterGlobal('diag');
12090
};
12191

12292
for (let i = 0; i < diagLoggerFunctions.length; i++) {
@@ -129,22 +99,27 @@ export class DiagAPI implements DiagLogger {
12999
* Return the currently configured logger instance, if no logger has been configured
130100
* it will return itself so any log level filtering will still be applied in this case.
131101
*/
132-
public getLogger!: () => DiagLogger;
102+
public getLogger!: () => FilteredDiagLogger;
133103

134104
/**
135-
* Set the DiagLogger instance
136-
* @param logger - [Optional] The DiagLogger instance to set as the default logger, if not provided it will set it back as a noop
105+
* Set the global DiagLogger and DiagLogLevel.
106+
* If a global diag logger is already set, this will override it.
107+
*
108+
* @param logger - [Optional] The DiagLogger instance to set as the default logger.
109+
* @param logLevel - [Optional] The DiagLogLevel used to filter logs sent to the logger. If not provided it will default to INFO.
137110
* @returns The previously registered DiagLogger
138111
*/
139-
public setLogger!: (logger?: DiagLogger) => DiagLogger;
140-
141-
/** Set the default maximum diagnostic logging level */
142-
public setLogLevel!: (maxLogLevel: DiagLogLevel) => void;
112+
public setLogger!: (logger: DiagLogger, logLevel?: DiagLogLevel) => void;
143113

144114
// DiagLogger implementation
145115
public verbose!: DiagLogFunction;
146116
public debug!: DiagLogFunction;
147117
public info!: DiagLogFunction;
148118
public warn!: DiagLogFunction;
149119
public error!: DiagLogFunction;
120+
121+
/**
122+
* Unregister the global logger and return to Noop
123+
*/
124+
public disable!: () => void;
150125
}

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
}

0 commit comments

Comments
 (0)