Skip to content
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

feat(astro): Add bundleSizeOptimizations vite options to integration #13250

Merged
merged 6 commits into from
Aug 6, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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
3 changes: 3 additions & 0 deletions packages/astro/src/integration/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,9 @@ export const sentryAstro = (options: SentryOptions = {}): AstroIntegration => {
sourcemaps: {
assets: uploadOptions.assets ?? [getSourcemapsAssetsGlob(config)],
},
bundleSizeOptimizations: {
excludePerformanceMonitoring: options.bundleSizeOptimizations?.excludeTracing || false,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

m: What about passing all the bundle size optimizations through (or more specifically, defining all of them)?

Copy link
Member Author

@s1gr1d s1gr1d Aug 6, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sounds good to me, @Lms24 what do you think?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sounds good, let's do it!

},
_metaOptions: {
telemetry: {
metaFramework: 'astro',
Expand Down
4 changes: 2 additions & 2 deletions packages/astro/src/integration/snippets.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ const buildCommonInitOptions = (options: SentryOptions): string => `dsn: ${
}`;

/**
* We don't include the `BrowserTracing` integration if the tracesSampleRate is set to 0.
* We don't include the `BrowserTracing` integration if `bundleSizeOptimizations.excludeTracing` is falsy.
* Likewise, we don't include the `Replay` integration if the replaysSessionSampleRate
* and replaysOnErrorSampleRate are set to 0.
*
Expand All @@ -56,7 +56,7 @@ const buildCommonInitOptions = (options: SentryOptions): string => `dsn: ${
const buildClientIntegrations = (options: SentryOptions): string => {
const integrations: string[] = [];

if (options.tracesSampleRate == null || options.tracesSampleRate) {
if (!options.bundleSizeOptimizations?.excludeTracing) {
integrations.push('Sentry.browserTracingIntegration()');
}

Expand Down
117 changes: 66 additions & 51 deletions packages/astro/src/integration/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,62 +25,63 @@ type SdkInitPaths = {

type SourceMapsOptions = {
/**
* Options for the Sentry Vite plugin to customize the source maps upload process.
* If this flag is `true`, and an auth token is detected, the Sentry integration will
* automatically generate and upload source maps to Sentry during a production build.
*
* These options are always read from the `sentryAstro` integration.
* Do not define them in the `sentry.client.config.(js|ts)` or `sentry.server.config.(js|ts)` files.
* @default true
*/
sourceMapsUploadOptions?: {
/**
* If this flag is `true`, and an auth token is detected, the Sentry integration will
* automatically generate and upload source maps to Sentry during a production build.
*
* @default true
*/
enabled?: boolean;
enabled?: boolean;

/**
* The auth token to use when uploading source maps to Sentry.
*
* Instead of specifying this option, you can also set the `SENTRY_AUTH_TOKEN` environment variable.
*
* To create an auth token, follow this guide:
* @see https://docs.sentry.io/product/accounts/auth-tokens/#organization-auth-tokens
*/
authToken?: string;
/**
* The auth token to use when uploading source maps to Sentry.
*
* Instead of specifying this option, you can also set the `SENTRY_AUTH_TOKEN` environment variable.
*
* To create an auth token, follow this guide:
* @see https://docs.sentry.io/product/accounts/auth-tokens/#organization-auth-tokens
*/
authToken?: string;

/**
* The organization slug of your Sentry organization.
* Instead of specifying this option, you can also set the `SENTRY_ORG` environment variable.
*/
org?: string;
/**
* The organization slug of your Sentry organization.
* Instead of specifying this option, you can also set the `SENTRY_ORG` environment variable.
*/
org?: string;

/**
* The project slug of your Sentry project.
* Instead of specifying this option, you can also set the `SENTRY_PROJECT` environment variable.
*/
project?: string;
/**
* The project slug of your Sentry project.
* Instead of specifying this option, you can also set the `SENTRY_PROJECT` environment variable.
*/
project?: string;

/**
* If this flag is `true`, the Sentry plugin will collect some telemetry data and send it to Sentry.
* It will not collect any sensitive or user-specific data.
*
* @default true
*/
telemetry?: boolean;
/**
* If this flag is `true`, the Sentry plugin will collect some telemetry data and send it to Sentry.
* It will not collect any sensitive or user-specific data.
*
* @default true
*/
telemetry?: boolean;

/**
* A glob or an array of globs that specify the build artifacts and source maps that will be uploaded to Sentry.
*
* If this option is not specified, sensible defaults based on your `outDir`, `rootDir` and `adapter`
* config will be used. Use this option to override these defaults, for instance if you have a
* customized build setup that diverges from Astro's defaults.
*
* The globbing patterns must follow the implementation of the `glob` package.
* @see https://www.npmjs.com/package/glob#glob-primer
*/
assets?: string | Array<string>;
};
/**
* A glob or an array of globs that specify the build artifacts and source maps that will be uploaded to Sentry.
*
* If this option is not specified, sensible defaults based on your `outDir`, `rootDir` and `adapter`
* config will be used. Use this option to override these defaults, for instance if you have a
* customized build setup that diverges from Astro's defaults.
*
* The globbing patterns must follow the implementation of the `glob` package.
* @see https://www.npmjs.com/package/glob#glob-primer
*/
assets?: string | Array<string>;
};

type BundleSizeOptimizationOptions = {
/**
* If set to true, the plugin will try to tree-shake performance monitoring statements out.
* Note that the success of this depends on tree shaking generally being enabled in your build.
* Attention: DO NOT enable this when you're using any performance monitoring-related SDK features (e.g. Sentry.startTransaction()).
*/
excludeTracing?: boolean;
};

type InstrumentationOptions = {
Expand Down Expand Up @@ -138,6 +139,20 @@ type SdkEnabledOptions = {
export type SentryOptions = SdkInitPaths &
Pick<Options, 'dsn' | 'release' | 'environment' | 'sampleRate' | 'tracesSampleRate' | 'debug'> &
Pick<BrowserOptions, 'replaysSessionSampleRate' | 'replaysOnErrorSampleRate'> &
SourceMapsOptions &
InstrumentationOptions &
SdkEnabledOptions;
SdkEnabledOptions & {
/**
* Options for the Sentry Vite plugin to customize the source maps upload process.
*
* These options are always read from the `sentryAstro` integration.
* Do not define them in the `sentry.client.config.(js|ts)` or `sentry.server.config.(js|ts)` files.
*/
sourceMapsUploadOptions?: SourceMapsOptions;
/**
* Options for the Sentry Vite plugin to customize bundle size optimizations.
*
* These options are always read from the `sentryAstro` integration.
* Do not define them in the `sentry.client.config.(js|ts)` or `sentry.server.config.(js|ts)` files.
*/
bundleSizeOptimizations?: BundleSizeOptimizationOptions;
};
12 changes: 12 additions & 0 deletions packages/astro/test/integration/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,9 @@ describe('sentryAstro integration', () => {
project: 'my-project',
telemetry: false,
debug: false,
bundleSizeOptimizations: {
excludePerformanceMonitoring: false,
},
sourcemaps: {
assets: ['out/**/*'],
},
Expand All @@ -82,6 +85,9 @@ describe('sentryAstro integration', () => {
project: 'my-project',
telemetry: false,
debug: false,
bundleSizeOptimizations: {
excludePerformanceMonitoring: false,
},
sourcemaps: {
assets: ['dist/**/*'],
},
Expand Down Expand Up @@ -114,6 +120,9 @@ describe('sentryAstro integration', () => {
project: 'my-project',
telemetry: false,
debug: false,
bundleSizeOptimizations: {
excludePerformanceMonitoring: false,
},
sourcemaps: {
assets: ['{.vercel,dist}/**/*'],
},
Expand Down Expand Up @@ -151,6 +160,9 @@ describe('sentryAstro integration', () => {
project: 'my-project',
telemetry: true,
debug: false,
bundleSizeOptimizations: {
excludePerformanceMonitoring: false,
},
sourcemaps: {
assets: ['dist/server/**/*, dist/client/**/*'],
},
Expand Down
22 changes: 20 additions & 2 deletions packages/astro/test/integration/snippets.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,25 @@ describe('buildClientSnippet', () => {
`);
});

it('does not include browserTracingIntegration if tracesSampleRate is 0', () => {
it('does not include browserTracingIntegration if bundleSizeOptimizations.excludeTracing is true', () => {
const snippet = buildClientSnippet({ bundleSizeOptimizations: { excludeTracing: true } });
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: [Sentry.replayIntegration()],
replaysSessionSampleRate: 0.1,
replaysOnErrorSampleRate: 1,
});"
`);
});

it('still include browserTracingIntegration if tracesSampleRate is 0', () => {
const snippet = buildClientSnippet({ tracesSampleRate: 0 });
expect(snippet).toMatchInlineSnapshot(`
"import * as Sentry from "@sentry/astro";
Expand All @@ -63,7 +81,7 @@ describe('buildClientSnippet', () => {
environment: import.meta.env.PUBLIC_VERCEL_ENV,
release: import.meta.env.PUBLIC_VERCEL_GIT_COMMIT_SHA,
tracesSampleRate: 0,
integrations: [Sentry.replayIntegration()],
integrations: [Sentry.browserTracingIntegration(), Sentry.replayIntegration()],
replaysSessionSampleRate: 0.1,
replaysOnErrorSampleRate: 1,
});"
Expand Down
Loading