MCPcopy Index your code
hub / github.com/getsentry/sentry-javascript / isValidSampleRate

Function isValidSampleRate

packages/profiling-node/src/utils.ts:264–287  ·  view source on GitHub ↗
(rate: unknown)

Source from the content-addressed store, hash-verified

262 * @returns {boolean}
263 */
264export function isValidSampleRate(rate: unknown): boolean {
265 // we need to check NaN explicitly because it's of type 'number' and therefore wouldn't get caught by this typecheck
266 if ((typeof rate !== 'number' && typeof rate !== 'boolean') || (typeof rate === 'number' && isNaN(rate))) {
267 DEBUG_BUILD &&
268 debug.warn(
269 `[Profiling] Invalid sample rate. Sample rate must be a boolean or a number between 0 and 1. Got ${JSON.stringify(
270 rate,
271 )} of type ${JSON.stringify(typeof rate)}.`,
272 );
273 return false;
274 }
275
276 // Boolean sample rates are always valid
277 if (rate === true || rate === false) {
278 return true;
279 }
280
281 // in case sampleRate is a boolean, it will get automatically cast to 1 if it's true and 0 if it's false
282 if (rate < 0 || rate > 1) {
283 DEBUG_BUILD && debug.warn(`[Profiling] Invalid sample rate. Sample rate must be between 0 and 1. Got ${rate}.`);
284 return false;
285 }
286 return true;
287}
288
289/**
290 * Checks if the profile is valid and can be sent to Sentry.

Callers 2

utils.test.tsFile · 0.90
maybeProfileSpanFunction · 0.90

Calls 1

warnMethod · 0.65

Tested by

no test coverage detected