Skip to content

Commit

Permalink
chore: do not export singletons (open-telemetry#46)
Browse files Browse the repository at this point in the history
  • Loading branch information
dyladan authored May 27, 2021
1 parent 0885688 commit 690067a
Show file tree
Hide file tree
Showing 11 changed files with 39 additions and 42 deletions.
13 changes: 7 additions & 6 deletions api/src/api/propagation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,19 +15,19 @@
*/

import { Context } from '../context/types';
import { NOOP_TEXT_MAP_PROPAGATOR } from '../propagation/NoopTextMapPropagator';
import {
getGlobal,
registerGlobal,
unregisterGlobal,
} from '../internal/global-utils';
import { NoopTextMapPropagator } from '../propagation/NoopTextMapPropagator';
import {
defaultTextMapGetter,
defaultTextMapSetter,
TextMapGetter,
TextMapPropagator,
TextMapSetter,
} from '../propagation/TextMapPropagator';
import {
getGlobal,
registerGlobal,
unregisterGlobal,
} from '../internal/global-utils';
import {
getBaggage,
setBaggage,
Expand All @@ -36,6 +36,7 @@ import {
import { createBaggage } from '../baggage/utils';

const API_NAME = 'propagation';
const NOOP_TEXT_MAP_PROPAGATOR = new NoopTextMapPropagator();

/**
* Singleton object which represents the entry point to the OpenTelemetry Propagation API
Expand Down
4 changes: 0 additions & 4 deletions api/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,9 @@ export { baggageEntryMetadataFromString } from './baggage/utils';
export * from './common/Exception';
export * from './common/Time';
export * from './diag';
export * from './propagation/NoopTextMapPropagator';
export * from './propagation/TextMapPropagator';
export * from './trace/attributes';
export * from './trace/link';
export * from './trace/NoopTracer';
export * from './trace/NoopTracerProvider';
export * from './trace/ProxyTracer';
export * from './trace/ProxyTracerProvider';
export * from './trace/Sampler';
Expand All @@ -49,7 +46,6 @@ export {
} from './trace/spancontext-utils';

export * from './context/context';
export * from './context/NoopContextManager';
export * from './context/types';

import { ContextAPI } from './api/context';
Expand Down
2 changes: 0 additions & 2 deletions api/src/propagation/NoopTextMapPropagator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,5 +31,3 @@ export class NoopTextMapPropagator implements TextMapPropagator {
return [];
}
}

export const NOOP_TEXT_MAP_PROPAGATOR = new NoopTextMapPropagator();
2 changes: 0 additions & 2 deletions api/src/trace/NoopTracer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,5 +95,3 @@ function isSpanContext(spanContext: any): spanContext is SpanContext {
typeof spanContext['traceFlags'] === 'number'
);
}

export const NOOP_TRACER = new NoopTracer();
6 changes: 2 additions & 4 deletions api/src/trace/NoopTracerProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
* limitations under the License.
*/

import { NOOP_TRACER } from './NoopTracer';
import { NoopTracer } from './NoopTracer';
import { Tracer } from './tracer';
import { TracerProvider } from './tracer_provider';

Expand All @@ -26,8 +26,6 @@ import { TracerProvider } from './tracer_provider';
*/
export class NoopTracerProvider implements TracerProvider {
getTracer(_name?: string, _version?: string): Tracer {
return NOOP_TRACER;
return new NoopTracer();
}
}

export const NOOP_TRACER_PROVIDER = new NoopTracerProvider();
4 changes: 2 additions & 2 deletions api/src/trace/ProxyTracer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
*/

import { Context } from '../context/types';
import { NOOP_TRACER } from './NoopTracer';
import { NoopTracer } from './NoopTracer';
import { ProxyTracerProvider } from './ProxyTracerProvider';
import { Span } from './span';
import { SpanOptions } from './SpanOptions';
Expand Down Expand Up @@ -60,7 +60,7 @@ export class ProxyTracer implements Tracer {
const tracer = this._provider.getDelegateTracer(this.name, this.version);

if (!tracer) {
return NOOP_TRACER;
return new NoopTracer();
}

this._delegate = tracer;
Expand Down
4 changes: 3 additions & 1 deletion api/src/trace/ProxyTracerProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@
import { Tracer } from './tracer';
import { TracerProvider } from './tracer_provider';
import { ProxyTracer } from './ProxyTracer';
import { NOOP_TRACER_PROVIDER } from './NoopTracerProvider';
import { NoopTracerProvider } from './NoopTracerProvider';

const NOOP_TRACER_PROVIDER = new NoopTracerProvider();

/**
* Tracer provider which provides {@link ProxyTracer}s.
Expand Down
24 changes: 12 additions & 12 deletions api/test/api/api.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,25 +16,25 @@

import * as assert from 'assert';
import api, {
TraceFlags,
NoopTracerProvider,
NoopTracer,
SpanOptions,
Span,
context,
trace,
propagation,
TextMapPropagator,
Context,
TextMapSetter,
TextMapGetter,
ROOT_CONTEXT,
defaultTextMapSetter,
defaultTextMapGetter,
defaultTextMapSetter,
diag,
propagation,
ROOT_CONTEXT,
Span,
SpanOptions,
TextMapGetter,
TextMapPropagator,
TextMapSetter,
trace,
TraceFlags,
} from '../../src';
import { DiagAPI } from '../../src/api/diag';
import { NonRecordingSpan } from '../../src/trace/NonRecordingSpan';
import { NoopTracer } from '../../src/trace/NoopTracer';
import { NoopTracerProvider } from '../../src/trace/NoopTracerProvider';

// DiagLogger implementation
const diagLoggerFunctions = [
Expand Down
17 changes: 10 additions & 7 deletions api/test/internal/global.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,28 +67,31 @@ describe('Global Utils', () => {
});

it('should disable both if one is disabled', () => {
const original = api1.context['_getContextManager']();

api1.context.setGlobalContextManager(new NoopContextManager());
const manager = new NoopContextManager();
api1.context.setGlobalContextManager(manager);

assert.notStrictEqual(original, api1.context['_getContextManager']());
assert.strictEqual(manager, api1.context['_getContextManager']());
api2.context.disable();
assert.strictEqual(original, api1.context['_getContextManager']());
assert.notStrictEqual(manager, api1.context['_getContextManager']());
});

it('should return the module NoOp implementation if the version is a mismatch', () => {
const original = api1.context['_getContextManager']();
const newContextManager = new NoopContextManager();
api1.context.setGlobalContextManager(newContextManager);

// ensure new context manager is returned
assert.strictEqual(api1.context['_getContextManager'](), newContextManager);

const globalInstance = getGlobal('context');
assert.ok(globalInstance);
// @ts-expect-error we are modifying internals for testing purposes here
_globalThis[Symbol.for(GLOBAL_API_SYMBOL_KEY)].version = '0.0.1';

assert.strictEqual(api1.context['_getContextManager'](), original);
// ensure new context manager is not returned because version above is incompatible
assert.notStrictEqual(
api1.context['_getContextManager'](),
newContextManager
);
});

it('should log an error if there is a duplicate registration', () => {
Expand Down
2 changes: 1 addition & 1 deletion api/test/noop-implementations/noop-tracer.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,14 @@
import * as assert from 'assert';
import {
context,
NoopTracer,
Span,
SpanContext,
SpanKind,
trace,
TraceFlags,
} from '../../src';
import { NonRecordingSpan } from '../../src/trace/NonRecordingSpan';
import { NoopTracer } from '../../src/trace/NoopTracer';

describe('NoopTracer', () => {
it('should not crash', () => {
Expand Down
3 changes: 2 additions & 1 deletion api/test/proxy-implementations/proxy-tracer.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ import * as assert from 'assert';
import * as sinon from 'sinon';
import {
context,
NoopTracer,
ProxyTracer,
ProxyTracerProvider,
ROOT_CONTEXT,
Expand All @@ -29,6 +28,8 @@ import {
TracerProvider,
} from '../../src';
import { NonRecordingSpan } from '../../src/trace/NonRecordingSpan';
import { NoopTracer } from '../../src/trace/NoopTracer';

describe('ProxyTracer', () => {
let provider: ProxyTracerProvider;
const sandbox = sinon.createSandbox();
Expand Down

0 comments on commit 690067a

Please sign in to comment.