-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(core): Handle string sample rates (#11305)
If users pass e.g. `sampleRate: process.env.SAMPLE_RATE`, this will be somewhat silently ignored because we ignore non-number sample rates. This is a bit of a footgun. So this PR changes this so that we actually accept such sample rates. In the process, I also removed the `isNaN` polyfill as this is not really needed anymore. I also updated OTEL to use the same sampling function as core (to avoid this drifting apart), and am using this for `sampleRate`, `tracesSampleRate` and the replay sample rates now. See e.g. #11262
- Loading branch information
Showing
19 changed files
with
184 additions
and
182 deletions.
There are no files selected for viewing
14 changes: 14 additions & 0 deletions
14
dev-packages/browser-integration-tests/suites/public-api/init/stringSampleRate/init.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
import * as Sentry from '@sentry/browser'; | ||
|
||
window.Sentry = Sentry; | ||
|
||
window._errorCount = 0; | ||
|
||
Sentry.init({ | ||
dsn: 'https://[email protected]/1337', | ||
sampleRate: '0', | ||
beforeSend() { | ||
window._errorCount++; | ||
return null; | ||
}, | ||
}); |
3 changes: 3 additions & 0 deletions
3
dev-packages/browser-integration-tests/suites/public-api/init/stringSampleRate/subject.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
Sentry.captureException(new Error('test error')); | ||
|
||
window._testDone = true; |
16 changes: 16 additions & 0 deletions
16
dev-packages/browser-integration-tests/suites/public-api/init/stringSampleRate/test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import { expect } from '@playwright/test'; | ||
|
||
import { sentryTest } from '../../../../utils/fixtures'; | ||
|
||
sentryTest('parses a string sample rate', async ({ getLocalTestUrl, page }) => { | ||
const url = await getLocalTestUrl({ testDir: __dirname }); | ||
|
||
await page.goto(url); | ||
|
||
await page.waitForFunction('window._testDone'); | ||
await page.evaluate('window.Sentry.getClient().flush()'); | ||
|
||
const count = await page.evaluate('window._errorCount'); | ||
|
||
expect(count).toStrictEqual(0); | ||
}); |
8 changes: 8 additions & 0 deletions
8
dev-packages/browser-integration-tests/suites/tracing/stringSampleRate/init.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
import * as Sentry from '@sentry/browser'; | ||
|
||
window.Sentry = Sentry; | ||
|
||
Sentry.init({ | ||
dsn: 'https://[email protected]/1337', | ||
tracesSampleRate: '1', | ||
}); |
3 changes: 3 additions & 0 deletions
3
dev-packages/browser-integration-tests/suites/tracing/stringSampleRate/subject.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
Sentry.startSpan({ name: 'test span' }, () => { | ||
// noop | ||
}); |
17 changes: 17 additions & 0 deletions
17
dev-packages/browser-integration-tests/suites/tracing/stringSampleRate/test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import { expect } from '@playwright/test'; | ||
|
||
import { sentryTest } from '../../../utils/fixtures'; | ||
import { envelopeRequestParser, shouldSkipTracingTest, waitForTransactionRequestOnUrl } from '../../../utils/helpers'; | ||
|
||
sentryTest('parses a string sample rate', async ({ getLocalTestUrl, page }) => { | ||
if (shouldSkipTracingTest()) { | ||
sentryTest.skip(); | ||
} | ||
|
||
const url = await getLocalTestUrl({ testDir: __dirname }); | ||
|
||
const req = await waitForTransactionRequestOnUrl(page, url); | ||
const eventData = envelopeRequestParser(req); | ||
|
||
expect(eventData.contexts?.trace?.data?.['sentry.sample_rate']).toStrictEqual(1); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
import { logger } from '@sentry/utils'; | ||
import { DEBUG_BUILD } from '../debug-build'; | ||
|
||
/** | ||
* Parse a sample rate from a given value. | ||
* This will either return a boolean or number sample rate, if the sample rate is valid (between 0 and 1). | ||
* If a string is passed, we try to convert it to a number. | ||
* | ||
* Any invalid sample rate will return `undefined`. | ||
*/ | ||
export function parseSampleRate(sampleRate: unknown): number | undefined { | ||
if (typeof sampleRate === 'boolean') { | ||
return Number(sampleRate); | ||
} | ||
|
||
const rate = typeof sampleRate === 'string' ? parseFloat(sampleRate) : sampleRate; | ||
if (typeof rate !== 'number' || isNaN(rate)) { | ||
DEBUG_BUILD && | ||
logger.warn( | ||
`[Tracing] Given sample rate is invalid. Sample rate must be a boolean or a number between 0 and 1. Got ${JSON.stringify( | ||
sampleRate, | ||
)} of type ${JSON.stringify(typeof sampleRate)}.`, | ||
); | ||
return undefined; | ||
} | ||
|
||
if (rate < 0 || rate > 1) { | ||
DEBUG_BUILD && | ||
logger.warn(`[Tracing] Given sample rate is invalid. Sample rate must be between 0 and 1. Got ${rate}.`); | ||
return undefined; | ||
} | ||
|
||
return rate; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
import { parseSampleRate } from '../../../src/utils/parseSampleRate'; | ||
|
||
describe('parseSampleRate', () => { | ||
it.each([ | ||
[undefined, undefined], | ||
[null, undefined], | ||
[0, 0], | ||
[1, 1], | ||
[0.555, 0.555], | ||
[2, undefined], | ||
[false, 0], | ||
[true, 1], | ||
['', undefined], | ||
['aha', undefined], | ||
['1', 1], | ||
['1.5', undefined], | ||
['0.555', 0.555], | ||
['0', 0], | ||
])('works with %p', (input, sampleRate) => { | ||
const actual = parseSampleRate(input); | ||
expect(actual).toBe(sampleRate); | ||
}); | ||
}); |
Oops, something went wrong.