-
Notifications
You must be signed in to change notification settings - Fork 317
/
Copy pathtext_map.js
756 lines (622 loc) · 23.3 KB
/
text_map.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
'use strict'
const pick = require('../../../../datadog-core/src/utils/src/pick')
const id = require('../../id')
const DatadogSpanContext = require('../span_context')
const OtelSpanContext = require('../../opentelemetry/span_context')
const log = require('../../log')
const TraceState = require('./tracestate')
const tags = require('../../../../../ext/tags')
const { channel } = require('dc-polyfill')
const { AUTO_KEEP, AUTO_REJECT, USER_KEEP } = require('../../../../../ext/priority')
const injectCh = channel('dd-trace:span:inject')
const extractCh = channel('dd-trace:span:extract')
const traceKey = 'x-datadog-trace-id'
const spanKey = 'x-datadog-parent-id'
const originKey = 'x-datadog-origin'
const samplingKey = 'x-datadog-sampling-priority'
const tagsKey = 'x-datadog-tags'
const baggagePrefix = 'ot-baggage-'
const b3TraceKey = 'x-b3-traceid'
const b3TraceExpr = /^([0-9a-f]{16}){1,2}$/i
const b3SpanKey = 'x-b3-spanid'
const b3SpanExpr = /^[0-9a-f]{16}$/i
const b3ParentKey = 'x-b3-parentspanid'
const b3SampledKey = 'x-b3-sampled'
const b3FlagsKey = 'x-b3-flags'
const b3HeaderKey = 'b3'
const sqsdHeaderHey = 'x-aws-sqsd-attr-_datadog'
const b3HeaderExpr = /^(([0-9a-f]{16}){1,2}-[0-9a-f]{16}(-[01d](-[0-9a-f]{16})?)?|[01d])$/i
const baggageExpr = new RegExp(`^${baggagePrefix}(.+)$`)
const tagKeyExpr = /^_dd\.p\.[\x21-\x2b\x2d-\x7e]+$/ // ASCII minus spaces and commas
const tagValueExpr = /^[\x20-\x2b\x2d-\x7e]*$/ // ASCII minus commas
const ddKeys = [traceKey, spanKey, samplingKey, originKey]
const b3Keys = [b3TraceKey, b3SpanKey, b3ParentKey, b3SampledKey, b3FlagsKey, b3HeaderKey]
const logKeys = ddKeys.concat(b3Keys)
const traceparentExpr = /^([a-f0-9]{2})-([a-f0-9]{32})-([a-f0-9]{16})-([a-f0-9]{2})(-.*)?$/i
const traceparentKey = 'traceparent'
// Origin value in tracestate replaces '~', ',' and ';' with '_"
const tracestateOriginFilter = /[^\x20-\x2b\x2d-\x3a\x3c-\x7d]/g
// Tag keys in tracestate replace ' ', ',' and '=' with '_'
const tracestateTagKeyFilter = /[^\x21-\x2b\x2d-\x3c\x3e-\x7e]/g
// Tag values in tracestate replace ',', '~' and ';' with '_'
const tracestateTagValueFilter = /[^\x20-\x2b\x2d-\x3a\x3c-\x7d]/g
const invalidSegment = /^0+$/
const zeroTraceId = '0000000000000000'
class TextMapPropagator {
constructor (config) {
this._config = config
}
inject (spanContext, carrier) {
if (!spanContext || !carrier) return
this._injectBaggageItems(spanContext, carrier)
this._injectDatadog(spanContext, carrier)
this._injectB3MultipleHeaders(spanContext, carrier)
this._injectB3SingleHeader(spanContext, carrier)
this._injectTraceparent(spanContext, carrier)
if (injectCh.hasSubscribers) {
injectCh.publish({ spanContext, carrier })
}
log.debug(() => `Inject into carrier: ${JSON.stringify(pick(carrier, logKeys))}.`)
}
extract (carrier) {
const spanContext = this._extractSpanContext(carrier)
if (!spanContext) return spanContext
if (extractCh.hasSubscribers) {
extractCh.publish({ spanContext, carrier })
}
log.debug(() => `Extract from carrier: ${JSON.stringify(pick(carrier, logKeys))}.`)
return spanContext
}
_injectDatadog (spanContext, carrier) {
if (!this._hasPropagationStyle('inject', 'datadog')) return
carrier[traceKey] = spanContext.toTraceId()
carrier[spanKey] = spanContext.toSpanId()
this._injectOrigin(spanContext, carrier)
this._injectSamplingPriority(spanContext, carrier)
this._injectTags(spanContext, carrier)
}
_injectOrigin (spanContext, carrier) {
const origin = spanContext._trace.origin
if (origin) {
carrier[originKey] = origin
}
}
_injectSamplingPriority (spanContext, carrier) {
const priority = spanContext._sampling.priority
if (Number.isInteger(priority)) {
carrier[samplingKey] = priority.toString()
}
}
_encodeOtelBaggageKey (key) {
let encoded = encodeURIComponent(key)
encoded = encoded.replaceAll('(', '%28')
encoded = encoded.replaceAll(')', '%29')
return encoded
}
_injectBaggageItems (spanContext, carrier) {
if (this._config.legacyBaggageEnabled) {
spanContext._baggageItems && Object.keys(spanContext._baggageItems).forEach(key => {
carrier[baggagePrefix + key] = String(spanContext._baggageItems[key])
})
}
if (this._hasPropagationStyle('inject', 'baggage')) {
let baggage = ''
let itemCounter = 0
let byteCounter = 0
for (const [key, value] of Object.entries(spanContext._baggageItems)) {
const item = `${this._encodeOtelBaggageKey(String(key).trim())}=${encodeURIComponent(String(value).trim())},`
itemCounter += 1
byteCounter += item.length
if (itemCounter > this._config.baggageMaxItems || byteCounter > this._config.baggageMaxBytes) break
baggage += item
}
baggage = baggage.slice(0, baggage.length - 1)
if (baggage) carrier.baggage = baggage
}
}
_injectTags (spanContext, carrier) {
const trace = spanContext._trace
if (this._config.tagsHeaderMaxLength === 0) {
log.debug('Trace tag propagation is disabled, skipping injection.')
return
}
const tags = []
for (const key in trace.tags) {
if (!trace.tags[key] || !key.startsWith('_dd.p.')) continue
if (!this._validateTagKey(key) || !this._validateTagValue(trace.tags[key])) {
log.error('Trace tags from span are invalid, skipping injection.')
return
}
tags.push(`${key}=${trace.tags[key]}`)
}
const header = tags.join(',')
if (header.length > this._config.tagsHeaderMaxLength) {
log.error('Trace tags from span are too large, skipping injection.')
} else if (header) {
carrier[tagsKey] = header
}
}
_injectB3MultipleHeaders (spanContext, carrier) {
const hasB3 = this._hasPropagationStyle('inject', 'b3')
const hasB3multi = this._hasPropagationStyle('inject', 'b3multi')
if (!(hasB3 || hasB3multi)) return
carrier[b3TraceKey] = this._getB3TraceId(spanContext)
carrier[b3SpanKey] = spanContext._spanId.toString(16)
carrier[b3SampledKey] = spanContext._sampling.priority >= AUTO_KEEP ? '1' : '0'
if (spanContext._sampling.priority > AUTO_KEEP) {
carrier[b3FlagsKey] = '1'
}
if (spanContext._parentId) {
carrier[b3ParentKey] = spanContext._parentId.toString(16)
}
}
_injectB3SingleHeader (spanContext, carrier) {
const hasB3SingleHeader = this._hasPropagationStyle('inject', 'b3 single header')
if (!hasB3SingleHeader) return null
const traceId = this._getB3TraceId(spanContext)
const spanId = spanContext._spanId.toString(16)
const sampled = spanContext._sampling.priority >= AUTO_KEEP ? '1' : '0'
carrier[b3HeaderKey] = `${traceId}-${spanId}-${sampled}`
if (spanContext._parentId) {
carrier[b3HeaderKey] += '-' + spanContext._parentId.toString(16)
}
}
_injectTraceparent (spanContext, carrier) {
if (!this._hasPropagationStyle('inject', 'tracecontext')) return
const {
_sampling: { priority, mechanism },
_tracestate: ts = new TraceState(),
_trace: { origin, tags }
} = spanContext
carrier[traceparentKey] = spanContext.toTraceparent()
ts.forVendor('dd', state => {
if (!spanContext._isRemote) {
// SpanContext was created by a ddtrace span.
// Last datadog span id should be set to the current span.
state.set('p', spanContext._spanId)
} else if (spanContext._trace.tags[tags.DD_PARENT_ID]) {
// Propagate the last Datadog span id set on the remote span.
state.set('p', spanContext._trace.tags[tags.DD_PARENT_ID])
}
state.set('s', priority)
if (mechanism) {
state.set('t.dm', `-${mechanism}`)
}
if (typeof origin === 'string') {
const originValue = origin
.replace(tracestateOriginFilter, '_')
.replace(/[\x3d]/g, '~')
state.set('o', originValue)
}
for (const key in tags) {
if (!tags[key] || !key.startsWith('_dd.p.')) continue
const tagKey = 't.' + key.slice(6)
.replace(tracestateTagKeyFilter, '_')
const tagValue = tags[key]
.toString()
.replace(tracestateTagValueFilter, '_')
.replace(/[\x3d]/g, '~')
state.set(tagKey, tagValue)
}
})
carrier.tracestate = ts.toString()
}
_hasPropagationStyle (mode, name) {
return this._config.tracePropagationStyle[mode].includes(name)
}
_hasTraceIdConflict (w3cSpanContext, firstSpanContext) {
return w3cSpanContext !== null &&
firstSpanContext.toTraceId(true) === w3cSpanContext.toTraceId(true) &&
firstSpanContext.toSpanId() !== w3cSpanContext.toSpanId()
}
_hasParentIdInTags (spanContext) {
return tags.DD_PARENT_ID in spanContext._trace.tags
}
_updateParentIdFromDdHeaders (carrier, firstSpanContext) {
const ddCtx = this._extractDatadogContext(carrier)
if (ddCtx !== null) {
firstSpanContext._trace.tags[tags.DD_PARENT_ID] = ddCtx._spanId.toString().padStart(16, '0')
}
}
_resolveTraceContextConflicts (w3cSpanContext, firstSpanContext, carrier) {
if (!this._hasTraceIdConflict(w3cSpanContext, firstSpanContext)) {
return firstSpanContext
}
if (this._hasParentIdInTags(w3cSpanContext)) {
// tracecontext headers contain a p value, ensure this value is sent to backend
firstSpanContext._trace.tags[tags.DD_PARENT_ID] = w3cSpanContext._trace.tags[tags.DD_PARENT_ID]
} else {
// if p value is not present in tracestate, use the parent id from the datadog headers
this._updateParentIdFromDdHeaders(carrier, firstSpanContext)
}
// the span_id in tracecontext takes precedence over the first extracted propagation style
firstSpanContext._spanId = w3cSpanContext._spanId
return firstSpanContext
}
_extractSpanContext (carrier) {
let context = null
for (const extractor of this._config.tracePropagationStyle.extract) {
let extractedContext = null
switch (extractor) {
case 'datadog':
extractedContext = this._extractDatadogContext(carrier)
break
case 'tracecontext':
extractedContext = this._extractTraceparentContext(carrier)
break
case 'b3 single header': // TODO: delete in major after singular "b3"
extractedContext = this._extractB3SingleContext(carrier)
break
case 'b3':
if (this._config.tracePropagationStyle.otelPropagators) {
// TODO: should match "b3 single header" in next major
extractedContext = this._extractB3SingleContext(carrier)
} else {
extractedContext = this._extractB3MultiContext(carrier)
}
break
case 'b3multi':
extractedContext = this._extractB3MultiContext(carrier)
break
default:
if (extractor !== 'baggage') log.warn(`Unknown propagation style: ${extractor}`)
}
if (extractedContext === null) { // If the current extractor was invalid, continue to the next extractor
continue
}
if (context === null) {
context = extractedContext
if (this._config.tracePropagationExtractFirst) {
this._extractBaggageItems(carrier, context)
return context
}
} else {
// If extractor is tracecontext, add tracecontext specific information to the context
if (extractor === 'tracecontext') {
context = this._resolveTraceContextConflicts(
this._extractTraceparentContext(carrier), context, carrier)
}
if (extractedContext._traceId && extractedContext._spanId &&
extractedContext.toTraceId(true) !== context.toTraceId(true)) {
const link = {
context: extractedContext,
attributes: { reason: 'terminated_context', context_headers: extractor }
}
context._links.push(link)
}
}
}
this._extractBaggageItems(carrier, context)
return context || this._extractSqsdContext(carrier)
}
_extractDatadogContext (carrier) {
const spanContext = this._extractGenericContext(carrier, traceKey, spanKey, 10)
if (!spanContext) return spanContext
this._extractOrigin(carrier, spanContext)
this._extractLegacyBaggageItems(carrier, spanContext)
this._extractSamplingPriority(carrier, spanContext)
this._extractTags(carrier, spanContext)
if (this._config.tracePropagationExtractFirst) return spanContext
const tc = this._extractTraceparentContext(carrier)
if (tc && spanContext._traceId.equals(tc._traceId)) {
spanContext._traceparent = tc._traceparent
spanContext._tracestate = tc._tracestate
}
return spanContext
}
_extractB3MultiContext (carrier) {
const b3 = this._extractB3MultipleHeaders(carrier)
if (!b3) return null
return this._extractB3Context(b3)
}
_extractB3SingleContext (carrier) {
if (!b3HeaderExpr.test(carrier[b3HeaderKey])) return null
const b3 = this._extractB3SingleHeader(carrier)
if (!b3) return null
return this._extractB3Context(b3)
}
_extractB3Context (b3) {
const debug = b3[b3FlagsKey] === '1'
const priority = this._getPriority(b3[b3SampledKey], debug)
const spanContext = this._extractGenericContext(b3, b3TraceKey, b3SpanKey, 16)
if (priority !== undefined) {
if (!spanContext) {
// B3 can force a sampling decision without providing IDs
return new DatadogSpanContext({
traceId: id(),
spanId: null,
sampling: { priority },
isRemote: true
})
}
spanContext._sampling.priority = priority
}
this._extract128BitTraceId(b3[b3TraceKey], spanContext)
return spanContext
}
_extractSqsdContext (carrier) {
const headerValue = carrier[sqsdHeaderHey]
if (!headerValue) {
return null
}
let parsed
try {
parsed = JSON.parse(headerValue)
} catch (e) {
return null
}
return this._extractDatadogContext(parsed)
}
_extractTraceparentContext (carrier) {
const headerValue = carrier[traceparentKey]
if (!headerValue) {
return null
}
const matches = headerValue.trim().match(traceparentExpr)
if (matches?.length) {
const [version, traceId, spanId, flags, tail] = matches.slice(1)
const traceparent = { version }
const tracestate = TraceState.fromString(carrier.tracestate)
if (invalidSegment.test(traceId)) return null
if (invalidSegment.test(spanId)) return null
// Version ff is considered invalid
if (version === 'ff') return null
// Version 00 should have no tail, but future versions may
if (tail && version === '00') return null
const spanContext = new DatadogSpanContext({
traceId: id(traceId, 16),
spanId: id(spanId, 16),
isRemote: true,
sampling: { priority: parseInt(flags, 10) & 1 ? 1 : 0 },
traceparent,
tracestate
})
this._extract128BitTraceId(traceId, spanContext)
tracestate.forVendor('dd', state => {
for (const [key, value] of state.entries()) {
switch (key) {
case 'p': {
spanContext._trace.tags[tags.DD_PARENT_ID] = value
break
}
case 's': {
const priority = parseInt(value, 10)
if (!Number.isInteger(priority)) continue
if (
(spanContext._sampling.priority === 1 && priority > 0) ||
(spanContext._sampling.priority === 0 && priority < 0)
) {
spanContext._sampling.priority = priority
}
break
}
case 'o':
spanContext._trace.origin = value
break
case 't.dm': {
const mechanism = Math.abs(parseInt(value, 10))
if (Number.isInteger(mechanism)) {
spanContext._sampling.mechanism = mechanism
spanContext._trace.tags['_dd.p.dm'] = `-${mechanism}`
}
break
}
default:
if (!key.startsWith('t.')) continue
spanContext._trace.tags[`_dd.p.${key.slice(2)}`] = value
.replace(/[\x7e]/gm, '=')
}
}
})
this._extractLegacyBaggageItems(carrier, spanContext)
return spanContext
}
return null
}
_extractGenericContext (carrier, traceKey, spanKey, radix) {
if (carrier && carrier[traceKey] && carrier[spanKey]) {
if (invalidSegment.test(carrier[traceKey])) return null
return new DatadogSpanContext({
traceId: id(carrier[traceKey], radix),
spanId: id(carrier[spanKey], radix),
isRemote: true
})
}
return null
}
_extractB3MultipleHeaders (carrier) {
let empty = true
const b3 = {}
if (b3TraceExpr.test(carrier[b3TraceKey]) && b3SpanExpr.test(carrier[b3SpanKey])) {
b3[b3TraceKey] = carrier[b3TraceKey]
b3[b3SpanKey] = carrier[b3SpanKey]
empty = false
}
if (carrier[b3SampledKey]) {
b3[b3SampledKey] = carrier[b3SampledKey]
empty = false
}
if (carrier[b3FlagsKey]) {
b3[b3FlagsKey] = carrier[b3FlagsKey]
empty = false
}
return empty ? null : b3
}
_extractB3SingleHeader (carrier) {
const header = carrier[b3HeaderKey]
if (!header) return null
const parts = header.split('-')
if (parts[0] === 'd') {
return {
[b3SampledKey]: '1',
[b3FlagsKey]: '1'
}
} else if (parts.length === 1) {
return {
[b3SampledKey]: parts[0]
}
} else {
const b3 = {
[b3TraceKey]: parts[0],
[b3SpanKey]: parts[1]
}
if (parts[2]) {
b3[b3SampledKey] = parts[2] !== '0' ? '1' : '0'
if (parts[2] === 'd') {
b3[b3FlagsKey] = '1'
}
}
return b3
}
}
_extractOrigin (carrier, spanContext) {
const origin = carrier[originKey]
if (typeof carrier[originKey] === 'string') {
spanContext._trace.origin = origin
}
}
_decodeOtelBaggageKey (key) {
let decoded = decodeURIComponent(key)
decoded = decoded.replaceAll('%28', '(')
decoded = decoded.replaceAll('%29', ')')
return decoded
}
_extractLegacyBaggageItems (carrier, spanContext) {
if (this._config.legacyBaggageEnabled) {
Object.keys(carrier).forEach(key => {
const match = key.match(baggageExpr)
if (match) {
spanContext._baggageItems[match[1]] = carrier[key]
}
})
}
}
_extractBaggageItems (carrier, spanContext) {
if (!this._hasPropagationStyle('extract', 'baggage')) return
if (!carrier || !carrier.baggage) return
if (!spanContext) return
const baggages = carrier.baggage.split(',')
for (const keyValue of baggages) {
if (!keyValue.includes('=')) {
spanContext._baggageItems = {}
return
}
let [key, value] = keyValue.split('=')
key = this._decodeOtelBaggageKey(key.trim())
value = decodeURIComponent(value.trim())
if (!key || !value) {
spanContext._baggageItems = {}
return
}
// the current code assumes precedence of ot-baggage- (legacy opentracing baggage) over baggage
if (key in spanContext._baggageItems) return
spanContext._baggageItems[key] = value
}
}
_extractSamplingPriority (carrier, spanContext) {
const priority = parseInt(carrier[samplingKey], 10)
if (Number.isInteger(priority)) {
spanContext._sampling.priority = priority
}
}
_extractTags (carrier, spanContext) {
if (!carrier[tagsKey]) return
const trace = spanContext._trace
if (this._config.tagsHeaderMaxLength === 0) {
log.debug('Trace tag propagation is disabled, skipping extraction.')
} else if (carrier[tagsKey].length > this._config.tagsHeaderMaxLength) {
log.error('Trace tags from carrier are too large, skipping extraction.')
} else {
const pairs = carrier[tagsKey].split(',')
const tags = {}
for (const pair of pairs) {
const [key, ...rest] = pair.split('=')
const value = rest.join('=')
if (!this._validateTagKey(key) || !this._validateTagValue(value)) {
log.error('Trace tags from carrier are invalid, skipping extraction.')
return
}
tags[key] = value
}
Object.assign(trace.tags, tags)
}
}
_extract128BitTraceId (traceId, spanContext) {
if (!spanContext) return
const buffer = spanContext._traceId.toBuffer()
if (buffer.length !== 16) return
const tid = traceId.substring(0, 16)
if (tid === zeroTraceId) return
spanContext._trace.tags['_dd.p.tid'] = tid
}
_validateTagKey (key) {
return tagKeyExpr.test(key)
}
_validateTagValue (value) {
return tagValueExpr.test(value)
}
_getPriority (sampled, debug) {
if (debug) {
return USER_KEEP
} else if (sampled === '1') {
return AUTO_KEEP
} else if (sampled === '0') {
return AUTO_REJECT
}
}
_getB3TraceId (spanContext) {
if (spanContext._traceId.toBuffer().length <= 8 && spanContext._trace.tags['_dd.p.tid']) {
return spanContext._trace.tags['_dd.p.tid'] + spanContext._traceId.toString(16)
}
return spanContext._traceId.toString(16)
}
static _convertOtelContextToDatadog (traceId, spanId, traceFlag, ts, meta = {}) {
const origin = null
let samplingPriority = traceFlag
ts = ts?.traceparent || null
if (ts) {
// Use TraceState.fromString to parse the tracestate header
const traceState = TraceState.fromString(ts)
let ddTraceStateData = null
// Extract Datadog specific trace state data
traceState.forVendor('dd', (state) => {
ddTraceStateData = state
return state // You might need to adjust this part based on actual logic needed
})
if (ddTraceStateData) {
// Assuming ddTraceStateData is now a Map or similar structure containing Datadog trace state data
// Extract values as needed, similar to the original logic
const samplingPriorityTs = ddTraceStateData.get('s')
const origin = ddTraceStateData.get('o')
// Convert Map to object for meta
const otherPropagatedTags = Object.fromEntries(ddTraceStateData.entries())
// Update meta and samplingPriority based on extracted values
Object.assign(meta, otherPropagatedTags)
samplingPriority = TextMapPropagator._getSamplingPriority(traceFlag, parseInt(samplingPriorityTs, 10), origin)
} else {
log.debug(`no dd list member in tracestate from incoming request: ${ts}`)
}
}
const spanContext = new OtelSpanContext({
traceId: id(traceId, 16), spanId: id(), tags: meta, parentId: id(spanId, 16)
})
spanContext._sampling = { priority: samplingPriority }
spanContext._trace = { origin }
return spanContext
}
static _getSamplingPriority (traceparentSampled, tracestateSamplingPriority, origin = null) {
const fromRumWithoutPriority = !tracestateSamplingPriority && origin === 'rum'
let samplingPriority
if (!fromRumWithoutPriority && traceparentSampled === 0 &&
(!tracestateSamplingPriority || tracestateSamplingPriority >= 0)) {
samplingPriority = 0
} else if (!fromRumWithoutPriority && traceparentSampled === 1 &&
(!tracestateSamplingPriority || tracestateSamplingPriority < 0)) {
samplingPriority = 1
} else {
samplingPriority = tracestateSamplingPriority
}
return samplingPriority
}
}
module.exports = TextMapPropagator