Skip to content

Commit

Permalink
fix(propagator-b3): update extract to check for array (#2285)
Browse files Browse the repository at this point in the history
* fix(propagator-b3): update extract to check for array

* fix(propagator-b3): normalize header value

Co-authored-by: Daniel Dyla <[email protected]>
  • Loading branch information
jordanworner and dyladan authored Jun 30, 2021
1 parent b442048 commit 8f2209f
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 1 deletion.
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

0 comments on commit 8f2209f

Please sign in to comment.