Skip to content

Commit 1c27690

Browse files
mwearobecny
andauthored
feat: simplify active span logic (#1589)
Co-authored-by: Bartlomiej Obecny <[email protected]>
1 parent be720b4 commit 1c27690

File tree

11 files changed

+86
-96
lines changed

11 files changed

+86
-96
lines changed

packages/opentelemetry-api/src/context/context.ts

Lines changed: 8 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
* limitations under the License.
1515
*/
1616

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

2020
/**
@@ -23,9 +23,7 @@ import { Context, createContextKey } from '@opentelemetry/context-base';
2323
const ACTIVE_SPAN_KEY = createContextKey(
2424
'OpenTelemetry Context Key ACTIVE_SPAN'
2525
);
26-
const EXTRACTED_SPAN_CONTEXT_KEY = createContextKey(
27-
'OpenTelemetry Context Key EXTRACTED_SPAN_CONTEXT'
28-
);
26+
2927
/**
3028
* Shared key for indicating if instrumentation should be suppressed beyond
3129
* this current scope.
@@ -54,29 +52,17 @@ export function setActiveSpan(context: Context, span: Span): Context {
5452
}
5553

5654
/**
57-
* Get the extracted span context from a context
58-
*
59-
* @param context context to get span context from
60-
*/
61-
export function getExtractedSpanContext(
62-
context: Context
63-
): SpanContext | undefined {
64-
return (
65-
(context.getValue(EXTRACTED_SPAN_CONTEXT_KEY) as SpanContext) || undefined
66-
);
67-
}
68-
69-
/**
70-
* Set the extracted span context on a context
55+
* Wrap extracted span context in a NoopSpan and set as active span in a new
56+
* context
7157
*
72-
* @param context context to set span context on
73-
* @param spanContext span context to set
58+
* @param context context to set active span on
59+
* @param spanContext span context to be wrapped
7460
*/
7561
export function setExtractedSpanContext(
7662
context: Context,
7763
spanContext: SpanContext
7864
): Context {
79-
return context.setValue(EXTRACTED_SPAN_CONTEXT_KEY, spanContext);
65+
return setActiveSpan(context, new NoopSpan(spanContext));
8066
}
8167

8268
/**
@@ -89,7 +75,7 @@ export function setExtractedSpanContext(
8975
export function getParentSpanContext(
9076
context: Context
9177
): SpanContext | undefined {
92-
return getActiveSpan(context)?.context() || getExtractedSpanContext(context);
78+
return getActiveSpan(context)?.context();
9379
}
9480

9581
/**

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ import { Span, SpanOptions, Tracer, SpanContext } from '..';
1818
import { Context } from '@opentelemetry/context-base';
1919
import { NoopSpan, NOOP_SPAN } from './NoopSpan';
2020
import { isSpanContextValid } from './spancontext-utils';
21-
import { getExtractedSpanContext } from '../context/context';
21+
import { getActiveSpan } from '../context/context';
2222

2323
/**
2424
* No-op implementations of {@link Tracer}.
@@ -31,7 +31,7 @@ export class NoopTracer implements Tracer {
3131
// startSpan starts a noop span.
3232
startSpan(name: string, options?: SpanOptions, context?: Context): Span {
3333
const parent = options?.parent;
34-
const parentFromContext = context && getExtractedSpanContext(context);
34+
const parentFromContext = context && getActiveSpan(context)?.context();
3535
if (isSpanContext(parent) && isSpanContextValid(parent)) {
3636
return new NoopSpan(parent);
3737
} else if (

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

Lines changed: 24 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ import {
1919
defaultSetter,
2020
SpanContext,
2121
TraceFlags,
22-
getExtractedSpanContext,
22+
getActiveSpan,
2323
setExtractedSpanContext,
2424
} from '@opentelemetry/api';
2525
import { ROOT_CONTEXT } from '@opentelemetry/context-base';
@@ -137,7 +137,7 @@ describe('B3MultiPropagator', () => {
137137
carrier,
138138
defaultGetter
139139
);
140-
const extractedSpanContext = getExtractedSpanContext(context);
140+
const extractedSpanContext = getActiveSpan(context)?.context();
141141
assert.deepStrictEqual(extractedSpanContext, {
142142
spanId: 'b7ad6b7169203331',
143143
traceId: '0af7651916cd43dd8448eb211c80319c',
@@ -158,7 +158,7 @@ describe('B3MultiPropagator', () => {
158158
carrier,
159159
defaultGetter
160160
);
161-
const extractedSpanContext = getExtractedSpanContext(context);
161+
const extractedSpanContext = getActiveSpan(context)?.context();
162162

163163
assert.deepStrictEqual(extractedSpanContext, {
164164
spanId: 'b7ad6b7169203331',
@@ -180,7 +180,7 @@ describe('B3MultiPropagator', () => {
180180
carrier,
181181
defaultGetter
182182
);
183-
const extractedSpanContext = getExtractedSpanContext(context);
183+
const extractedSpanContext = getActiveSpan(context)?.context();
184184

185185
assert.deepStrictEqual(extractedSpanContext, {
186186
spanId: 'b7ad6b7169203331',
@@ -202,7 +202,7 @@ describe('B3MultiPropagator', () => {
202202
carrier,
203203
defaultGetter
204204
);
205-
const extractedSpanContext = getExtractedSpanContext(context);
205+
const extractedSpanContext = getActiveSpan(context)?.context();
206206

207207
assert.deepStrictEqual(extractedSpanContext, {
208208
spanId: 'b7ad6b7169203331',
@@ -226,7 +226,7 @@ describe('B3MultiPropagator', () => {
226226
carrier,
227227
defaultGetter
228228
);
229-
const extractedSpanContext = getExtractedSpanContext(context);
229+
const extractedSpanContext = getActiveSpan(context)?.context();
230230

231231
assert.deepStrictEqual(extractedSpanContext, {
232232
spanId: 'b7ad6b7169203331',
@@ -251,7 +251,7 @@ describe('B3MultiPropagator', () => {
251251
carrier,
252252
defaultGetter
253253
);
254-
const extractedSpanContext = getExtractedSpanContext(context);
254+
const extractedSpanContext = getActiveSpan(context)?.context();
255255

256256
assert.deepStrictEqual(extractedSpanContext, {
257257
spanId: 'b7ad6b7169203331',
@@ -274,7 +274,7 @@ describe('B3MultiPropagator', () => {
274274
carrier,
275275
defaultGetter
276276
);
277-
const extractedSpanContext = getExtractedSpanContext(context);
277+
const extractedSpanContext = getActiveSpan(context)?.context();
278278

279279
assert.deepStrictEqual(extractedSpanContext, {
280280
spanId: 'b7ad6b7169203331',
@@ -297,7 +297,7 @@ describe('B3MultiPropagator', () => {
297297
carrier,
298298
defaultGetter
299299
);
300-
const extractedSpanContext = getExtractedSpanContext(context);
300+
const extractedSpanContext = getActiveSpan(context)?.context();
301301

302302
assert.deepStrictEqual(extractedSpanContext, {
303303
spanId: 'b7ad6b7169203331',
@@ -320,7 +320,7 @@ describe('B3MultiPropagator', () => {
320320
carrier,
321321
defaultGetter
322322
);
323-
const extractedSpanContext = getExtractedSpanContext(context);
323+
const extractedSpanContext = getActiveSpan(context)?.context();
324324

325325
assert.deepStrictEqual(extractedSpanContext, {
326326
spanId: 'b7ad6b7169203331',
@@ -338,9 +338,9 @@ describe('B3MultiPropagator', () => {
338338
it('should return undefined', () => {
339339
carrier[X_B3_TRACE_ID] = undefined;
340340
carrier[X_B3_SPAN_ID] = 'b7ad6b7169203331';
341-
const context = getExtractedSpanContext(
341+
const context = getActiveSpan(
342342
b3Propagator.extract(ROOT_CONTEXT, carrier, defaultGetter)
343-
);
343+
)?.context();
344344
assert.deepStrictEqual(context, undefined);
345345
});
346346
});
@@ -349,9 +349,9 @@ describe('B3MultiPropagator', () => {
349349
it('should return undefined', () => {
350350
carrier[X_B3_TRACE_ID] = '0af7651916cd43dd8448eb211c80319c';
351351
carrier[X_B3_SPAN_ID] = undefined;
352-
const context = getExtractedSpanContext(
352+
const context = getActiveSpan(
353353
b3Propagator.extract(ROOT_CONTEXT, carrier, defaultGetter)
354-
);
354+
)?.context();
355355
assert.deepStrictEqual(context, undefined);
356356
});
357357
});
@@ -361,18 +361,18 @@ describe('B3MultiPropagator', () => {
361361
carrier[X_B3_TRACE_ID] = '0af7651916cd43dd8448eb211c80319c';
362362
carrier[X_B3_SPAN_ID] = 'b7ad6b7169203331';
363363
carrier[X_B3_SAMPLED] = '2';
364-
const context = getExtractedSpanContext(
364+
const context = getActiveSpan(
365365
b3Propagator.extract(ROOT_CONTEXT, carrier, defaultGetter)
366-
);
366+
)?.context();
367367
assert.deepStrictEqual(context, undefined);
368368
});
369369
});
370370

371371
describe('AND b3 header is missing', () => {
372372
it('should return undefined', () => {
373-
const context = getExtractedSpanContext(
373+
const context = getActiveSpan(
374374
b3Propagator.extract(ROOT_CONTEXT, carrier, defaultGetter)
375-
);
375+
)?.context();
376376
assert.deepStrictEqual(context, undefined);
377377
});
378378
});
@@ -381,9 +381,9 @@ describe('B3MultiPropagator', () => {
381381
it('should return undefined', () => {
382382
carrier[X_B3_TRACE_ID] = 'invalid!';
383383
carrier[X_B3_SPAN_ID] = 'b7ad6b7169203331';
384-
const context = getExtractedSpanContext(
384+
const context = getActiveSpan(
385385
b3Propagator.extract(ROOT_CONTEXT, carrier, defaultGetter)
386-
);
386+
)?.context();
387387
assert.deepStrictEqual(context, undefined);
388388
});
389389
});
@@ -398,7 +398,7 @@ describe('B3MultiPropagator', () => {
398398
carrier,
399399
defaultGetter
400400
);
401-
const extractedSpanContext = getExtractedSpanContext(context);
401+
const extractedSpanContext = getActiveSpan(context)?.context();
402402
assert.deepStrictEqual(extractedSpanContext, {
403403
spanId: 'b7ad6b7169203331',
404404
traceId: '0af7651916cd43dd8448eb211c80319c',
@@ -447,9 +447,9 @@ describe('B3MultiPropagator', () => {
447447

448448
Object.getOwnPropertyNames(testCases).forEach(testCase => {
449449
carrier[X_B3_TRACE_ID] = testCases[testCase];
450-
const extractedSpanContext = getExtractedSpanContext(
450+
const extractedSpanContext = getActiveSpan(
451451
b3Propagator.extract(ROOT_CONTEXT, carrier, defaultGetter)
452-
);
452+
)?.context();
453453
assert.deepStrictEqual(extractedSpanContext, undefined, testCase);
454454
});
455455
});
@@ -485,7 +485,7 @@ describe('B3MultiPropagator', () => {
485485
carrier,
486486
defaultGetter
487487
);
488-
const extractedSpanContext = getExtractedSpanContext(context);
488+
const extractedSpanContext = getActiveSpan(context)?.context();
489489

490490
assert.deepStrictEqual(extractedSpanContext, {
491491
spanId: 'b7ad6b7169203331',

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ import {
2020
defaultSetter,
2121
SpanContext,
2222
TraceFlags,
23-
getExtractedSpanContext,
23+
getActiveSpan,
2424
setExtractedSpanContext,
2525
} from '@opentelemetry/api';
2626
import { ROOT_CONTEXT } from '@opentelemetry/context-base';
@@ -109,7 +109,7 @@ describe('B3Propagator', () => {
109109
defaultGetter
110110
);
111111

112-
const extractedSpanContext = getExtractedSpanContext(context);
112+
const extractedSpanContext = getActiveSpan(context)?.context();
113113
assert.deepStrictEqual(extractedSpanContext, {
114114
spanId: 'e457b5a2e4d86bd1',
115115
traceId: '80f198ee56343ba864fe8b2a57d3eff7',
@@ -125,7 +125,7 @@ describe('B3Propagator', () => {
125125
defaultGetter
126126
);
127127

128-
const extractedSpanContext = getExtractedSpanContext(context);
128+
const extractedSpanContext = getActiveSpan(context)?.context();
129129
assert.deepStrictEqual(extractedSpanContext, {
130130
spanId: '6e0c63257de34c92',
131131
traceId: 'd4cda95b652f4a1592b449d5929fda1b',
@@ -141,7 +141,7 @@ describe('B3Propagator', () => {
141141
defaultGetter
142142
);
143143

144-
const extractedSpanContext = getExtractedSpanContext(context);
144+
const extractedSpanContext = getActiveSpan(context)?.context();
145145
assert.deepStrictEqual(extractedSpanContext, {
146146
spanId: 'e457b5a2e4d86bd1',
147147
traceId: '80f198ee56343ba864fe8b2a57d3eff7',

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

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ import {
2222
TraceFlags,
2323
INVALID_SPANID,
2424
INVALID_TRACEID,
25-
getExtractedSpanContext,
25+
getActiveSpan,
2626
setExtractedSpanContext,
2727
} from '@opentelemetry/api';
2828
import { ROOT_CONTEXT } from '@opentelemetry/context-base';
@@ -136,7 +136,7 @@ describe('B3SinglePropagator', () => {
136136

137137
const context = propagator.extract(ROOT_CONTEXT, carrier, defaultGetter);
138138

139-
const extractedSpanContext = getExtractedSpanContext(context);
139+
const extractedSpanContext = getActiveSpan(context)?.context();
140140
assert.deepStrictEqual(extractedSpanContext, {
141141
spanId: 'e457b5a2e4d86bd1',
142142
traceId: '80f198ee56343ba864fe8b2a57d3eff7',
@@ -153,7 +153,7 @@ describe('B3SinglePropagator', () => {
153153

154154
const context = propagator.extract(ROOT_CONTEXT, carrier, defaultGetter);
155155

156-
const extractedSpanContext = getExtractedSpanContext(context);
156+
const extractedSpanContext = getActiveSpan(context)?.context();
157157
assert.deepStrictEqual(extractedSpanContext, {
158158
spanId: 'e457b5a2e4d86bd1',
159159
traceId: '80f198ee56343ba864fe8b2a57d3eff7',
@@ -170,7 +170,7 @@ describe('B3SinglePropagator', () => {
170170

171171
const context = propagator.extract(ROOT_CONTEXT, carrier, defaultGetter);
172172

173-
const extractedSpanContext = getExtractedSpanContext(context);
173+
const extractedSpanContext = getActiveSpan(context)?.context();
174174
assert.deepStrictEqual(extractedSpanContext, {
175175
spanId: 'e457b5a2e4d86bd1',
176176
traceId: '80f198ee56343ba864fe8b2a57d3eff7',
@@ -186,7 +186,7 @@ describe('B3SinglePropagator', () => {
186186

187187
const context = propagator.extract(ROOT_CONTEXT, carrier, defaultGetter);
188188

189-
const extractedSpanContext = getExtractedSpanContext(context);
189+
const extractedSpanContext = getActiveSpan(context)?.context();
190190
assert.deepStrictEqual(extractedSpanContext, {
191191
spanId: 'e457b5a2e4d86bd1',
192192
traceId: '00000000000000004aaba1a52cf8ee09',
@@ -203,7 +203,7 @@ describe('B3SinglePropagator', () => {
203203

204204
const context = propagator.extract(ROOT_CONTEXT, carrier, defaultGetter);
205205

206-
const extractedSpanContext = getExtractedSpanContext(context);
206+
const extractedSpanContext = getActiveSpan(context)?.context();
207207
assert.deepStrictEqual(extractedSpanContext, {
208208
spanId: 'e457b5a2e4d86bd1',
209209
traceId: '80f198ee56343ba864fe8b2a57d3eff7',
@@ -220,7 +220,7 @@ describe('B3SinglePropagator', () => {
220220

221221
const context = propagator.extract(ROOT_CONTEXT, carrier, defaultGetter);
222222

223-
const extractedSpanContext = getExtractedSpanContext(context);
223+
const extractedSpanContext = getActiveSpan(context)?.context();
224224
assert.deepStrictEqual(undefined, extractedSpanContext);
225225
});
226226

@@ -231,7 +231,7 @@ describe('B3SinglePropagator', () => {
231231

232232
const context = propagator.extract(ROOT_CONTEXT, carrier, defaultGetter);
233233

234-
const extractedSpanContext = getExtractedSpanContext(context);
234+
const extractedSpanContext = getActiveSpan(context)?.context();
235235
assert.deepStrictEqual(undefined, extractedSpanContext);
236236
});
237237

@@ -242,7 +242,7 @@ describe('B3SinglePropagator', () => {
242242

243243
const context = propagator.extract(ROOT_CONTEXT, carrier, defaultGetter);
244244

245-
const extractedSpanContext = getExtractedSpanContext(context);
245+
const extractedSpanContext = getActiveSpan(context)?.context();
246246
assert.deepStrictEqual(undefined, extractedSpanContext);
247247
});
248248

@@ -253,7 +253,7 @@ describe('B3SinglePropagator', () => {
253253

254254
const context = propagator.extract(ROOT_CONTEXT, carrier, defaultGetter);
255255

256-
const extractedSpanContext = getExtractedSpanContext(context);
256+
const extractedSpanContext = getActiveSpan(context)?.context();
257257
assert.deepStrictEqual(undefined, extractedSpanContext);
258258
});
259259
});

0 commit comments

Comments
 (0)