Skip to content

Commit

Permalink
fix(opentelemtry-core): fix header extraction
Browse files Browse the repository at this point in the history
  • Loading branch information
doronkopit5 committed Mar 14, 2022
1 parent 4ca2d89 commit b5a6a1c
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,8 @@ export class W3CBaggagePropagator implements TextMapPropagator {
}

extract(context: Context, carrier: unknown, getter: TextMapGetter): Context {
const headerValue: string = getter.get(carrier, BAGGAGE_HEADER) as string;
const headerBaggage = getter.get(carrier, BAGGAGE_HEADER);
const headerValue: string = (headerBaggage instanceof Array)? headerBaggage[0]: headerBaggage as string;
if (!headerValue) return context;
const baggage: Record<string, BaggageEntry> = {};
if (headerValue.length === 0) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -175,9 +175,29 @@ describe('W3CBaggagePropagator', () => {
});

describe('.extract()', () => {
const baggageValue = 'key1=d4cda95b,key3=c88815a7, keyn = valn, keym =valm';
const expected = propagation.createBaggage({
key1: { value: 'd4cda95b' },
key3: { value: 'c88815a7' },
keyn: { value: 'valn' },
keym: { value: 'valm' },
});

it('should extract context of a sampled span from carrier', () => {
carrier[BAGGAGE_HEADER] =
'key1=d4cda95b,key3=c88815a7, keyn = valn, keym =valm';
carrier[BAGGAGE_HEADER] = baggageValue;
const extractedBaggage = propagation.getBaggage(
httpBaggagePropagator.extract(
ROOT_CONTEXT,
carrier,
defaultTextMapGetter
)
);

assert.deepStrictEqual(extractedBaggage, expected);
});

it('should extract context of a sampled span when the headerValue comes as array', () => {
carrier[BAGGAGE_HEADER] = [baggageValue];
const extractedBaggage = propagation.getBaggage(
httpBaggagePropagator.extract(
ROOT_CONTEXT,
Expand All @@ -186,12 +206,6 @@ describe('W3CBaggagePropagator', () => {
)
);

const expected = propagation.createBaggage({
key1: { value: 'd4cda95b' },
key3: { value: 'c88815a7' },
keyn: { value: 'valn' },
keym: { value: 'valm' },
});
assert.deepStrictEqual(extractedBaggage, expected);
});
});
Expand Down

0 comments on commit b5a6a1c

Please sign in to comment.