From fb674d6e2572391f7bcb854fd51617ba05c24546 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gerhard=20St=C3=B6bich?= Date: Wed, 10 Feb 2021 16:58:46 +0100 Subject: [PATCH] chore: create NoopSpan instead reusing NOOP_SPAN (#1899) --- api/src/context/context.ts | 3 ++- api/src/index.ts | 1 - api/src/trace/NoopSpan.ts | 2 -- api/src/trace/NoopTracer.ts | 6 +++--- api/test/api/api.test.ts | 2 +- .../noop-implementations/noop-span.test.ts | 2 +- .../noop-implementations/noop-tracer.test.ts | 18 ++++++++---------- .../proxy-tracer.test.ts | 19 ++++++++----------- 8 files changed, 23 insertions(+), 30 deletions(-) diff --git a/api/src/context/context.ts b/api/src/context/context.ts index 4dc2b4c47c..86e7aabec3 100644 --- a/api/src/context/context.ts +++ b/api/src/context/context.ts @@ -15,7 +15,8 @@ */ import { Context, createContextKey } from '@opentelemetry/context-base'; -import { Baggage, NoopSpan, Span, SpanContext } from '../'; +import { Baggage, Span, SpanContext } from '../'; +import { NoopSpan } from '../trace/NoopSpan'; /** * span key diff --git a/api/src/index.ts b/api/src/index.ts index 4c6b64ffd6..b3cbbd0f0f 100644 --- a/api/src/index.ts +++ b/api/src/index.ts @@ -26,7 +26,6 @@ export * from './trace/Event'; export * from './trace/link_context'; export * from './trace/link'; export * from './trace/NoopLogger'; -export * from './trace/NoopSpan'; export * from './trace/NoopTracer'; export * from './trace/NoopTracerProvider'; export * from './trace/ProxyTracer'; diff --git a/api/src/trace/NoopSpan.ts b/api/src/trace/NoopSpan.ts index 85406098e1..40f238d3ad 100644 --- a/api/src/trace/NoopSpan.ts +++ b/api/src/trace/NoopSpan.ts @@ -73,5 +73,3 @@ export class NoopSpan implements Span { // By default does nothing recordException(_exception: Exception, _time?: TimeInput): void {} } - -export const NOOP_SPAN = new NoopSpan(); diff --git a/api/src/trace/NoopTracer.ts b/api/src/trace/NoopTracer.ts index bb25bcbe1d..683a7e5ab2 100644 --- a/api/src/trace/NoopTracer.ts +++ b/api/src/trace/NoopTracer.ts @@ -16,7 +16,7 @@ import { Span, SpanOptions, Tracer, SpanContext } from '..'; import { Context } from '@opentelemetry/context-base'; -import { NoopSpan, NOOP_SPAN } from './NoopSpan'; +import { NoopSpan } from './NoopSpan'; import { isSpanContextValid } from './spancontext-utils'; import { getSpanContext } from '../context/context'; @@ -28,7 +28,7 @@ export class NoopTracer implements Tracer { startSpan(name: string, options?: SpanOptions, context?: Context): Span { const root = Boolean(options?.root); if (root) { - return NOOP_SPAN; + return new NoopSpan(); } const parentFromContext = context && getSpanContext(context); @@ -39,7 +39,7 @@ export class NoopTracer implements Tracer { ) { return new NoopSpan(parentFromContext); } else { - return NOOP_SPAN; + return new NoopSpan(); } } } diff --git a/api/test/api/api.test.ts b/api/test/api/api.test.ts index b3e7728095..609077f93e 100644 --- a/api/test/api/api.test.ts +++ b/api/test/api/api.test.ts @@ -17,7 +17,6 @@ import * as assert from 'assert'; import api, { TraceFlags, - NoopSpan, NoopTracerProvider, NoopTracer, SpanOptions, @@ -33,6 +32,7 @@ import api, { defaultTextMapSetter, defaultTextMapGetter, } from '../../src'; +import { NoopSpan } from '../../src/trace/NoopSpan'; describe('API', () => { it('should expose a tracer provider via getTracerProvider', () => { diff --git a/api/test/noop-implementations/noop-span.test.ts b/api/test/noop-implementations/noop-span.test.ts index f05beae0b7..d2974fca7e 100644 --- a/api/test/noop-implementations/noop-span.test.ts +++ b/api/test/noop-implementations/noop-span.test.ts @@ -19,9 +19,9 @@ import { SpanStatusCode, INVALID_SPANID, INVALID_TRACEID, - NoopSpan, TraceFlags, } from '../../src'; +import { NoopSpan } from '../../src/trace/NoopSpan'; describe('NoopSpan', () => { it('do not crash', () => { diff --git a/api/test/noop-implementations/noop-tracer.test.ts b/api/test/noop-implementations/noop-tracer.test.ts index 3730968323..661ff34dbc 100644 --- a/api/test/noop-implementations/noop-tracer.test.ts +++ b/api/test/noop-implementations/noop-tracer.test.ts @@ -17,28 +17,26 @@ import * as assert from 'assert'; import { NoopTracer, - NOOP_SPAN, SpanContext, SpanKind, TraceFlags, context, setSpanContext, } from '../../src'; +import { NoopSpan } from '../../src/trace/NoopSpan'; describe('NoopTracer', () => { it('should not crash', () => { const tracer = new NoopTracer(); - assert.deepStrictEqual(tracer.startSpan('span-name'), NOOP_SPAN); - assert.deepStrictEqual( - tracer.startSpan('span-name1', { kind: SpanKind.CLIENT }), - NOOP_SPAN + assert.ok(tracer.startSpan('span-name') instanceof NoopSpan); + assert.ok( + tracer.startSpan('span-name1', { kind: SpanKind.CLIENT }) instanceof + NoopSpan ); - assert.deepStrictEqual( - tracer.startSpan('span-name2', { - kind: SpanKind.CLIENT, - }), - NOOP_SPAN + assert.ok( + tracer.startSpan('span-name2', { kind: SpanKind.CLIENT }) instanceof + NoopSpan ); }); diff --git a/api/test/proxy-implementations/proxy-tracer.test.ts b/api/test/proxy-implementations/proxy-tracer.test.ts index 9170f3e44a..c6e2756ca5 100644 --- a/api/test/proxy-implementations/proxy-tracer.test.ts +++ b/api/test/proxy-implementations/proxy-tracer.test.ts @@ -17,8 +17,6 @@ import * as assert from 'assert'; import * as sinon from 'sinon'; import { - NoopSpan, - NOOP_SPAN, ProxyTracerProvider, SpanKind, TracerProvider, @@ -29,6 +27,7 @@ import { ROOT_CONTEXT, SpanOptions, } from '../../src'; +import { NoopSpan } from '../../src/trace/NoopSpan'; describe('ProxyTracer', () => { let provider: ProxyTracerProvider; @@ -52,16 +51,14 @@ describe('ProxyTracer', () => { it('startSpan should return Noop Spans', () => { const tracer = provider.getTracer('test'); - assert.deepStrictEqual(tracer.startSpan('span-name'), NOOP_SPAN); - assert.deepStrictEqual( - tracer.startSpan('span-name1', { kind: SpanKind.CLIENT }), - NOOP_SPAN + assert.ok(tracer.startSpan('span-name') instanceof NoopSpan); + assert.ok( + tracer.startSpan('span-name1', { kind: SpanKind.CLIENT }) instanceof + NoopSpan ); - assert.deepStrictEqual( - tracer.startSpan('span-name2', { - kind: SpanKind.CLIENT, - }), - NOOP_SPAN + assert.ok( + tracer.startSpan('span-name2', { kind: SpanKind.CLIENT }) instanceof + NoopSpan ); }); });