Skip to content

Commit 441f702

Browse files
authored
fix(astro): Make Replay and BrowserTracing integrations tree-shakeable (#9287)
Fix a Sentry.init code generation bug that always added `BrowserTracing` and `Replay` to the client init code, even if sample rates were specifically set to 0. This prohibited tree shaking of these integrations if users disabled them by setting the respective sample rates.
1 parent dfe5db5 commit 441f702

File tree

2 files changed

+63
-1
lines changed

2 files changed

+63
-1
lines changed

packages/astro/src/integration/snippets.ts

+27-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ export function buildClientSnippet(options: SentryOptions): string {
1616
1717
Sentry.init({
1818
${buildCommonInitOptions(options)}
19-
integrations: [new Sentry.BrowserTracing(), new Sentry.Replay()],
19+
integrations: [${buildClientIntegrations(options)}],
2020
replaysSessionSampleRate: ${options.replaysSessionSampleRate ?? 0.1},
2121
replaysOnErrorSampleRate: ${options.replaysOnErrorSampleRate ?? 1.0},
2222
});`;
@@ -43,3 +43,29 @@ const buildCommonInitOptions = (options: SentryOptions): string => `dsn: ${
4343
tracesSampleRate: ${options.tracesSampleRate ?? 1.0},${
4444
options.sampleRate ? `\n sampleRate: ${options.sampleRate},` : ''
4545
}`;
46+
47+
/**
48+
* We don't include the `BrowserTracing` integration if the tracesSampleRate is set to 0.
49+
* Likewise, we don't include the `Replay` integration if the replaysSessionSampleRate
50+
* and replaysOnErrorSampleRate are set to 0.
51+
*
52+
* This way, we avoid unnecessarily adding the integrations and thereby enable tree shaking of the integrations.
53+
*/
54+
const buildClientIntegrations = (options: SentryOptions): string => {
55+
const integrations: string[] = [];
56+
57+
if (options.tracesSampleRate == null || options.tracesSampleRate) {
58+
integrations.push('new Sentry.BrowserTracing()');
59+
}
60+
61+
if (
62+
options.replaysSessionSampleRate == null ||
63+
options.replaysSessionSampleRate ||
64+
options.replaysOnErrorSampleRate == null ||
65+
options.replaysOnErrorSampleRate
66+
) {
67+
integrations.push('new Sentry.Replay()');
68+
}
69+
70+
return integrations.join(', ');
71+
};

packages/astro/test/integration/snippets.test.ts

+36
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,42 @@ describe('buildClientSnippet', () => {
4949
});"
5050
`);
5151
});
52+
53+
it('does not include BrowserTracing if tracesSampleRate is 0', () => {
54+
const snippet = buildClientSnippet({ tracesSampleRate: 0 });
55+
expect(snippet).toMatchInlineSnapshot(`
56+
"import * as Sentry from \\"@sentry/astro\\";
57+
58+
Sentry.init({
59+
dsn: import.meta.env.PUBLIC_SENTRY_DSN,
60+
debug: false,
61+
environment: import.meta.env.PUBLIC_VERCEL_ENV,
62+
release: import.meta.env.PUBLIC_VERCEL_GIT_COMMIT_SHA,
63+
tracesSampleRate: 0,
64+
integrations: [new Sentry.Replay()],
65+
replaysSessionSampleRate: 0.1,
66+
replaysOnErrorSampleRate: 1,
67+
});"
68+
`);
69+
});
70+
});
71+
72+
it('does not include Replay if replay sample ratest are 0', () => {
73+
const snippet = buildClientSnippet({ replaysSessionSampleRate: 0, replaysOnErrorSampleRate: 0 });
74+
expect(snippet).toMatchInlineSnapshot(`
75+
"import * as Sentry from \\"@sentry/astro\\";
76+
77+
Sentry.init({
78+
dsn: import.meta.env.PUBLIC_SENTRY_DSN,
79+
debug: false,
80+
environment: import.meta.env.PUBLIC_VERCEL_ENV,
81+
release: import.meta.env.PUBLIC_VERCEL_GIT_COMMIT_SHA,
82+
tracesSampleRate: 1,
83+
integrations: [new Sentry.BrowserTracing()],
84+
replaysSessionSampleRate: 0,
85+
replaysOnErrorSampleRate: 0,
86+
});"
87+
`);
5288
});
5389

5490
describe('buildServerSnippet', () => {

0 commit comments

Comments
 (0)