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

Commit

Permalink
chore: use semver to determine API compatibility
Browse files Browse the repository at this point in the history
  • Loading branch information
dyladan committed Feb 24, 2021
1 parent 92df4aa commit 3c20ab7
Show file tree
Hide file tree
Showing 11 changed files with 413 additions and 183 deletions.
30 changes: 8 additions & 22 deletions src/api/context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,12 @@
import { NoopContextManager } from '../context/NoopContextManager';
import { Context, ContextManager } from '../context/types';
import {
API_BACKWARDS_COMPATIBILITY_VERSION,
GLOBAL_CONTEXT_MANAGER_API_KEY,
makeGetter,
_global,
} from './global-utils';
getGlobal,
registerGlobal,
unregisterGlobal,
} from '../internal/global-utils';

const API_NAME = 'context';
const NOOP_CONTEXT_MANAGER = new NoopContextManager();

/**
Expand All @@ -49,17 +49,7 @@ export class ContextAPI {
public setGlobalContextManager(
contextManager: ContextManager
): ContextManager {
if (_global[GLOBAL_CONTEXT_MANAGER_API_KEY]) {
// global context manager has already been set
return this._getContextManager();
}

_global[GLOBAL_CONTEXT_MANAGER_API_KEY] = makeGetter(
API_BACKWARDS_COMPATIBILITY_VERSION,
contextManager,
NOOP_CONTEXT_MANAGER
);

registerGlobal(API_NAME, contextManager);
return contextManager;
}

Expand Down Expand Up @@ -98,16 +88,12 @@ export class ContextAPI {
}

private _getContextManager(): ContextManager {
return (
_global[GLOBAL_CONTEXT_MANAGER_API_KEY]?.(
API_BACKWARDS_COMPATIBILITY_VERSION
) ?? NOOP_CONTEXT_MANAGER
);
return getGlobal(API_NAME) || NOOP_CONTEXT_MANAGER;
}

/** Disable and remove the global context manager */
public disable() {
this._getContextManager().disable();
delete _global[GLOBAL_CONTEXT_MANAGER_API_KEY];
unregisterGlobal(API_NAME);
}
}
56 changes: 19 additions & 37 deletions src/api/diag.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,48 +23,27 @@ import {
} from '../diag/logger';
import { DiagLogLevel, createLogLevelDiagLogger } from '../diag/logLevel';
import {
API_BACKWARDS_COMPATIBILITY_VERSION,
GLOBAL_DIAG_LOGGER_API_KEY,
makeGetter,
_global,
} from './global-utils';

/** Internal simple Noop Diag API that returns a noop logger and does not allow any changes */
function noopDiagApi(): DiagAPI {
const noopLogger = createNoopDiagLogger();
return {
disable: () => {},
getLogger: () => noopLogger,
setLogger: () => {},
...noopLogger,
};
}
getGlobal,
registerGlobal,
unregisterGlobal,
} from '../internal/global-utils';

const API_NAME = 'diag';

/**
* Singleton object which represents the entry point to the OpenTelemetry internal
* diagnostic API
*/
export class DiagAPI implements DiagLogger {
private static _instance?: DiagAPI;

/** Get the singleton instance of the DiagAPI API */
public static instance(): DiagAPI {
let theInst = null;
if (_global[GLOBAL_DIAG_LOGGER_API_KEY]) {
// Looks like a previous instance was set, so try and fetch it
theInst = _global[GLOBAL_DIAG_LOGGER_API_KEY]?.(
API_BACKWARDS_COMPATIBILITY_VERSION
) as DiagAPI;
if (!this._instance) {
this._instance = new DiagAPI();
}

if (!theInst) {
theInst = new DiagAPI();
_global[GLOBAL_DIAG_LOGGER_API_KEY] = makeGetter(
API_BACKWARDS_COMPATIBILITY_VERSION,
theInst,
noopDiagApi()
);
}

return theInst;
return this._instance;
}

/**
Expand All @@ -73,12 +52,11 @@ export class DiagAPI implements DiagLogger {
*/
private constructor() {
const _noopLogger = createNoopDiagLogger();
let _filteredLogger: FilteredDiagLogger | undefined;

function _logProxy(funcName: keyof DiagLogger): DiagLogFunction {
return function () {
const orgArguments = arguments as unknown;
const theLogger = _filteredLogger || _noopLogger;
const theLogger = self.getLogger();
const theFunc = theLogger[funcName];
if (typeof theFunc === 'function') {
return theFunc.apply(
Expand All @@ -95,7 +73,7 @@ export class DiagAPI implements DiagLogger {
// DiagAPI specific functions

self.getLogger = (): FilteredDiagLogger => {
return _filteredLogger || _noopLogger;
return getGlobal(API_NAME) || _noopLogger;
};

self.setLogger = (
Expand All @@ -106,11 +84,15 @@ export class DiagAPI implements DiagLogger {
// is used as a child of itself accidentally.
logger = logger === self ? self.getLogger().getChild() : logger;
logger = logger ?? _noopLogger;
_filteredLogger = createLogLevelDiagLogger(logLevel, logger);
registerGlobal(
API_NAME,
createLogLevelDiagLogger(logLevel, logger),
true
);
};

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

for (let i = 0; i < diagLoggerFunctions.length; i++) {
Expand Down
70 changes: 0 additions & 70 deletions src/api/global-utils.ts

This file was deleted.

31 changes: 9 additions & 22 deletions src/api/propagation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,12 @@ import {
TextMapSetter,
} from '../propagation/TextMapPropagator';
import {
API_BACKWARDS_COMPATIBILITY_VERSION,
GLOBAL_PROPAGATION_API_KEY,
makeGetter,
_global,
} from './global-utils';
getGlobal,
registerGlobal,
unregisterGlobal,
} from '../internal/global-utils';

const API_NAME = 'propagation';

/**
* Singleton object which represents the entry point to the OpenTelemetry Propagation API
Expand All @@ -52,17 +53,7 @@ export class PropagationAPI {
* Set the current propagator. Returns the initialized propagator
*/
public setGlobalPropagator(propagator: TextMapPropagator): TextMapPropagator {
if (_global[GLOBAL_PROPAGATION_API_KEY]) {
// global propagator has already been set
return this._getGlobalPropagator();
}

_global[GLOBAL_PROPAGATION_API_KEY] = makeGetter(
API_BACKWARDS_COMPATIBILITY_VERSION,
propagator,
NOOP_TEXT_MAP_PROPAGATOR
);

registerGlobal(API_NAME, propagator);
return propagator;
}

Expand Down Expand Up @@ -105,14 +96,10 @@ export class PropagationAPI {

/** Remove the global propagator */
public disable() {
delete _global[GLOBAL_PROPAGATION_API_KEY];
unregisterGlobal(API_NAME);
}

private _getGlobalPropagator(): TextMapPropagator {
return (
_global[GLOBAL_PROPAGATION_API_KEY]?.(
API_BACKWARDS_COMPATIBILITY_VERSION
) ?? NOOP_TEXT_MAP_PROPAGATOR
);
return getGlobal(API_NAME) || NOOP_TEXT_MAP_PROPAGATOR;
}
}
34 changes: 10 additions & 24 deletions src/api/trace.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,17 @@
* limitations under the License.
*/

import { NOOP_TRACER_PROVIDER } from '../trace/NoopTracerProvider';
import { ProxyTracerProvider } from '../trace/ProxyTracerProvider';
import { Tracer } from '../trace/tracer';
import { TracerProvider } from '../trace/tracer_provider';
import { isSpanContextValid } from '../trace/spancontext-utils';
import {
API_BACKWARDS_COMPATIBILITY_VERSION,
GLOBAL_TRACE_API_KEY,
makeGetter,
_global,
} from './global-utils';
getGlobal,
registerGlobal,
unregisterGlobal,
} from '../internal/global-utils';

const API_NAME = 'trace';

/**
* Singleton object which represents the entry point to the OpenTelemetry Tracing API
Expand All @@ -50,30 +50,16 @@ export class TraceAPI {
* Set the current global tracer. Returns the initialized global tracer provider
*/
public setGlobalTracerProvider(provider: TracerProvider): TracerProvider {
if (_global[GLOBAL_TRACE_API_KEY]) {
// global tracer provider has already been set
return this.getTracerProvider();
}

this._proxyTracerProvider.setDelegate(provider);

_global[GLOBAL_TRACE_API_KEY] = makeGetter(
API_BACKWARDS_COMPATIBILITY_VERSION,
this._proxyTracerProvider,
NOOP_TRACER_PROVIDER
);

return this.getTracerProvider();
registerGlobal(API_NAME, this._proxyTracerProvider);
return this._proxyTracerProvider;
}

/**
* Returns the global tracer provider.
*/
public getTracerProvider(): TracerProvider {
return (
_global[GLOBAL_TRACE_API_KEY]?.(API_BACKWARDS_COMPATIBILITY_VERSION) ??
this._proxyTracerProvider
);
return getGlobal(API_NAME) || this._proxyTracerProvider;
}

/**
Expand All @@ -85,7 +71,7 @@ export class TraceAPI {

/** Remove the global tracer provider */
public disable() {
delete _global[GLOBAL_TRACE_API_KEY];
unregisterGlobal(API_NAME);
this._proxyTracerProvider = new ProxyTracerProvider();
}

Expand Down
Loading

0 comments on commit 3c20ab7

Please sign in to comment.