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

Use semver to determine API compatibility #10

Merged
merged 5 commits into from
Feb 26, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
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);
}
}
73 changes: 24 additions & 49 deletions src/api/diag.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,65 +15,43 @@
*/

import { createLogLevelDiagLogger } from '../diag/internal/logLevelLogger';
import { createNoopDiagLogger } from '../diag/internal/noopLogger';
import { DiagLogFunction, DiagLogger, DiagLogLevel } from '../diag/types';
import {
API_BACKWARDS_COMPATIBILITY_VERSION,
GLOBAL_DIAG_LOGGER_API_KEY,
makeGetter,
_global,
} from './global-utils';
getGlobal,
registerGlobal,
unregisterGlobal,
} from '../internal/global-utils';

function nop() {}

/** Internal simple Noop Diag API that returns a noop logger and does not allow any changes */
function noopDiagApi(): DiagAPI {
return Object.assign(
{ disable: nop, setLogger: nop },
createNoopDiagLogger()
);
}
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;
}

/**
* Private internal constructor
* @private
*/
private constructor() {
let _filteredLogger: DiagLogger | undefined;

function _logProxy(funcName: keyof DiagLogger): DiagLogFunction {
return function () {
const logger = getGlobal('diag');
// shortcut if logger not set
if (!_filteredLogger) return;
return _filteredLogger[funcName].apply(
_filteredLogger,
if (!logger) return;
return logger[funcName].apply(
logger,
// work around Function.prototype.apply types
// eslint-disable-next-line @typescript-eslint/no-explicit-any
arguments as any
Expand All @@ -91,24 +69,21 @@ export class DiagAPI implements DiagLogger {
logLevel: DiagLogLevel = DiagLogLevel.INFO
) => {
if (logger === self) {
if (_filteredLogger) {
const err = new Error(
'Cannot use diag as the logger for itself. Please use a DiagLogger implementation like ConsoleDiagLogger or a custom implementation'
);
_filteredLogger.error(err.stack ?? err.message);
logger = _filteredLogger;
} else {
// There isn't much we can do here.
// Logging to the console might break the user application.
return;
}
// There isn't much we can do here.
// Logging to the console might break the user application.
// Try to log to self. If a logger was previously registered it will receive the log.
const err = new Error(
'Cannot use diag as the logger for itself. Please use a DiagLogger implementation like ConsoleDiagLogger or a custom implementation'
);
self.error(err.stack ?? err.message);
return;
}

_filteredLogger = createLogLevelDiagLogger(logLevel, logger);
registerGlobal('diag', createLogLevelDiagLogger(logLevel, logger), true);
};

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

self.verbose = _logProxy('verbose');
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