Skip to content

Commit

Permalink
chore: create NoopSpan instead reusing NOOP_SPAN (open-telemetry#1899)
Browse files Browse the repository at this point in the history
  • Loading branch information
Flarna authored and dyladan committed Feb 18, 2021
1 parent 14c77f8 commit 453a101
Show file tree
Hide file tree
Showing 8 changed files with 23 additions and 30 deletions.
3 changes: 2 additions & 1 deletion 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 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 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 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 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
2 changes: 1 addition & 1 deletion api/test/noop-implementations/noop-span.test.ts
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
18 changes: 8 additions & 10 deletions api/test/noop-implementations/noop-tracer.test.ts
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
19 changes: 8 additions & 11 deletions api/test/proxy-implementations/proxy-tracer.test.ts
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

0 comments on commit 453a101

Please sign in to comment.