Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(propagator-b3): update extract to check for array #2285

Merged
merged 7 commits into from
Jun 30, 2021
Merged
5 changes: 4 additions & 1 deletion packages/opentelemetry-propagator-b3/src/B3Propagator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,10 @@ export class B3Propagator implements TextMapPropagator {
}

extract(context: Context, carrier: unknown, getter: TextMapGetter): Context {
if (getter.get(carrier, B3_CONTEXT_HEADER)) {
const header = getter.get(carrier, B3_CONTEXT_HEADER);
const b3Context = Array.isArray(header) ? header[0] : header;

if (b3Context) {
return this._b3SinglePropagator.extract(context, carrier, getter);
} else {
return this._b3MultiPropagator.extract(context, carrier, getter);
Expand Down
25 changes: 25 additions & 0 deletions packages/opentelemetry-propagator-b3/test/B3Propagator.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,31 @@ describe('B3Propagator', () => {
});
});

it('extracts multi header b3 using array getter', () => {
const context = propagator.extract(
ROOT_CONTEXT,
b3MultiCarrier,
{
get(carrier, key) {
if (carrier == null || carrier[key] === undefined) {
return [];
}
return [carrier[key]];
},

keys: defaultTextMapGetter.keys
}
);

const extractedSpanContext = trace.getSpanContext(context);
assert.deepStrictEqual(extractedSpanContext, {
spanId: '6e0c63257de34c92',
traceId: 'd4cda95b652f4a1592b449d5929fda1b',
isRemote: true,
traceFlags: TraceFlags.SAMPLED,
});
});

it('extracts single header over multi', () => {
const context = propagator.extract(
ROOT_CONTEXT,
Expand Down