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

chore: do not export singletons #46

Merged
merged 7 commits into from
May 27, 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
3 changes: 1 addition & 2 deletions src/api/context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ import {
} from '../internal/global-utils';

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

/**
* Singleton object which represents the entry point to the OpenTelemetry Context API
Expand Down Expand Up @@ -87,7 +86,7 @@ export class ContextAPI {
}

private _getContextManager(): ContextManager {
return getGlobal(API_NAME) || NOOP_CONTEXT_MANAGER;
return getGlobal(API_NAME) || new NoopContextManager();
dyladan marked this conversation as resolved.
Show resolved Hide resolved
}

/** Disable and remove the global context manager */
Expand Down
14 changes: 7 additions & 7 deletions 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';

const API_NAME = 'propagation';

Expand Down Expand Up @@ -101,6 +101,6 @@ export class PropagationAPI {
}

private _getGlobalPropagator(): TextMapPropagator {
return getGlobal(API_NAME) || NOOP_TEXT_MAP_PROPAGATOR;
return getGlobal(API_NAME) || new NoopTextMapPropagator();
}
}
2 changes: 0 additions & 2 deletions 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 src/trace/NoopTracer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,5 +55,3 @@ function isSpanContext(spanContext: any): spanContext is SpanContext {
typeof spanContext['traceFlags'] === 'number'
);
}

export const NOOP_TRACER = new NoopTracer();
obecny marked this conversation as resolved.
Show resolved Hide resolved
6 changes: 2 additions & 4 deletions 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 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 @@ -50,7 +50,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: 2 additions & 2 deletions src/trace/ProxyTracerProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
import { Tracer } from './tracer';
import { TracerProvider } from './tracer_provider';
import { ProxyTracer } from './ProxyTracer';
import { NOOP_TRACER_PROVIDER } from './NoopTracerProvider';
import { NoopTracerProvider } from './NoopTracerProvider';

/**
* Tracer provider which provides {@link ProxyTracer}s.
Expand All @@ -41,7 +41,7 @@ export class ProxyTracerProvider implements TracerProvider {
}

getDelegate(): TracerProvider {
return this._delegate ?? NOOP_TRACER_PROVIDER;
return this._delegate ?? new NoopTracerProvider();
}

/**
Expand Down
17 changes: 10 additions & 7 deletions 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