@@ -290,50 +290,63 @@ class TextMapPropagator {
290
290
}
291
291
292
292
_extractSpanContext ( carrier ) {
293
- let spanContext = null
293
+ let context = null
294
294
for ( const extractor of this . _config . tracePropagationStyle . extract ) {
295
- // add logic to ensure tracecontext headers takes precedence over other extracted headers
296
- if ( spanContext !== null ) {
297
- if ( this . _config . tracePropagationExtractFirst ) {
298
- return spanContext
299
- }
300
- if ( extractor !== 'tracecontext' ) {
301
- continue
302
- }
303
- spanContext = this . _resolveTraceContextConflicts (
304
- this . _extractTraceparentContext ( carrier ) , spanContext , carrier )
305
- break
306
- }
307
-
295
+ let extractedContext = null
308
296
switch ( extractor ) {
309
297
case 'datadog' :
310
- spanContext = this . _extractDatadogContext ( carrier )
298
+ extractedContext = this . _extractDatadogContext ( carrier )
311
299
break
312
300
case 'tracecontext' :
313
- spanContext = this . _extractTraceparentContext ( carrier )
301
+ extractedContext = this . _extractTraceparentContext ( carrier )
314
302
break
315
303
case 'b3' && this
316
304
. _config
317
305
. tracePropagationStyle
318
306
. otelPropagators : // TODO: should match "b3 single header" in next major
319
307
case 'b3 single header' : // TODO: delete in major after singular "b3"
320
- spanContext = this . _extractB3SingleContext ( carrier )
308
+ extractedContext = this . _extractB3SingleContext ( carrier )
321
309
break
322
310
case 'b3' :
323
311
case 'b3multi' :
324
- spanContext = this . _extractB3MultiContext ( carrier )
312
+ extractedContext = this . _extractB3MultiContext ( carrier )
325
313
break
326
314
default :
327
315
if ( extractor !== 'baggage' ) log . warn ( `Unknown propagation style: ${ extractor } ` )
328
316
}
329
317
318
+ if ( extractedContext === null ) { // If the current extractor was invalid, continue to the next extractor
319
+ continue
320
+ }
321
+
322
+ if ( context === null ) {
323
+ context = extractedContext
324
+ if ( this . _config . tracePropagationExtractFirst ) {
325
+ return context
326
+ }
327
+ } else {
328
+ // If extractor is tracecontext, add tracecontext specific information to the context
329
+ if ( extractor === 'tracecontext' ) {
330
+ context = this . _resolveTraceContextConflicts (
331
+ this . _extractTraceparentContext ( carrier ) , context , carrier )
332
+ }
333
+ if ( extractedContext . _traceId && extractedContext . _spanId &&
334
+ extractedContext . toTraceId ( true ) !== context . toTraceId ( true ) ) {
335
+ const link = {
336
+ context : extractedContext ,
337
+ attributes : { reason : 'terminated_context' , context_headers : extractor }
338
+ }
339
+ context . _links . push ( link )
340
+ }
341
+ }
342
+
330
343
if ( this . _config . tracePropagationStyle . extract . includes ( 'baggage' ) && carrier . baggage ) {
331
- spanContext = spanContext || new DatadogSpanContext ( )
332
- this . _extractBaggageItems ( carrier , spanContext )
344
+ context = context || new DatadogSpanContext ( )
345
+ this . _extractBaggageItems ( carrier , context )
333
346
}
334
347
}
335
348
336
- return spanContext || this . _extractSqsdContext ( carrier )
349
+ return context || this . _extractSqsdContext ( carrier )
337
350
}
338
351
339
352
_extractDatadogContext ( carrier ) {
0 commit comments