Skip to content
This repository was archived by the owner on Nov 17, 2025. It is now read-only.

Commit 950433f

Browse files
authored
chore: do not export singletons (#46)
1 parent 4cda9bd commit 950433f

File tree

11 files changed

+39
-42
lines changed

11 files changed

+39
-42
lines changed

src/api/propagation.ts

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,19 +15,19 @@
1515
*/
1616

1717
import { Context } from '../context/types';
18-
import { NOOP_TEXT_MAP_PROPAGATOR } from '../propagation/NoopTextMapPropagator';
18+
import {
19+
getGlobal,
20+
registerGlobal,
21+
unregisterGlobal,
22+
} from '../internal/global-utils';
23+
import { NoopTextMapPropagator } from '../propagation/NoopTextMapPropagator';
1924
import {
2025
defaultTextMapGetter,
2126
defaultTextMapSetter,
2227
TextMapGetter,
2328
TextMapPropagator,
2429
TextMapSetter,
2530
} from '../propagation/TextMapPropagator';
26-
import {
27-
getGlobal,
28-
registerGlobal,
29-
unregisterGlobal,
30-
} from '../internal/global-utils';
3131
import {
3232
getBaggage,
3333
setBaggage,
@@ -36,6 +36,7 @@ import {
3636
import { createBaggage } from '../baggage/utils';
3737

3838
const API_NAME = 'propagation';
39+
const NOOP_TEXT_MAP_PROPAGATOR = new NoopTextMapPropagator();
3940

4041
/**
4142
* Singleton object which represents the entry point to the OpenTelemetry Propagation API

src/index.ts

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,9 @@ export { baggageEntryMetadataFromString } from './baggage/utils';
1919
export * from './common/Exception';
2020
export * from './common/Time';
2121
export * from './diag';
22-
export * from './propagation/NoopTextMapPropagator';
2322
export * from './propagation/TextMapPropagator';
2423
export * from './trace/attributes';
2524
export * from './trace/link';
26-
export * from './trace/NoopTracer';
27-
export * from './trace/NoopTracerProvider';
2825
export * from './trace/ProxyTracer';
2926
export * from './trace/ProxyTracerProvider';
3027
export * from './trace/Sampler';
@@ -49,7 +46,6 @@ export {
4946
} from './trace/spancontext-utils';
5047

5148
export * from './context/context';
52-
export * from './context/NoopContextManager';
5349
export * from './context/types';
5450

5551
import { ContextAPI } from './api/context';

src/propagation/NoopTextMapPropagator.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,5 +31,3 @@ export class NoopTextMapPropagator implements TextMapPropagator {
3131
return [];
3232
}
3333
}
34-
35-
export const NOOP_TEXT_MAP_PROPAGATOR = new NoopTextMapPropagator();

src/trace/NoopTracer.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -95,5 +95,3 @@ function isSpanContext(spanContext: any): spanContext is SpanContext {
9595
typeof spanContext['traceFlags'] === 'number'
9696
);
9797
}
98-
99-
export const NOOP_TRACER = new NoopTracer();

src/trace/NoopTracerProvider.ts

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
* limitations under the License.
1515
*/
1616

17-
import { NOOP_TRACER } from './NoopTracer';
17+
import { NoopTracer } from './NoopTracer';
1818
import { Tracer } from './tracer';
1919
import { TracerProvider } from './tracer_provider';
2020

@@ -26,8 +26,6 @@ import { TracerProvider } from './tracer_provider';
2626
*/
2727
export class NoopTracerProvider implements TracerProvider {
2828
getTracer(_name?: string, _version?: string): Tracer {
29-
return NOOP_TRACER;
29+
return new NoopTracer();
3030
}
3131
}
32-
33-
export const NOOP_TRACER_PROVIDER = new NoopTracerProvider();

src/trace/ProxyTracer.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
*/
1616

1717
import { Context } from '../context/types';
18-
import { NOOP_TRACER } from './NoopTracer';
18+
import { NoopTracer } from './NoopTracer';
1919
import { ProxyTracerProvider } from './ProxyTracerProvider';
2020
import { Span } from './span';
2121
import { SpanOptions } from './SpanOptions';
@@ -60,7 +60,7 @@ export class ProxyTracer implements Tracer {
6060
const tracer = this._provider.getDelegateTracer(this.name, this.version);
6161

6262
if (!tracer) {
63-
return NOOP_TRACER;
63+
return new NoopTracer();
6464
}
6565

6666
this._delegate = tracer;

src/trace/ProxyTracerProvider.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,9 @@
1717
import { Tracer } from './tracer';
1818
import { TracerProvider } from './tracer_provider';
1919
import { ProxyTracer } from './ProxyTracer';
20-
import { NOOP_TRACER_PROVIDER } from './NoopTracerProvider';
20+
import { NoopTracerProvider } from './NoopTracerProvider';
21+
22+
const NOOP_TRACER_PROVIDER = new NoopTracerProvider();
2123

2224
/**
2325
* Tracer provider which provides {@link ProxyTracer}s.

test/api/api.test.ts

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -16,25 +16,25 @@
1616

1717
import * as assert from 'assert';
1818
import api, {
19-
TraceFlags,
20-
NoopTracerProvider,
21-
NoopTracer,
22-
SpanOptions,
23-
Span,
2419
context,
25-
trace,
26-
propagation,
27-
TextMapPropagator,
2820
Context,
29-
TextMapSetter,
30-
TextMapGetter,
31-
ROOT_CONTEXT,
32-
defaultTextMapSetter,
3321
defaultTextMapGetter,
22+
defaultTextMapSetter,
3423
diag,
24+
propagation,
25+
ROOT_CONTEXT,
26+
Span,
27+
SpanOptions,
28+
TextMapGetter,
29+
TextMapPropagator,
30+
TextMapSetter,
31+
trace,
32+
TraceFlags,
3533
} from '../../src';
3634
import { DiagAPI } from '../../src/api/diag';
3735
import { NonRecordingSpan } from '../../src/trace/NonRecordingSpan';
36+
import { NoopTracer } from '../../src/trace/NoopTracer';
37+
import { NoopTracerProvider } from '../../src/trace/NoopTracerProvider';
3838

3939
// DiagLogger implementation
4040
const diagLoggerFunctions = [

test/internal/global.test.ts

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -67,28 +67,31 @@ describe('Global Utils', () => {
6767
});
6868

6969
it('should disable both if one is disabled', () => {
70-
const original = api1.context['_getContextManager']();
71-
72-
api1.context.setGlobalContextManager(new NoopContextManager());
70+
const manager = new NoopContextManager();
71+
api1.context.setGlobalContextManager(manager);
7372

74-
assert.notStrictEqual(original, api1.context['_getContextManager']());
73+
assert.strictEqual(manager, api1.context['_getContextManager']());
7574
api2.context.disable();
76-
assert.strictEqual(original, api1.context['_getContextManager']());
75+
assert.notStrictEqual(manager, api1.context['_getContextManager']());
7776
});
7877

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

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

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

91-
assert.strictEqual(api1.context['_getContextManager'](), original);
90+
// ensure new context manager is not returned because version above is incompatible
91+
assert.notStrictEqual(
92+
api1.context['_getContextManager'](),
93+
newContextManager
94+
);
9295
});
9396

9497
it('should log an error if there is a duplicate registration', () => {

test/noop-implementations/noop-tracer.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,14 @@
1717
import * as assert from 'assert';
1818
import {
1919
context,
20-
NoopTracer,
2120
Span,
2221
SpanContext,
2322
SpanKind,
2423
trace,
2524
TraceFlags,
2625
} from '../../src';
2726
import { NonRecordingSpan } from '../../src/trace/NonRecordingSpan';
27+
import { NoopTracer } from '../../src/trace/NoopTracer';
2828

2929
describe('NoopTracer', () => {
3030
it('should not crash', () => {

0 commit comments

Comments
 (0)