Skip to content

Commit 2b1f3bd

Browse files
committed
feat(api): propagate spanContext only using API #1456
1 parent 3309ea4 commit 2b1f3bd

File tree

23 files changed

+121
-71
lines changed

23 files changed

+121
-71
lines changed

benchmark/propagator.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ for (const setup of setups) {
3131
const suite = benchmark(100)
3232
.add('#Inject', function () {
3333
propagator.inject(
34-
opentelemetry.setExtractedSpanContext(Context.ROOT_CONTEXT, {
34+
api.setExtractedSpanContext(Context.ROOT_CONTEXT, {
3535
traceId: 'd4cda95b652f4a1592b449d5929fda1b',
3636
spanId: '6e0c63257de34c92'
3737
}), setup.injectCarrier, api.defaultSetter);

packages/opentelemetry-core/src/context/context.ts renamed to packages/opentelemetry-api/src/context/context.ts

+4-3
Original file line numberDiff line numberDiff line change
@@ -14,23 +14,24 @@
1414
* limitations under the License.
1515
*/
1616

17-
import { Span, SpanContext } from '@opentelemetry/api';
17+
import { Span, SpanContext } from '../';
1818
import { Context } from '@opentelemetry/context-base';
1919

2020
/**
2121
* Active span key
2222
*/
23-
export const ACTIVE_SPAN_KEY = Context.createKey(
23+
const ACTIVE_SPAN_KEY = Context.createKey(
2424
'OpenTelemetry Context Key ACTIVE_SPAN'
2525
);
26+
2627
const EXTRACTED_SPAN_CONTEXT_KEY = Context.createKey(
2728
'OpenTelemetry Context Key EXTRACTED_SPAN_CONTEXT'
2829
);
2930
/**
3031
* Shared key for indicating if instrumentation should be suppressed beyond
3132
* this current scope.
3233
*/
33-
export const SUPPRESS_INSTRUMENTATION_KEY = Context.createKey(
34+
const SUPPRESS_INSTRUMENTATION_KEY = Context.createKey(
3435
'OpenTelemetry Context Key SUPPRESS_INSTRUMENTATION'
3536
);
3637

packages/opentelemetry-api/src/index.ts

+1
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
export * from './common/Exception';
1818
export * from './common/Logger';
1919
export * from './common/Time';
20+
export * from './context/context';
2021
export * from './context/propagation/getter';
2122
export * from './context/propagation/TextMapPropagator';
2223
export * from './context/propagation/NoopTextMapPropagator';

packages/opentelemetry-api/src/trace/NoopTracer.ts

+27-4
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,11 @@
1414
* limitations under the License.
1515
*/
1616

17-
import { Span, SpanOptions, Tracer } from '..';
18-
import { NOOP_SPAN } from './NoopSpan';
17+
import { Span, SpanOptions, Tracer, SpanContext } from '..';
18+
import { Context } from '@opentelemetry/context-base';
19+
import { NoopSpan, NOOP_SPAN } from './NoopSpan';
20+
import { isSpanContextValid } from './spancontext-utils';
21+
import { getExtractedSpanContext } from '../context/context';
1922

2023
/**
2124
* No-op implementations of {@link Tracer}.
@@ -26,8 +29,19 @@ export class NoopTracer implements Tracer {
2629
}
2730

2831
// startSpan starts a noop span.
29-
startSpan(name: string, options?: SpanOptions): Span {
30-
return NOOP_SPAN;
32+
startSpan(name: string, options?: SpanOptions, context?: Context): Span {
33+
const parent = options?.parent;
34+
const parentFromContext = context && getExtractedSpanContext(context);
35+
if (isSpanContext(parent) && isSpanContextValid(parent)) {
36+
return new NoopSpan(parent);
37+
} else if (
38+
isSpanContext(parentFromContext) &&
39+
isSpanContextValid(parentFromContext)
40+
) {
41+
return new NoopSpan(parentFromContext);
42+
} else {
43+
return NOOP_SPAN;
44+
}
3145
}
3246

3347
withSpan<T extends (...args: unknown[]) => ReturnType<T>>(
@@ -42,4 +56,13 @@ export class NoopTracer implements Tracer {
4256
}
4357
}
4458

59+
function isSpanContext(spanContext: any): spanContext is SpanContext {
60+
return (
61+
typeof spanContext === 'object' &&
62+
typeof spanContext['spanId'] === 'string' &&
63+
typeof spanContext['traceId'] === 'string' &&
64+
typeof spanContext['traceFlags'] === 'number'
65+
);
66+
}
67+
4568
export const NOOP_TRACER = new NoopTracer();

packages/opentelemetry-core/test/context/context.test.ts renamed to packages/opentelemetry-api/test/context/context.test.ts

+7-14
Original file line numberDiff line numberDiff line change
@@ -17,35 +17,28 @@
1717
import * as assert from 'assert';
1818

1919
import {
20-
SUPPRESS_INSTRUMENTATION_KEY,
2120
suppressInstrumentation,
2221
unsuppressInstrumentation,
2322
isInstrumentationSuppressed,
2423
} from '../../src/context/context';
25-
import { Context } from '@opentelemetry/api';
24+
import { Context } from '../../src';
25+
26+
const SUPPRESS_INSTRUMENTATION_KEY = Context.createKey(
27+
'OpenTelemetry Context Key SUPPRESS_INSTRUMENTATION'
28+
);
2629

2730
describe('Context Helpers', () => {
2831
describe('suppressInstrumentation', () => {
2932
it('should set suppress to true', () => {
30-
const expectedValue = true;
3133
const context = suppressInstrumentation(Context.ROOT_CONTEXT);
32-
33-
const value = context.getValue(SUPPRESS_INSTRUMENTATION_KEY);
34-
const boolValue = value as boolean;
35-
36-
assert.equal(boolValue, expectedValue);
34+
assert.deepStrictEqual(isInstrumentationSuppressed(context), true);
3735
});
3836
});
3937

4038
describe('unsuppressInstrumentation', () => {
4139
it('should set suppress to false', () => {
42-
const expectedValue = false;
4340
const context = unsuppressInstrumentation(Context.ROOT_CONTEXT);
44-
45-
const value = context.getValue(SUPPRESS_INSTRUMENTATION_KEY);
46-
const boolValue = value as boolean;
47-
48-
assert.equal(boolValue, expectedValue);
41+
assert.deepStrictEqual(isInstrumentationSuppressed(context), false);
4942
});
5043
});
5144

packages/opentelemetry-api/test/noop-implementations/noop-tracer.test.ts

+39-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,15 @@
1515
*/
1616

1717
import * as assert from 'assert';
18-
import { NoopTracer, NOOP_SPAN, SpanKind } from '../../src';
18+
import {
19+
NoopTracer,
20+
NOOP_SPAN,
21+
SpanContext,
22+
SpanKind,
23+
TraceFlags,
24+
context,
25+
setExtractedSpanContext,
26+
} from '../../src';
1927

2028
describe('NoopTracer', () => {
2129
it('should not crash', () => {
@@ -51,4 +59,34 @@ describe('NoopTracer', () => {
5159
const patchedFn = tracer.bind(fn, NOOP_SPAN);
5260
return patchedFn();
5361
});
62+
63+
it('should propagate valid spanContext on the span (from parent)', () => {
64+
const tracer = new NoopTracer();
65+
const parent: SpanContext = {
66+
traceId: 'd4cda95b652f4a1592b449d5929fda1b',
67+
spanId: '6e0c63257de34c92',
68+
traceFlags: TraceFlags.NONE,
69+
};
70+
const span = tracer.startSpan('test-1', { parent });
71+
assert(span.context().traceId === parent.traceId);
72+
assert(span.context().spanId === parent.spanId);
73+
assert(span.context().traceFlags === parent.traceFlags);
74+
});
75+
76+
it('should propagate valid spanContext on the span (from context)', () => {
77+
const tracer = new NoopTracer();
78+
const parent: SpanContext = {
79+
traceId: 'd4cda95b652f4a1592b449dd92ffda3b',
80+
spanId: '6e0c63ffe4e34c42',
81+
traceFlags: TraceFlags.NONE,
82+
};
83+
const span = tracer.startSpan(
84+
'test-1',
85+
{},
86+
setExtractedSpanContext(context.active(), parent)
87+
);
88+
assert(span.context().traceId === parent.traceId);
89+
assert(span.context().spanId === parent.spanId);
90+
assert(span.context().traceFlags === parent.traceFlags);
91+
});
5492
});

packages/opentelemetry-core/src/context/propagation/B3Propagator.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,9 @@ import {
2020
TextMapPropagator,
2121
SetterFunction,
2222
TraceFlags,
23+
getParentSpanContext,
24+
setExtractedSpanContext,
2325
} from '@opentelemetry/api';
24-
import { getParentSpanContext, setExtractedSpanContext } from '../context';
2526

2627
export const X_B3_TRACE_ID = 'x-b3-traceid';
2728
export const X_B3_SPAN_ID = 'x-b3-spanid';

packages/opentelemetry-core/src/context/propagation/HttpTraceContext.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,10 @@ import {
2121
SetterFunction,
2222
SpanContext,
2323
TraceFlags,
24+
getParentSpanContext,
25+
setExtractedSpanContext,
2426
} from '@opentelemetry/api';
2527
import { TraceState } from '../../trace/TraceState';
26-
import { getParentSpanContext, setExtractedSpanContext } from '../context';
2728

2829
export const TRACE_PARENT_HEADER = 'traceparent';
2930
export const TRACE_STATE_HEADER = 'tracestate';

packages/opentelemetry-core/src/index.ts

-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ export * from './common/time';
2020
export * from './common/types';
2121
export * from './ExportResult';
2222
export * from './version';
23-
export * from './context/context';
2423
export * from './context/propagation/B3Propagator';
2524
export * from './context/propagation/composite';
2625
export * from './context/propagation/HttpTraceContext';

packages/opentelemetry-core/test/context/B3Propagator.test.ts

+2-4
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,11 @@ import {
1919
defaultSetter,
2020
SpanContext,
2121
TraceFlags,
22+
getExtractedSpanContext,
23+
setExtractedSpanContext,
2224
} from '@opentelemetry/api';
2325
import { Context } from '@opentelemetry/context-base';
2426
import * as assert from 'assert';
25-
import {
26-
getExtractedSpanContext,
27-
setExtractedSpanContext,
28-
} from '../../src/context/context';
2927
import {
3028
B3Propagator,
3129
X_B3_FLAGS,

packages/opentelemetry-core/test/context/HttpTraceContext.test.ts

+2-4
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,11 @@ import {
1919
defaultSetter,
2020
SpanContext,
2121
TraceFlags,
22+
getExtractedSpanContext,
23+
setExtractedSpanContext,
2224
} from '@opentelemetry/api';
2325
import { Context } from '@opentelemetry/context-base';
2426
import * as assert from 'assert';
25-
import {
26-
getExtractedSpanContext,
27-
setExtractedSpanContext,
28-
} from '../../src/context/context';
2927
import {
3028
HttpTraceContext,
3129
TRACE_PARENT_HEADER,

packages/opentelemetry-core/test/context/composite.test.ts

+2-4
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ import {
1919
defaultSetter,
2020
TextMapPropagator,
2121
SpanContext,
22+
getExtractedSpanContext,
23+
setExtractedSpanContext,
2224
} from '@opentelemetry/api';
2325
import { Context } from '@opentelemetry/context-base';
2426
import * as assert from 'assert';
@@ -27,10 +29,6 @@ import {
2729
HttpTraceContext,
2830
RandomIdGenerator,
2931
} from '../../src';
30-
import {
31-
getExtractedSpanContext,
32-
setExtractedSpanContext,
33-
} from '../../src/context/context';
3432
import {
3533
B3Propagator,
3634
X_B3_SAMPLED,

packages/opentelemetry-node/test/NodeTracerProvider.test.ts

+1-2
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,12 @@
1414
* limitations under the License.
1515
*/
1616

17-
import { context, TraceFlags } from '@opentelemetry/api';
17+
import { context, TraceFlags, setActiveSpan } from '@opentelemetry/api';
1818
import {
1919
AlwaysOnSampler,
2020
AlwaysOffSampler,
2121
NoopLogger,
2222
NoRecordingSpan,
23-
setActiveSpan,
2423
} from '@opentelemetry/core';
2524
import { AsyncHooksContextManager } from '@opentelemetry/context-async-hooks';
2625
import { Span } from '@opentelemetry/tracing';

packages/opentelemetry-plugin-http/src/http.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,11 @@ import {
2323
Status,
2424
SpanContext,
2525
TraceFlags,
26+
getExtractedSpanContext,
2627
} from '@opentelemetry/api';
2728
import {
2829
BasePlugin,
2930
NoRecordingSpan,
30-
getExtractedSpanContext,
3131
} from '@opentelemetry/core';
3232
import {
3333
ClientRequest,

packages/opentelemetry-plugin-http/test/utils/DummyPropagation.ts

+4-2
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,13 @@
1313
* See the License for the specific language governing permissions and
1414
* limitations under the License.
1515
*/
16-
import { Context, TextMapPropagator, TraceFlags } from '@opentelemetry/api';
1716
import {
17+
Context,
18+
TextMapPropagator,
19+
TraceFlags,
1820
getParentSpanContext,
1921
setExtractedSpanContext,
20-
} from '@opentelemetry/core';
22+
} from '@opentelemetry/api';
2123
import * as http from 'http';
2224

2325
export class DummyPropagation implements TextMapPropagator {

packages/opentelemetry-plugin-https/test/utils/DummyPropagation.ts

+4-2
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,13 @@
1313
* See the License for the specific language governing permissions and
1414
* limitations under the License.
1515
*/
16-
import { Context, TextMapPropagator, TraceFlags } from '@opentelemetry/api';
1716
import {
17+
Context,
18+
TextMapPropagator,
19+
TraceFlags,
1820
setExtractedSpanContext,
1921
getParentSpanContext,
20-
} from '@opentelemetry/core';
22+
} from '@opentelemetry/api';
2123
import * as http from 'http';
2224

2325
export class DummyPropagation implements TextMapPropagator {

packages/opentelemetry-shim-opentracing/src/shim.ts

+4-7
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,8 @@
1616

1717
import * as api from '@opentelemetry/api';
1818
import {
19-
getExtractedSpanContext,
2019
NoopLogger,
21-
setExtractedSpanContext,
2220
setCorrelationContext,
23-
setActiveSpan,
2421
getCorrelationContext,
2522
} from '@opentelemetry/core';
2623
import * as opentracing from 'opentracing';
@@ -56,9 +53,9 @@ function translateSpanOptions(
5653
function getContextWithParent(options: opentracing.SpanOptions) {
5754
if (options.childOf) {
5855
if (options.childOf instanceof SpanShim) {
59-
return setActiveSpan(api.context.active(), options.childOf.getSpan());
56+
return api.setActiveSpan(api.context.active(), options.childOf.getSpan());
6057
} else if (options.childOf instanceof SpanContextShim) {
61-
return setExtractedSpanContext(
58+
return api.setExtractedSpanContext(
6259
api.context.active(),
6360
options.childOf.getSpanContext()
6461
);
@@ -180,7 +177,7 @@ export class TracerShim extends opentracing.Tracer {
180177
carrier,
181178
api.defaultSetter,
182179
setCorrelationContext(
183-
setExtractedSpanContext(api.Context.ROOT_CONTEXT, oTelSpanContext),
180+
api.setExtractedSpanContext(api.Context.ROOT_CONTEXT, oTelSpanContext),
184181
oTelSpanCorrelationContext
185182
)
186183
);
@@ -202,7 +199,7 @@ export class TracerShim extends opentracing.Tracer {
202199
case opentracing.FORMAT_HTTP_HEADERS:
203200
case opentracing.FORMAT_TEXT_MAP: {
204201
const context: api.Context = api.propagation.extract(carrier);
205-
const spanContext = getExtractedSpanContext(context);
202+
const spanContext = api.getExtractedSpanContext(context);
206203
const correlationContext = getCorrelationContext(context);
207204

208205
if (!spanContext) {

0 commit comments

Comments
 (0)