Skip to content

fix(utils): Make empty traceparent string count as invalid #5731

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

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
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
3 changes: 3 additions & 0 deletions packages/tracing/test/utils.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,9 @@ describe('extractTraceparentData', () => {
});

test('invalid', () => {
// empty string
expect(extractTraceparentData('')).toBeUndefined();

// trace id wrong length
expect(extractTraceparentData('a-bbbbbbbbbbbbbbbb-1')).toBeUndefined();

Expand Down
30 changes: 17 additions & 13 deletions packages/utils/src/tracing.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,18 +17,22 @@ export const TRACEPARENT_REGEXP = new RegExp(
*/
export function extractTraceparentData(traceparent: string): TraceparentData | undefined {
const matches = traceparent.match(TRACEPARENT_REGEXP);
if (matches) {
let parentSampled: boolean | undefined;
if (matches[3] === '1') {
parentSampled = true;
} else if (matches[3] === '0') {
parentSampled = false;
}
return {
traceId: matches[1],
parentSampled,
parentSpanId: matches[2],
};

if (!traceparent || !matches) {
// empty string or no matches is invalid traceparent data
return undefined;
}

let parentSampled: boolean | undefined;
if (matches[3] === '1') {
parentSampled = true;
} else if (matches[3] === '0') {
parentSampled = false;
}
return undefined;

return {
traceId: matches[1],
parentSampled,
parentSpanId: matches[2],
};
}