Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: create NoopSpan instead reusing NOOP_SPAN #1899

Merged
merged 10 commits into from
Feb 10, 2021
2 changes: 0 additions & 2 deletions packages/opentelemetry-api/src/trace/NoopSpan.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
6 changes: 3 additions & 3 deletions packages/opentelemetry-api/src/trace/NoopTracer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand All @@ -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);
Expand All @@ -39,7 +39,7 @@ export class NoopTracer implements Tracer {
) {
return new NoopSpan(parentFromContext);
} else {
return NOOP_SPAN;
return new NoopSpan();
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,28 +17,26 @@
import * as assert from 'assert';
import {
NoopTracer,
NOOP_SPAN,
SpanContext,
SpanKind,
TraceFlags,
context,
setSpanContext,
NoopSpan,
} from '../../src';

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
);
});

Expand Down
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 {
NoopSpan,
NOOP_SPAN,
ProxyTracerProvider,
SpanKind,
TracerProvider,
Expand Down Expand Up @@ -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
);
});
});
Expand Down
2 changes: 1 addition & 1 deletion packages/opentelemetry-tracing/src/Tracer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ export class Tracer implements api.Tracer {
): api.Span {
if (api.isInstrumentationSuppressed(context)) {
this.logger.debug('Instrumentation suppressed, returning Noop Span');
return api.NOOP_SPAN;
return new api.NoopSpan();
}

const parentContext = getParent(options, context);
Expand Down
3 changes: 1 addition & 2 deletions packages/opentelemetry-tracing/test/Tracer.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ import {
NoopSpan,
Sampler,
SamplingDecision,
NOOP_SPAN,
TraceFlags,
ROOT_CONTEXT,
suppressInstrumentation,
Expand Down Expand Up @@ -130,7 +129,7 @@ describe('Tracer', () => {

const span = tracer.startSpan('span3', undefined, context);

assert.equal(span, NOOP_SPAN);
assert.ok(span instanceof NoopSpan);
span.end();

done();
Expand Down