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 6 commits
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
13 changes: 7 additions & 6 deletions src/api/propagation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,21 +15,22 @@
*/

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';
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 src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,11 @@ export * from './baggage';
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/Event';
export * from './trace/link_context';
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 @@ -51,7 +48,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 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: 3 additions & 1 deletion 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 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 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
6 changes: 3 additions & 3 deletions test/noop-implementations/noop-tracer.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,14 @@

import * as assert from 'assert';
import {
NoopTracer,
context,
setSpanContext,
SpanContext,
SpanKind,
TraceFlags,
context,
setSpanContext,
} from '../../src';
import { NonRecordingSpan } from '../../src/trace/NonRecordingSpan';
import { NoopTracer } from '../../src/trace/NoopTracer';

describe('NoopTracer', () => {
it('should not crash', () => {
Expand Down
12 changes: 6 additions & 6 deletions test/proxy-implementations/proxy-tracer.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,17 +17,17 @@
import * as assert from 'assert';
import * as sinon from 'sinon';
import {
ProxyTracerProvider,
SpanKind,
TracerProvider,
ProxyTracer,
Tracer,
Span,
NoopTracer,
ProxyTracerProvider,
ROOT_CONTEXT,
Span,
SpanKind,
SpanOptions,
Tracer,
TracerProvider,
} from '../../src';
import { NonRecordingSpan } from '../../src/trace/NonRecordingSpan';
import { NoopTracer } from '../../src/trace/NoopTracer';

describe('ProxyTracer', () => {
let provider: ProxyTracerProvider;
Expand Down