Skip to content

Commit 108a847

Browse files
authored
add baggage to span tags (#5767)
* add baggage to span tags * update config option naming * update test
1 parent fe11a42 commit 108a847

File tree

3 files changed

+77
-1
lines changed

3 files changed

+77
-1
lines changed

packages/dd-trace/src/config.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -477,6 +477,7 @@ class Config {
477477
this._setValue(defaults, 'appsec.wafTimeout', 5e3) // µs
478478
this._setValue(defaults, 'baggageMaxBytes', 8192)
479479
this._setValue(defaults, 'baggageMaxItems', 64)
480+
this._setValue(defaults, 'baggageTagKeys', 'user.id,session.id,account.id')
480481
this._setValue(defaults, 'ciVisibilityTestSessionName', '')
481482
this._setValue(defaults, 'clientIpEnabled', false)
482483
this._setValue(defaults, 'clientIpHeader', null)
@@ -731,6 +732,7 @@ class Config {
731732
DD_TRACE_AWS_ADD_SPAN_POINTERS,
732733
DD_TRACE_BAGGAGE_MAX_BYTES,
733734
DD_TRACE_BAGGAGE_MAX_ITEMS,
735+
DD_TRACE_BAGGAGE_TAG_KEYS,
734736
DD_TRACE_CLIENT_IP_ENABLED,
735737
DD_TRACE_CLIENT_IP_HEADER,
736738
DD_TRACE_DYNAMODB_TABLE_PRIMARY_KEYS,
@@ -833,6 +835,7 @@ class Config {
833835
this._envUnprocessed['appsec.wafTimeout'] = DD_APPSEC_WAF_TIMEOUT
834836
this._setValue(env, 'baggageMaxBytes', DD_TRACE_BAGGAGE_MAX_BYTES)
835837
this._setValue(env, 'baggageMaxItems', DD_TRACE_BAGGAGE_MAX_ITEMS)
838+
this._setValue(env, 'baggageTagKeys', DD_TRACE_BAGGAGE_TAG_KEYS)
836839
this._setBoolean(env, 'clientIpEnabled', DD_TRACE_CLIENT_IP_ENABLED)
837840
this._setString(env, 'clientIpHeader', DD_TRACE_CLIENT_IP_HEADER?.toLowerCase())
838841
this._setBoolean(env, 'crashtracking.enabled', coalesce(
@@ -1063,6 +1066,7 @@ class Config {
10631066
this._setString(opts, 'clientIpHeader', options.clientIpHeader?.toLowerCase())
10641067
this._setValue(opts, 'baggageMaxBytes', options.baggageMaxBytes)
10651068
this._setValue(opts, 'baggageMaxItems', options.baggageMaxItems)
1069+
this._setValue(opts, 'baggageTagKeys', options.baggageTagKeys)
10661070
this._setBoolean(opts, 'codeOriginForSpans.enabled', options.codeOriginForSpans?.enabled)
10671071
this._setBoolean(
10681072
opts,

packages/dd-trace/src/opentracing/propagation/text_map.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -633,6 +633,9 @@ class TextMapPropagator {
633633
if (!carrier || !carrier.baggage) return
634634
if (!spanContext) removeAllBaggageItems()
635635
const baggages = carrier.baggage.split(',')
636+
const keysToSpanTag = this._config.baggageTagKeys !== '*'
637+
? new Set(this._config.baggageTagKeys.split(','))
638+
: undefined
636639
for (const keyValue of baggages) {
637640
if (!keyValue.includes('=')) {
638641
if (spanContext) spanContext._baggageItems = {}
@@ -647,8 +650,11 @@ class TextMapPropagator {
647650
}
648651
// the current code assumes precedence of ot-baggage- (legacy opentracing baggage) over baggage
649652
if (spanContext) {
650-
if (key in spanContext._baggageItems) return
653+
if (Object.hasOwn(spanContext._baggageItems, key)) continue
651654
spanContext._baggageItems[key] = value
655+
if (this._config.baggageTagKeys === '*' || keysToSpanTag.has(key)) {
656+
spanContext._trace.tags['baggage.' + key] = value
657+
}
652658
} else {
653659
setBaggageItem(key, value)
654660
}

packages/dd-trace/test/opentracing/propagation/text_map.spec.js

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -468,6 +468,72 @@ describe('TextMapPropagator', () => {
468468
expect(spanContextD._baggageItems).to.deep.equal({})
469469
})
470470

471+
it('should add baggage items to span tags', () => {
472+
// should add baggage with default keys
473+
let carrier = {
474+
'x-datadog-trace-id': '123',
475+
'x-datadog-parent-id': '456',
476+
baggage: 'user.id=capybara,session.id=987,account.id=789,nonDefaultKey=shouldBeIgnored'
477+
}
478+
const spanContextA = propagator.extract(carrier)
479+
expect(spanContextA._trace.tags).to.deep.equal({
480+
'baggage.user.id': 'capybara',
481+
'baggage.session.id': '987',
482+
'baggage.account.id': '789'
483+
})
484+
485+
// should add baggage with case sensitive keys
486+
carrier = {
487+
'x-datadog-trace-id': '123',
488+
'x-datadog-parent-id': '456',
489+
baggage: 'user.id=capybara,sesSion.id=987,account.id=789'
490+
}
491+
const spanContextB = propagator.extract(carrier)
492+
expect(spanContextB._trace.tags).to.deep.equal({
493+
'baggage.user.id': 'capybara',
494+
'baggage.account.id': '789'
495+
})
496+
497+
// should not add baggage when key list is empty
498+
config = new Config({
499+
baggageTagKeys: ''
500+
})
501+
propagator = new TextMapPropagator(config)
502+
const spanContextC = propagator.extract(carrier)
503+
expect(spanContextC._trace.tags).to.deep.equal({})
504+
505+
// should not add baggage when key list is empty
506+
config = new Config({
507+
baggageTagKeys: 'customKey'
508+
})
509+
propagator = new TextMapPropagator(config)
510+
carrier = {
511+
'x-datadog-trace-id': '123',
512+
'x-datadog-parent-id': '456',
513+
baggage: 'customKey=beluga,randomKey=shouldBeIgnored'
514+
}
515+
const spanContextD = propagator.extract(carrier)
516+
expect(spanContextD._trace.tags).to.deep.equal({
517+
'baggage.customKey': 'beluga'
518+
})
519+
520+
// should add all baggage to span tags
521+
config = new Config({
522+
baggageTagKeys: '*'
523+
})
524+
propagator = new TextMapPropagator(config)
525+
carrier = {
526+
'x-datadog-trace-id': '123',
527+
'x-datadog-parent-id': '456',
528+
baggage: 'customKey=beluga,randomKey=nothingIsIgnored'
529+
}
530+
const spanContextE = propagator.extract(carrier)
531+
expect(spanContextE._trace.tags).to.deep.equal({
532+
'baggage.customKey': 'beluga',
533+
'baggage.randomKey': 'nothingIsIgnored'
534+
})
535+
})
536+
471537
it('should discard malformed tids', () => {
472538
// tid with malformed characters
473539
let carrier = {

0 commit comments

Comments
 (0)