Skip to content

Commit 662ff4c

Browse files
authored
fix: clear stale baggage items before extracting new ones from carrier (#7507)
When _extractBaggageItems extracts baggage from a carrier, it previously accumulated new items on top of existing baggage in async local storage, causing stale baggage from previous requests to leak into subsequent traces and outbound SQS messages. Moves the baggage cleanup to the very first line of _extractBaggageItems() so stale baggage is cleared even when the current carrier has no baggage header or baggage is not in the extraction propagation styles. Added test covering the no-baggage-header scenario.
1 parent b9ea9c5 commit 662ff4c

File tree

2 files changed

+32
-0
lines changed

2 files changed

+32
-0
lines changed

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -667,6 +667,7 @@ class TextMapPropagator {
667667
}
668668

669669
_extractBaggageItems (carrier, spanContext) {
670+
removeAllBaggageItems()
670671
if (!this._hasPropagationStyle('extract', 'baggage')) return
671672
if (!carrier?.baggage) return
672673
const baggages = carrier.baggage.split(',')

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

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -750,6 +750,37 @@ describe('TextMapPropagator', () => {
750750
assert.deepStrictEqual(getAllBaggageItems(), { foo: 'bar' })
751751
})
752752

753+
it('should clear pre-existing baggage items before extracting new ones', () => {
754+
removeAllBaggageItems()
755+
setBaggageItem('stale', 'leftover')
756+
setBaggageItem('foo', 'old-value')
757+
assert.deepStrictEqual(getAllBaggageItems(), { stale: 'leftover', foo: 'old-value' })
758+
759+
const carrier = {
760+
'x-datadog-trace-id': '123',
761+
'x-datadog-parent-id': '456',
762+
baggage: 'foo=new-value,fresh=added',
763+
}
764+
propagator.extract(carrier)
765+
766+
assert.deepStrictEqual(getAllBaggageItems(), { foo: 'new-value', fresh: 'added' })
767+
assert.strictEqual(getBaggageItem('stale'), undefined)
768+
})
769+
770+
it('should clear pre-existing baggage items when carrier has no baggage header', () => {
771+
removeAllBaggageItems()
772+
setBaggageItem('stale', 'leftover')
773+
assert.deepStrictEqual(getAllBaggageItems(), { stale: 'leftover' })
774+
775+
const carrier = {
776+
'x-datadog-trace-id': '123',
777+
'x-datadog-parent-id': '456',
778+
}
779+
propagator.extract(carrier)
780+
781+
assert.deepStrictEqual(getAllBaggageItems(), {})
782+
})
783+
753784
it('should convert signed IDs to unsigned', () => {
754785
textMap['x-datadog-trace-id'] = '-123'
755786
textMap['x-datadog-parent-id'] = '-456'

0 commit comments

Comments
 (0)