Skip to content

fix(astro): Make Replay and BrowserTracing integrations tree-shakeable #9287

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Oct 17, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 27 additions & 1 deletion packages/astro/src/integration/snippets.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ export function buildClientSnippet(options: SentryOptions): string {

Sentry.init({
${buildCommonInitOptions(options)}
integrations: [new Sentry.BrowserTracing(), new Sentry.Replay()],
integrations: [${buildClientIntegrations(options)}],
replaysSessionSampleRate: ${options.replaysSessionSampleRate ?? 0.1},
replaysOnErrorSampleRate: ${options.replaysOnErrorSampleRate ?? 1.0},
});`;
Expand All @@ -43,3 +43,29 @@ const buildCommonInitOptions = (options: SentryOptions): string => `dsn: ${
tracesSampleRate: ${options.tracesSampleRate ?? 1.0},${
options.sampleRate ? `\n sampleRate: ${options.sampleRate},` : ''
}`;

/**
* We don't include the `BrowserTracing` integration if the tracesSampleRate is set to 0.
* Likewise, we don't include the `Replay` integration if the replaysSessionSampleRate
* and replaysOnErrorSampleRate are set to 0.
*
* This way, we avoid unnecessarily adding the integrations and thereby enable tree shaking of the integrations.
*/
const buildClientIntegrations = (options: SentryOptions): string => {
const integrations: string[] = [];

if (options.tracesSampleRate == null || options.tracesSampleRate) {
integrations.push('new Sentry.BrowserTracing()');
}

if (
options.replaysSessionSampleRate == null ||
options.replaysSessionSampleRate ||
options.replaysOnErrorSampleRate == null ||
options.replaysOnErrorSampleRate
) {
integrations.push('new Sentry.Replay()');
}

return integrations.join(', ');
};
36 changes: 36 additions & 0 deletions packages/astro/test/integration/snippets.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,42 @@ describe('buildClientSnippet', () => {
});"
`);
});

it('does not include BrowserTracing if tracesSampleRate is 0', () => {
const snippet = buildClientSnippet({ tracesSampleRate: 0 });
expect(snippet).toMatchInlineSnapshot(`
"import * as Sentry from \\"@sentry/astro\\";

Sentry.init({
dsn: import.meta.env.PUBLIC_SENTRY_DSN,
debug: false,
environment: import.meta.env.PUBLIC_VERCEL_ENV,
release: import.meta.env.PUBLIC_VERCEL_GIT_COMMIT_SHA,
tracesSampleRate: 0,
integrations: [new Sentry.Replay()],
replaysSessionSampleRate: 0.1,
replaysOnErrorSampleRate: 1,
});"
`);
});
});

it('does not include Replay if replay sample ratest are 0', () => {
const snippet = buildClientSnippet({ replaysSessionSampleRate: 0, replaysOnErrorSampleRate: 0 });
expect(snippet).toMatchInlineSnapshot(`
"import * as Sentry from \\"@sentry/astro\\";

Sentry.init({
dsn: import.meta.env.PUBLIC_SENTRY_DSN,
debug: false,
environment: import.meta.env.PUBLIC_VERCEL_ENV,
release: import.meta.env.PUBLIC_VERCEL_GIT_COMMIT_SHA,
tracesSampleRate: 1,
integrations: [new Sentry.BrowserTracing()],
replaysSessionSampleRate: 0,
replaysOnErrorSampleRate: 0,
});"
`);
});

describe('buildServerSnippet', () => {
Expand Down