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
3 changes: 2 additions & 1 deletion packages/opentelemetry-api/src/context/context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 0 additions & 1 deletion packages/opentelemetry-api/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down
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
2 changes: 1 addition & 1 deletion packages/opentelemetry-api/test/api/api.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
import * as assert from 'assert';
import api, {
TraceFlags,
NoopSpan,
NoopTracerProvider,
NoopTracer,
SpanOptions,
Expand All @@ -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', () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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', () => {
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,
} 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
);
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,6 @@
import * as assert from 'assert';
import * as sinon from 'sinon';
import {
NoopSpan,
NOOP_SPAN,
ProxyTracerProvider,
SpanKind,
TracerProvider,
Expand All @@ -29,6 +27,7 @@ import {
ROOT_CONTEXT,
SpanOptions,
} from '../../src';
import { NoopSpan } from '../../src/trace/NoopSpan';

describe('ProxyTracer', () => {
let provider: ProxyTracerProvider;
Expand All @@ -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
4 changes: 2 additions & 2 deletions packages/opentelemetry-instrumentation-http/src/http.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ import {
ROOT_CONTEXT,
getSpan,
suppressInstrumentation,
NoopSpan,
NOOP_TRACER,
} from '@opentelemetry/api';
import type * as http from 'http';
import type * as https from 'https';
Expand Down Expand Up @@ -587,7 +587,7 @@ export class HttpInstrumentation extends InstrumentationBase<Http> {
if (requireParent === true && currentSpan === undefined) {
// TODO: Refactor this when a solution is found in
// https://github.com/open-telemetry/opentelemetry-specification/issues/530
span = new NoopSpan();
span = NOOP_TRACER.startSpan(name, options);
} else if (requireParent === true && currentSpan?.context().isRemote) {
span = currentSpan;
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ describe('HttpInstrumentation', () => {
});

beforeEach(() => {
NOOP_TRACER.startSpan = sinon.spy();
sinon.spy(NOOP_TRACER, 'startSpan');
});

afterEach(() => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ describe('HttpsInstrumentation', () => {
});

beforeEach(() => {
NOOP_TRACER.startSpan = sinon.spy();
sinon.spy(NOOP_TRACER, 'startSpan');
});

afterEach(() => {
Expand Down
2 changes: 0 additions & 2 deletions packages/opentelemetry-node/test/NodeTracerProvider.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ import {
setSpan,
setSpanContext,
getSpan,
NoopSpan,
} from '@opentelemetry/api';
import { AlwaysOnSampler, AlwaysOffSampler } from '@opentelemetry/core';
import { AsyncHooksContextManager } from '@opentelemetry/context-async-hooks';
Expand Down Expand Up @@ -121,7 +120,6 @@ describe('NodeTracerProvider', () => {
logger: new NoopLogger(),
});
const span = provider.getTracer('default').startSpan('my-span');
assert.ok(span instanceof NoopSpan);
assert.strictEqual(span.context().traceFlags, TraceFlags.NONE);
assert.strictEqual(span.isRecording(), false);
});
Expand Down
4 changes: 2 additions & 2 deletions packages/opentelemetry-plugin-http/src/http.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ import {
ROOT_CONTEXT,
getSpan,
suppressInstrumentation,
NoopSpan,
NOOP_TRACER,
} from '@opentelemetry/api';
import { BasePlugin } from '@opentelemetry/core';
import type {
Expand Down Expand Up @@ -446,7 +446,7 @@ export class HttpPlugin extends BasePlugin<Http> {
if (requireParent === true && currentSpan === undefined) {
// TODO: Refactor this when a solution is found in
// https://github.com/open-telemetry/opentelemetry-specification/issues/530
span = new NoopSpan();
span = NOOP_TRACER.startSpan(name, options);
} else if (requireParent === true && currentSpan?.context().isRemote) {
span = currentSpan;
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ describe('HttpPlugin', () => {
});

beforeEach(() => {
NOOP_TRACER.startSpan = sinon.spy();
sinon.spy(NOOP_TRACER, 'startSpan');
});

afterEach(() => {
Expand Down
8 changes: 6 additions & 2 deletions packages/opentelemetry-tracing/src/Tracer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,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 api.NOOP_TRACER.startSpan(name, options, context);
}

const parentContext = getParent(options, context);
Expand Down Expand Up @@ -103,7 +103,11 @@ export class Tracer implements api.Tracer {
const spanContext = { traceId, spanId, traceFlags, traceState };
if (samplingResult.decision === api.SamplingDecision.NOT_RECORD) {
this.logger.debug('Recording is off, starting no recording span');
return new api.NoopSpan(spanContext);
return api.NOOP_TRACER.startSpan(
name,
options,
api.setSpanContext(context, spanContext)
);
}

const span = new Span(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ import {
TraceFlags,
ROOT_CONTEXT,
NoopLogger,
NoopSpan,
setSpan,
setSpanContext,
getSpan,
Expand Down Expand Up @@ -258,13 +257,13 @@ describe('BasicTracerProvider', () => {
assert.deepStrictEqual(context.traceState, undefined);
});

it('should return a no recording span when never sampling', () => {
it('should return a non recording span when never sampling', () => {
const tracer = new BasicTracerProvider({
sampler: new AlwaysOffSampler(),
logger: new NoopLogger(),
}).getTracer('default');
const span = tracer.startSpan('my-span');
assert.ok(span instanceof NoopSpan);
assert.ok(!span.isRecording());
const context = span.context();
assert.ok(context.traceId.match(/[a-f0-9]{32}/));
assert.ok(context.spanId.match(/[a-f0-9]{16}/));
Expand Down
8 changes: 3 additions & 5 deletions packages/opentelemetry-tracing/test/Tracer.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,8 @@

import * as assert from 'assert';
import {
NoopSpan,
Sampler,
SamplingDecision,
NOOP_SPAN,
TraceFlags,
ROOT_CONTEXT,
suppressInstrumentation,
Expand Down Expand Up @@ -79,7 +77,7 @@ describe('Tracer', () => {
tracerProvider
);
const span = tracer.startSpan('span1');
assert.ok(span instanceof NoopSpan);
assert.ok(!span.isRecording());
span.end();
});

Expand All @@ -90,7 +88,7 @@ describe('Tracer', () => {
tracerProvider
);
const span = tracer.startSpan('span2');
assert.ok(!(span instanceof NoopSpan));
assert.ok(span.isRecording());
span.end();
});

Expand Down Expand Up @@ -130,7 +128,7 @@ describe('Tracer', () => {

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

assert.equal(span, NOOP_SPAN);
assert.ok(!span.isRecording());
span.end();

done();
Expand Down