Skip to content

Commit ec0f1d7

Browse files
committed
validate sample rate and clean up logging
1 parent 8aef214 commit ec0f1d7

File tree

1 file changed

+34
-4
lines changed

1 file changed

+34
-4
lines changed

packages/tracing/src/hubextensions.ts

Lines changed: 34 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,6 @@ function sample<T extends Transaction>(hub: Hub, transaction: T, sampleContext:
4949
return transaction;
5050
}
5151

52-
logger.log('Tracing enabled');
53-
5452
// we have to test for a pre-existsing sampling decision, in case this transaction is a child transaction and has
5553
// inherited its parent's decision
5654
if (transaction.sampled === undefined) {
@@ -59,9 +57,20 @@ function sample<T extends Transaction>(hub: Hub, transaction: T, sampleContext:
5957
const sampleRate =
6058
typeof options.tracesSampler === 'function' ? options.tracesSampler(sampleContext) : options.tracesSampleRate;
6159

60+
// since this is coming from the user, who knows what we might get
61+
if (!isValidSampleRate(sampleRate)) {
62+
logger.warn(`[Tracing] Discarding trace because of invalid sample rate.`);
63+
transaction.sampled = false;
64+
return transaction;
65+
}
66+
6267
// if the function returned 0, or if the sample rate is set to 0, it's a sign the transaction should be dropped
6368
if (!sampleRate) {
64-
logger.log('Discarding trace because tracesSampler returned 0 or tracesSampleRate is set to 0');
69+
logger.log(
70+
`[Tracing] Discarding trace because ${
71+
typeof options.tracesSampler === 'function' ? 'tracesSampler returned 0' : 'tracesSampleRate is set to 0'
72+
}`,
73+
);
6574
transaction.sampled = false;
6675
return transaction;
6776
}
@@ -71,7 +80,9 @@ function sample<T extends Transaction>(hub: Hub, transaction: T, sampleContext:
7180

7281
// if we're not going to keep it, we're done
7382
if (!transaction.sampled) {
74-
logger.log(`Discarding trace because it's not included in the random sample (sampling rate = ${sampleRate})`);
83+
logger.log(
84+
`[Tracing] Discarding trace because it's not included in the random sample (sampling rate = ${sampleRate})`,
85+
);
7586
return transaction;
7687
}
7788
}
@@ -120,6 +131,25 @@ function getDefaultSampleContext(): SampleContext {
120131
return defaultSampleContext;
121132
}
122133

134+
/**
135+
* Checks the given sample rate to make sure it is valid (a number between 0 and 1).
136+
*/
137+
function isValidSampleRate(rate: unknown): boolean {
138+
if (!(typeof rate === 'number')) {
139+
logger.warn(
140+
`[Tracing] Given sample rate is invalid. Sample rate must be a number between 0 and 1. Got ${JSON.stringify(
141+
rate,
142+
)} of type ${JSON.stringify(typeof rate)}.`,
143+
);
144+
return false;
145+
}
146+
if (rate < 0 || rate > 1) {
147+
logger.warn(`[Tracing] Given sample rate is invalid. Sample rate must be between 0 and 1. Got ${rate}.`);
148+
return false;
149+
}
150+
return true;
151+
}
152+
123153
/**
124154
* Creates a new transaction and adds a sampling decision if it doesn't yet have one.
125155
*

0 commit comments

Comments
 (0)