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: left pad short b3 trace identifiers #1021

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
fix: left pad short b3 trace identifiers
  • Loading branch information
dyladan committed May 5, 2020
commit ddf3d3bde1116512f9382082823ec9e19deab44c
Original file line number Diff line number Diff line change
Expand Up @@ -72,16 +72,27 @@ export class B3Propagator implements HttpTextPropagator {
const traceIdHeader = getter(carrier, X_B3_TRACE_ID);
const spanIdHeader = getter(carrier, X_B3_SPAN_ID);
const sampledHeader = getter(carrier, X_B3_SAMPLED);
const traceId = Array.isArray(traceIdHeader)

const traceIdHeaderValue = Array.isArray(traceIdHeader)
? traceIdHeader[0]
: traceIdHeader;
const spanId = Array.isArray(spanIdHeader) ? spanIdHeader[0] : spanIdHeader;
const spanIdHeaderValue = Array.isArray(spanIdHeader)
? spanIdHeader[0]
: spanIdHeader;

const options = Array.isArray(sampledHeader)
? sampledHeader[0]
: sampledHeader;

if (typeof traceId !== 'string' || typeof spanId !== 'string')
if (
typeof traceIdHeaderValue !== 'string' ||
typeof spanIdHeaderValue !== 'string'
) {
return context;
}

const traceId = traceIdHeaderValue.padStart(32, '0');
const spanId = spanIdHeaderValue.padStart(16, '0');

if (isValidTraceId(traceId) && isValidSpanId(spanId)) {
return setExtractedSpanContext(context, {
Expand Down
16 changes: 16 additions & 0 deletions packages/opentelemetry-core/test/context/B3Propagator.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -289,5 +289,21 @@ describe('B3Propagator', () => {
assert.ok(ctx2 === Context.ROOT_CONTEXT);
assert.ok(ctx3 === Context.ROOT_CONTEXT);
});

it('should left-pad short ids with 0', () => {
carrier[X_B3_TRACE_ID] = 'dd8448eb211c80319c';
carrier[X_B3_SPAN_ID] = '9203331';
carrier[X_B3_SAMPLED] = '1';
const extractedSpanContext = getExtractedSpanContext(
b3Propagator.extract(Context.ROOT_CONTEXT, carrier, defaultGetter)
);

assert.deepStrictEqual(extractedSpanContext, {
spanId: '0000000009203331',
traceId: '00000000000000dd8448eb211c80319c',
isRemote: true,
traceFlags: TraceFlags.SAMPLED,
});
});
});
});