Skip to content

Commit 8054398

Browse files
committed
handle exponential sample rate notation
1 parent a47cb46 commit 8054398

File tree

2 files changed

+34
-1
lines changed

2 files changed

+34
-1
lines changed

packages/tracing/src/span.ts

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -402,7 +402,16 @@ export class Span implements SpanInterface {
402402
transactionName && setBaggageValue(baggage, 'transaction', transactionName);
403403
userId && setBaggageValue(baggage, 'userid', userId);
404404
userSegment && setBaggageValue(baggage, 'usersegment', userSegment);
405-
sampelRate && setBaggageValue(baggage, 'samplerate', sampelRate.toString());
405+
sampelRate &&
406+
setBaggageValue(
407+
baggage,
408+
'samplerate',
409+
// This will make sure that expnent notation (e.g. 1.45e-14) is converted to simple decimal representation
410+
// Another edge case would be something like Number.NEGATIVE_INFINITY in which case we could still
411+
// add something like .replace(/-?∞/, '0'). For the sake of saving bytes, I'll not add this until
412+
// it becomes a problem
413+
sampelRate.toLocaleString('fullwide', { useGrouping: false, maximumFractionDigits: 16 }),
414+
);
406415
publicKey && setBaggageValue(baggage, 'publickey', publicKey);
407416
traceId && setBaggageValue(baggage, 'traceid', traceId);
408417

packages/tracing/test/span.test.ts

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -453,5 +453,29 @@ describe('Span', () => {
453453
});
454454
expect(baggage && getThirdPartyBaggage(baggage)).toStrictEqual('');
455455
});
456+
457+
test('exponential sample rate notation is converted to decimal notation', () => {
458+
const transaction = new Transaction(
459+
{
460+
name: 'tx',
461+
metadata: {
462+
transactionSampling: { rate: 1.45e-14, method: 'client_rate' },
463+
},
464+
},
465+
hub,
466+
);
467+
468+
const baggage = transaction.getBaggage();
469+
470+
expect(baggage && isSentryBaggageEmpty(baggage)).toBe(false);
471+
expect(baggage && getSentryBaggageItems(baggage)).toStrictEqual({
472+
release: '1.0.1',
473+
environment: 'production',
474+
transaction: 'tx',
475+
samplerate: '0.0000000000000145',
476+
traceid: expect.any(String),
477+
});
478+
expect(baggage && getThirdPartyBaggage(baggage)).toStrictEqual('');
479+
});
456480
});
457481
});

0 commit comments

Comments
 (0)