Skip to content

Commit

Permalink
feat(api): propagate spanContext only using API #1456 (#1527)
Browse files Browse the repository at this point in the history
Co-authored-by: Bartlomiej Obecny <[email protected]>
Co-authored-by: Daniel Dyla <[email protected]>
  • Loading branch information
3 people authored Oct 1, 2020
1 parent aac3f56 commit acaa074
Show file tree
Hide file tree
Showing 23 changed files with 121 additions and 85 deletions.
2 changes: 1 addition & 1 deletion benchmark/propagator.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ for (const setup of setups) {
const suite = benchmark(100)
.add('#Inject', function () {
propagator.inject(
opentelemetry.setExtractedSpanContext(Context.ROOT_CONTEXT, {
api.setExtractedSpanContext(Context.ROOT_CONTEXT, {
traceId: 'd4cda95b652f4a1592b449d5929fda1b',
spanId: '6e0c63257de34c92'
}), setup.injectCarrier, api.defaultSetter);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,13 @@
* limitations under the License.
*/

import {
Span,
SpanContext,
createContextKey,
Context,
} from '@opentelemetry/api';
import { Span, SpanContext } from '../';
import { Context, createContextKey } from '@opentelemetry/context-base';

/**
* Active span key
*/
export const ACTIVE_SPAN_KEY = createContextKey(
const ACTIVE_SPAN_KEY = createContextKey(
'OpenTelemetry Context Key ACTIVE_SPAN'
);
const EXTRACTED_SPAN_CONTEXT_KEY = createContextKey(
Expand All @@ -34,7 +30,7 @@ const EXTRACTED_SPAN_CONTEXT_KEY = createContextKey(
* Shared key for indicating if instrumentation should be suppressed beyond
* this current scope.
*/
export const SUPPRESS_INSTRUMENTATION_KEY = createContextKey(
const SUPPRESS_INSTRUMENTATION_KEY = createContextKey(
'OpenTelemetry Context Key SUPPRESS_INSTRUMENTATION'
);

Expand Down
1 change: 1 addition & 0 deletions packages/opentelemetry-api/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
export * from './common/Exception';
export * from './common/Logger';
export * from './common/Time';
export * from './context/context';
export * from './context/propagation/getter';
export * from './context/propagation/TextMapPropagator';
export * from './context/propagation/NoopTextMapPropagator';
Expand Down
31 changes: 27 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,11 @@
* 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';
import { getExtractedSpanContext } from '../context/context';

/**
* No-op implementations of {@link Tracer}.
Expand All @@ -26,8 +29,19 @@ 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 && getExtractedSpanContext(context);
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 +56,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,37 +15,29 @@
*/

import * as assert from 'assert';

import { ROOT_CONTEXT, createContextKey } from '@opentelemetry/context-base';
import {
SUPPRESS_INSTRUMENTATION_KEY,
suppressInstrumentation,
unsuppressInstrumentation,
isInstrumentationSuppressed,
} from '../../src/context/context';
import { ROOT_CONTEXT } from '@opentelemetry/api';

const SUPPRESS_INSTRUMENTATION_KEY = createContextKey(
'OpenTelemetry Context Key SUPPRESS_INSTRUMENTATION'
);

describe('Context Helpers', () => {
describe('suppressInstrumentation', () => {
it('should set suppress to true', () => {
const expectedValue = true;
const context = suppressInstrumentation(ROOT_CONTEXT);

const value = context.getValue(SUPPRESS_INSTRUMENTATION_KEY);
const boolValue = value as boolean;

assert.equal(boolValue, expectedValue);
assert.deepStrictEqual(isInstrumentationSuppressed(context), true);
});
});

describe('unsuppressInstrumentation', () => {
it('should set suppress to false', () => {
const expectedValue = false;
const context = unsuppressInstrumentation(ROOT_CONTEXT);

const value = context.getValue(SUPPRESS_INSTRUMENTATION_KEY);
const boolValue = value as boolean;

assert.equal(boolValue, expectedValue);
assert.deepStrictEqual(isInstrumentationSuppressed(context), false);
});
});

Expand Down
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,
setExtractedSpanContext,
} 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',
{},
setExtractedSpanContext(context.active(), parent)
);
assert(span.context().traceId === parent.traceId);
assert(span.context().spanId === parent.spanId);
assert(span.context().traceFlags === parent.traceFlags);
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,11 @@ import {
TextMapPropagator,
SetterFunction,
TraceFlags,
createContextKey,
getParentSpanContext,
setExtractedSpanContext,
} from '@opentelemetry/api';
import { getParentSpanContext, setExtractedSpanContext } from '../context';

import { createContextKey } from '@opentelemetry/context-base';

export const X_B3_TRACE_ID = 'x-b3-traceid';
export const X_B3_SPAN_ID = 'x-b3-spanid';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,10 @@ import {
SetterFunction,
SpanContext,
TraceFlags,
getParentSpanContext,
setExtractedSpanContext,
} from '@opentelemetry/api';
import { TraceState } from '../../trace/TraceState';
import { getParentSpanContext, setExtractedSpanContext } from '../context';

export const TRACE_PARENT_HEADER = 'traceparent';
export const TRACE_STATE_HEADER = 'tracestate';
Expand Down
1 change: 0 additions & 1 deletion packages/opentelemetry-core/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ export * from './common/time';
export * from './common/types';
export * from './ExportResult';
export * from './version';
export * from './context/context';
export * from './context/propagation/B3Propagator';
export * from './context/propagation/composite';
export * from './context/propagation/HttpTraceContext';
Expand Down
6 changes: 2 additions & 4 deletions packages/opentelemetry-core/test/context/B3Propagator.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,11 @@ import {
defaultSetter,
SpanContext,
TraceFlags,
getExtractedSpanContext,
setExtractedSpanContext,
} from '@opentelemetry/api';
import { ROOT_CONTEXT } from '@opentelemetry/context-base';
import * as assert from 'assert';
import {
getExtractedSpanContext,
setExtractedSpanContext,
} from '../../src/context/context';
import {
B3Propagator,
X_B3_FLAGS,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,11 @@ import {
defaultSetter,
SpanContext,
TraceFlags,
getExtractedSpanContext,
setExtractedSpanContext,
} from '@opentelemetry/api';
import { ROOT_CONTEXT } from '@opentelemetry/context-base';
import * as assert from 'assert';
import {
getExtractedSpanContext,
setExtractedSpanContext,
} from '../../src/context/context';
import {
HttpTraceContext,
TRACE_PARENT_HEADER,
Expand Down
6 changes: 2 additions & 4 deletions packages/opentelemetry-core/test/context/composite.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ import {
defaultSetter,
TextMapPropagator,
SpanContext,
getExtractedSpanContext,
setExtractedSpanContext,
} from '@opentelemetry/api';
import { Context, ROOT_CONTEXT } from '@opentelemetry/context-base';
import * as assert from 'assert';
Expand All @@ -27,10 +29,6 @@ import {
HttpTraceContext,
RandomIdGenerator,
} from '../../src';
import {
getExtractedSpanContext,
setExtractedSpanContext,
} from '../../src/context/context';
import {
B3Propagator,
X_B3_SAMPLED,
Expand Down
3 changes: 1 addition & 2 deletions packages/opentelemetry-node/test/NodeTracerProvider.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,12 @@
* limitations under the License.
*/

import { context, TraceFlags } from '@opentelemetry/api';
import { context, TraceFlags, setActiveSpan } from '@opentelemetry/api';
import {
AlwaysOnSampler,
AlwaysOffSampler,
NoopLogger,
NoRecordingSpan,
setActiveSpan,
} from '@opentelemetry/core';
import { AsyncHooksContextManager } from '@opentelemetry/context-async-hooks';
import { Span } from '@opentelemetry/tracing';
Expand Down
7 changes: 2 additions & 5 deletions packages/opentelemetry-plugin-http/src/http.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,9 @@ import {
Status,
SpanContext,
TraceFlags,
} from '@opentelemetry/api';
import {
BasePlugin,
NoRecordingSpan,
getExtractedSpanContext,
} from '@opentelemetry/core';
} from '@opentelemetry/api';
import { BasePlugin, NoRecordingSpan } from '@opentelemetry/core';
import type {
ClientRequest,
IncomingMessage,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,13 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import { Context, TextMapPropagator, TraceFlags } from '@opentelemetry/api';
import {
Context,
TextMapPropagator,
TraceFlags,
getParentSpanContext,
setExtractedSpanContext,
} from '@opentelemetry/core';
} from '@opentelemetry/api';
import * as http from 'http';

export class DummyPropagation implements TextMapPropagator {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,13 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import { Context, TextMapPropagator, TraceFlags } from '@opentelemetry/api';
import {
Context,
TextMapPropagator,
TraceFlags,
setExtractedSpanContext,
getParentSpanContext,
} from '@opentelemetry/core';
} from '@opentelemetry/api';
import * as http from 'http';

export class DummyPropagation implements TextMapPropagator {
Expand Down
11 changes: 4 additions & 7 deletions packages/opentelemetry-shim-opentracing/src/shim.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,8 @@

import * as api from '@opentelemetry/api';
import {
getExtractedSpanContext,
NoopLogger,
setExtractedSpanContext,
setCorrelationContext,
setActiveSpan,
getCorrelationContext,
} from '@opentelemetry/core';
import * as opentracing from 'opentracing';
Expand Down Expand Up @@ -57,9 +54,9 @@ function translateSpanOptions(
function getContextWithParent(options: opentracing.SpanOptions) {
if (options.childOf) {
if (options.childOf instanceof SpanShim) {
return setActiveSpan(api.context.active(), options.childOf.getSpan());
return api.setActiveSpan(api.context.active(), options.childOf.getSpan());
} else if (options.childOf instanceof SpanContextShim) {
return setExtractedSpanContext(
return api.setExtractedSpanContext(
api.context.active(),
options.childOf.getSpanContext()
);
Expand Down Expand Up @@ -181,7 +178,7 @@ export class TracerShim extends opentracing.Tracer {
carrier,
api.defaultSetter,
setCorrelationContext(
setExtractedSpanContext(api.ROOT_CONTEXT, oTelSpanContext),
api.setExtractedSpanContext(api.ROOT_CONTEXT, oTelSpanContext),
oTelSpanCorrelationContext
)
);
Expand All @@ -203,7 +200,7 @@ export class TracerShim extends opentracing.Tracer {
case opentracing.FORMAT_HTTP_HEADERS:
case opentracing.FORMAT_TEXT_MAP: {
const context: api.Context = api.propagation.extract(carrier);
const spanContext = getExtractedSpanContext(context);
const spanContext = api.getExtractedSpanContext(context);
const correlationContext = getCorrelationContext(context);

if (!spanContext) {
Expand Down
Loading

0 comments on commit acaa074

Please sign in to comment.