Skip to content

Commit

Permalink
fix: add exported const of NoopSpan instance (#139)
Browse files Browse the repository at this point in the history
* fix: add exported const of NoopSpan instance

* fix: add INVALID_SPAN_CONTEXT as default value

* fix: remove instance suffix
  • Loading branch information
mayurkale22 committed Jul 29, 2019
1 parent 7b46da2 commit 807a56d
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 17 deletions.
16 changes: 7 additions & 9 deletions packages/opentelemetry-basic-tracer/src/BasicTracer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import {
NoopSpan,
randomSpanId,
randomTraceId,
INVALID_SPAN_CONTEXT,
NOOP_SPAN,
ALWAYS_SAMPLER,
} from '@opentelemetry/core';
import { BasicTracerConfig } from '../src/types';
Expand All @@ -32,13 +32,11 @@ import { BinaryFormat, HttpTextFormat } from '@opentelemetry/types';
* This class represents a basic tracer.
*/
export class BasicTracer implements types.Tracer {
static defaultSpan = new NoopSpan(INVALID_SPAN_CONTEXT);

private _defaultAttributes: types.Attributes;
private _binaryFormat: types.BinaryFormat;
private _httpTextFormat: types.HttpTextFormat;
private _sampler: types.Sampler;
private _scopeManager: ScopeManager;
private readonly _defaultAttributes: types.Attributes;
private readonly _binaryFormat: types.BinaryFormat;
private readonly _httpTextFormat: types.HttpTextFormat;
private readonly _sampler: types.Sampler;
private readonly _scopeManager: ScopeManager;

/**
* Constructs a new Tracer instance.
Expand Down Expand Up @@ -74,7 +72,7 @@ export class BasicTracer implements types.Tracer {
if (!this._sampler.shouldSample(parentSpanContext)) {
// TODO: propagate SpanContext, for more information see
// https://github.com/open-telemetry/opentelemetry-js/pull/99#issuecomment-513325536
return BasicTracer.defaultSpan;
return NOOP_SPAN;
}

// span context
Expand Down
3 changes: 2 additions & 1 deletion packages/opentelemetry-basic-tracer/test/BasicTracer.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import {
ALWAYS_SAMPLER,
NEVER_SAMPLER,
NoopLogger,
NOOP_SPAN,
} from '@opentelemetry/core';
import { NoopScopeManager } from '@opentelemetry/scope-base';

Expand Down Expand Up @@ -101,7 +102,7 @@ describe('BasicTracer', () => {
scopeManager: new NoopScopeManager(),
});
const span = tracer.startSpan('my-span');
assert.deepStrictEqual(span, BasicTracer.defaultSpan);
assert.deepStrictEqual(span, NOOP_SPAN);
});

// @todo: implement
Expand Down
9 changes: 7 additions & 2 deletions packages/opentelemetry-core/src/trace/NoopSpan.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,17 @@

import * as types from '@opentelemetry/types';
import { SpanContext } from '@opentelemetry/types';
import { INVALID_SPAN_CONTEXT } from '../trace/spancontext-utils';

/**
* The NoopSpan is the default {@link Span} that is used when no Span
* implementation is available. All operations are no-op except context
* implementation is available. All operations are no-op including context
* propagation.
*/
export class NoopSpan implements types.Span {
constructor(private readonly _spanContext: SpanContext) {}
constructor(
private readonly _spanContext: SpanContext = INVALID_SPAN_CONTEXT
) {}

// Returns a SpanContext.
context(): types.SpanContext {
Expand Down Expand Up @@ -68,3 +71,5 @@ export class NoopSpan implements types.Span {
return false;
}
}

export const NOOP_SPAN = new NoopSpan();
5 changes: 1 addition & 4 deletions packages/opentelemetry-core/src/trace/NoopTracer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,7 @@ import {
} from '@opentelemetry/types';
import { NOOP_HTTP_TEXT_FORMAT } from '../context/propagation/NoopHttpTextFormat';
import { NOOP_BINARY_FORMAT } from '../context/propagation/NoopBinaryFormat';
import { NoopSpan } from './NoopSpan';
import { INVALID_SPAN_CONTEXT } from './spancontext-utils';

export const NOOP_SPAN = new NoopSpan(INVALID_SPAN_CONTEXT);
import { NOOP_SPAN } from './NoopSpan';

/**
* No-op implementations of {@link Tracer}.
Expand Down
3 changes: 2 additions & 1 deletion packages/opentelemetry-core/test/trace/NoopTracer.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@
*/

import * as assert from 'assert';
import { NoopTracer, NOOP_SPAN } from '../../src/trace/NoopTracer';
import { NoopTracer } from '../../src/trace/NoopTracer';
import { NOOP_SPAN } from '../../src/trace/NoopSpan';
import { SpanKind } from '@opentelemetry/types';

describe('NoopTracer', () => {
Expand Down

0 comments on commit 807a56d

Please sign in to comment.