Skip to content

Commit

Permalink
fix(propagator-ot-trace): read sampled flag correctly from span context
Browse files Browse the repository at this point in the history
The way the sampled flag is currently read from the span context will
break when more flags are introduced. This is mentioned explicitly in
the [specification](https://www.w3.org/TR/trace-context/#trace-flags).
  • Loading branch information
Bastian Krol committed Aug 9, 2022
1 parent 57cedc5 commit 1e8558a
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,9 @@ export class OTTracePropagator implements TextMapPropagator {
setter.set(
carrier,
OT_SAMPLED_HEADER,
spanContext.traceFlags === TraceFlags.SAMPLED ? 'true' : 'false'
(spanContext.traceFlags & TraceFlags.SAMPLED) === TraceFlags.SAMPLED
? 'true'
: 'false'
);

const baggage = propagation.getBaggage(context);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,25 @@ describe('OTTracePropagator', () => {
assert.strictEqual(carrier[OT_SAMPLED_HEADER], 'true');
});

it('correctly reads the sampled flag even if other flags are set', () => {
const spanContext: SpanContext = {
traceId: '80f198ee56343ba864fe8b2a57d3eff7',
spanId: 'e457b5a2e4d86bd1',
// 81 = 1000 0001, so this sets the sampled flag (rightmost bit) and one other flag (leftmost bit, semantics unspecified at the time of writing)
traceFlags: 81,
};

propagator.inject(
trace.setSpan(ROOT_CONTEXT, trace.wrapSpanContext(spanContext)),
carrier,
defaultTextMapSetter
);

assert.strictEqual(carrier[OT_TRACE_ID_HEADER], '64fe8b2a57d3eff7');
assert.strictEqual(carrier[OT_SPAN_ID_HEADER], 'e457b5a2e4d86bd1');
assert.strictEqual(carrier[OT_SAMPLED_HEADER], 'true');
});

it('injects context with unspecified trace flags', () => {
const spanContext: SpanContext = {
traceId: '80f198ee56343ba864fe8b2a57d3eff7',
Expand Down

0 comments on commit 1e8558a

Please sign in to comment.