Skip to content

Commit acb0345

Browse files
fix(opentelemtry-core): fix header extraction (#2832)
Co-authored-by: legendecas <[email protected]>
1 parent 038bbdb commit acb0345

File tree

2 files changed

+40
-12
lines changed

2 files changed

+40
-12
lines changed

packages/opentelemetry-core/src/baggage/propagation/W3CBaggagePropagator.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -58,13 +58,14 @@ export class W3CBaggagePropagator implements TextMapPropagator {
5858
}
5959

6060
extract(context: Context, carrier: unknown, getter: TextMapGetter): Context {
61-
const headerValue: string = getter.get(carrier, BAGGAGE_HEADER) as string;
62-
if (!headerValue) return context;
61+
const headerValue = getter.get(carrier, BAGGAGE_HEADER);
62+
const baggageString = Array.isArray(headerValue) ? headerValue.join(BAGGAGE_ITEMS_SEPARATOR) : headerValue;
63+
if (!baggageString) return context;
6364
const baggage: Record<string, BaggageEntry> = {};
64-
if (headerValue.length === 0) {
65+
if (baggageString.length === 0) {
6566
return context;
6667
}
67-
const pairs = headerValue.split(BAGGAGE_ITEMS_SEPARATOR);
68+
const pairs = baggageString.split(BAGGAGE_ITEMS_SEPARATOR);
6869
pairs.forEach(entry => {
6970
const keyPair = parsePairKeyValue(entry);
7071
if (keyPair) {

packages/opentelemetry-core/test/baggage/W3CBaggagePropagator.test.ts

Lines changed: 35 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -175,9 +175,42 @@ describe('W3CBaggagePropagator', () => {
175175
});
176176

177177
describe('.extract()', () => {
178+
const baggageValue = 'key1=d4cda95b,key3=c88815a7, keyn = valn, keym =valm';
179+
const expected = propagation.createBaggage({
180+
key1: { value: 'd4cda95b' },
181+
key3: { value: 'c88815a7' },
182+
keyn: { value: 'valn' },
183+
keym: { value: 'valm' },
184+
});
185+
178186
it('should extract context of a sampled span from carrier', () => {
179-
carrier[BAGGAGE_HEADER] =
180-
'key1=d4cda95b,key3=c88815a7, keyn = valn, keym =valm';
187+
carrier[BAGGAGE_HEADER] = baggageValue;
188+
const extractedBaggage = propagation.getBaggage(
189+
httpBaggagePropagator.extract(
190+
ROOT_CONTEXT,
191+
carrier,
192+
defaultTextMapGetter
193+
)
194+
);
195+
196+
assert.deepStrictEqual(extractedBaggage, expected);
197+
});
198+
199+
it('should extract context of a sampled span when the headerValue comes as array', () => {
200+
carrier[BAGGAGE_HEADER] = [baggageValue];
201+
const extractedBaggage = propagation.getBaggage(
202+
httpBaggagePropagator.extract(
203+
ROOT_CONTEXT,
204+
carrier,
205+
defaultTextMapGetter
206+
)
207+
);
208+
209+
assert.deepStrictEqual(extractedBaggage, expected);
210+
});
211+
212+
it('should extract context of a sampled span when the headerValue comes as array with multiple items', () => {
213+
carrier[BAGGAGE_HEADER] = ['key1=d4cda95b,key3=c88815a7, keyn = valn', 'keym =valm'];
181214
const extractedBaggage = propagation.getBaggage(
182215
httpBaggagePropagator.extract(
183216
ROOT_CONTEXT,
@@ -186,12 +219,6 @@ describe('W3CBaggagePropagator', () => {
186219
)
187220
);
188221

189-
const expected = propagation.createBaggage({
190-
key1: { value: 'd4cda95b' },
191-
key3: { value: 'c88815a7' },
192-
keyn: { value: 'valn' },
193-
keym: { value: 'valm' },
194-
});
195222
assert.deepStrictEqual(extractedBaggage, expected);
196223
});
197224
});

0 commit comments

Comments
 (0)