Skip to content

Commit

Permalink
feat(api): propagate spanContext only using API #1456
Browse files Browse the repository at this point in the history
  • Loading branch information
vmarchaud committed Sep 13, 2020
1 parent f824195 commit c1f21f2
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 13 deletions.
12 changes: 4 additions & 8 deletions packages/opentelemetry-api/src/metrics/NoopMeter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -131,24 +131,21 @@ export class NoopMetric<T> implements UnboundMetric<T> {
}
}

export class NoopCounterMetric
extends NoopMetric<BoundCounter>
export class NoopCounterMetric extends NoopMetric<BoundCounter>
implements Counter {
add(value: number, labels: Labels) {
this.bind(labels).add(value);
}
}

export class NoopValueRecorderMetric
extends NoopMetric<BoundValueRecorder>
export class NoopValueRecorderMetric extends NoopMetric<BoundValueRecorder>
implements ValueRecorder {
record(value: number, labels: Labels) {
this.bind(labels).record(value);
}
}

export class NoopBaseObserverMetric
extends NoopMetric<BoundBaseObserver>
export class NoopBaseObserverMetric extends NoopMetric<BoundBaseObserver>
implements BaseObserver {
observation() {
return {
Expand All @@ -158,8 +155,7 @@ export class NoopBaseObserverMetric
}
}

export class NoopBatchObserverMetric
extends NoopMetric<void>
export class NoopBatchObserverMetric extends NoopMetric<void>
implements BatchObserver {}

export class NoopBoundCounter implements BoundCounter {
Expand Down
38 changes: 34 additions & 4 deletions packages/opentelemetry-api/src/trace/NoopTracer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,17 @@
* limitations under the License.
*/

import { Span, SpanOptions, Tracer } from '..';
import { NOOP_SPAN } from './NoopSpan';
import { Span, SpanOptions, Tracer, SpanContext } from '..';
import { Context } from '@opentelemetry/context-base';
import { NoopSpan, NOOP_SPAN } from './NoopSpan';
import { isSpanContextValid } from './spancontext-utils';

/**
* Internal key used by the core to store spanContext in the global context
*/
export const _EXTRACTED_SPAN_CONTEXT_KEY = Context.createKey(
'OpenTelemetry Context Key EXTRACTED_SPAN_CONTEXT'
);

/**
* No-op implementations of {@link Tracer}.
Expand All @@ -26,8 +35,20 @@ export class NoopTracer implements Tracer {
}

// startSpan starts a noop span.
startSpan(name: string, options?: SpanOptions): Span {
return NOOP_SPAN;
startSpan(name: string, options?: SpanOptions, context?: Context): Span {
const parent = options?.parent;
const parentFromContext =
context && context.getValue(_EXTRACTED_SPAN_CONTEXT_KEY);
if (isSpanContext(parent) && isSpanContextValid(parent)) {
return new NoopSpan(parent);
} else if (
isSpanContext(parentFromContext) &&
isSpanContextValid(parentFromContext)
) {
return new NoopSpan(parentFromContext);
} else {
return NOOP_SPAN;
}
}

withSpan<T extends (...args: unknown[]) => ReturnType<T>>(
Expand All @@ -42,4 +63,13 @@ export class NoopTracer implements Tracer {
}
}

function isSpanContext(spanContext: any): spanContext is SpanContext {
return (
typeof spanContext === 'object' &&
typeof spanContext['spanId'] === 'string' &&
typeof spanContext['traceId'] === 'string' &&
typeof spanContext['traceFlags'] === 'number'
);
}

export const NOOP_TRACER = new NoopTracer();
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,15 @@
*/

import * as assert from 'assert';
import { NoopTracer, NOOP_SPAN, SpanKind } from '../../src';
import {
NoopTracer,
NOOP_SPAN,
SpanContext,
SpanKind,
TraceFlags,
context,
_EXTRACTED_SPAN_CONTEXT_KEY,
} from '../../src';

describe('NoopTracer', () => {
it('should not crash', () => {
Expand Down Expand Up @@ -51,4 +59,34 @@ describe('NoopTracer', () => {
const patchedFn = tracer.bind(fn, NOOP_SPAN);
return patchedFn();
});

it('should propagate valid spanContext on the span (from parent)', () => {
const tracer = new NoopTracer();
const parent: SpanContext = {
traceId: 'd4cda95b652f4a1592b449d5929fda1b',
spanId: '6e0c63257de34c92',
traceFlags: TraceFlags.NONE,
};
const span = tracer.startSpan('test-1', { parent });
assert(span.context().traceId === parent.traceId);
assert(span.context().spanId === parent.spanId);
assert(span.context().traceFlags === parent.traceFlags);
});

it('should propagate valid spanContext on the span (from context)', () => {
const tracer = new NoopTracer();
const parent: SpanContext = {
traceId: 'd4cda95b652f4a1592b449dd92ffda3b',
spanId: '6e0c63ffe4e34c42',
traceFlags: TraceFlags.NONE,
};
const span = tracer.startSpan(
'test-1',
{},
context.active().setValue(_EXTRACTED_SPAN_CONTEXT_KEY, parent)
);
assert(span.context().traceId === parent.traceId);
assert(span.context().spanId === parent.spanId);
assert(span.context().traceFlags === parent.traceFlags);
});
});

0 comments on commit c1f21f2

Please sign in to comment.