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 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
37 changes: 23 additions & 14 deletions packages/astro/src/integration/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import * as path from 'path';
import { sentryVitePlugin } from '@sentry/vite-plugin';
import type { AstroConfig, AstroIntegration } from 'astro';

import { dropUndefinedKeys } from '@sentry/utils';
import { buildClientSnippet, buildSdkInitFileImportSnippet, buildServerSnippet } from './snippets';
import type { SentryOptions } from './types';

Expand Down Expand Up @@ -40,21 +41,29 @@ export const sentryAstro = (options: SentryOptions = {}): AstroIntegration => {
sourcemap: true,
},
plugins: [
sentryVitePlugin({
org: uploadOptions.org ?? env.SENTRY_ORG,
project: uploadOptions.project ?? env.SENTRY_PROJECT,
authToken: uploadOptions.authToken ?? env.SENTRY_AUTH_TOKEN,
telemetry: uploadOptions.telemetry ?? true,
sourcemaps: {
assets: uploadOptions.assets ?? [getSourcemapsAssetsGlob(config)],
},
_metaOptions: {
telemetry: {
metaFramework: 'astro',
sentryVitePlugin(
dropUndefinedKeys({
org: uploadOptions.org ?? env.SENTRY_ORG,
project: uploadOptions.project ?? env.SENTRY_PROJECT,
authToken: uploadOptions.authToken ?? env.SENTRY_AUTH_TOKEN,
telemetry: uploadOptions.telemetry ?? true,
sourcemaps: {
assets: uploadOptions.assets ?? [getSourcemapsAssetsGlob(config)],
},
},
debug: options.debug ?? false,
}),
bundleSizeOptimizations: {
...options.bundleSizeOptimizations,
// TODO: with a future version of the vite plugin (probably 2.22.0) this re-mapping is not needed anymore
// ref: https://github.com/getsentry/sentry-javascript-bundler-plugins/pull/582
excludePerformanceMonitoring: options.bundleSizeOptimizations?.excludeTracing,
},
_metaOptions: {
telemetry: {
metaFramework: 'astro',
},
},
debug: options.debug ?? false,
}),
),
],
},
});
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
149 changes: 98 additions & 51 deletions packages/astro/src/integration/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,62 +25,95 @@ 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 attempt to tree-shake (remove) any debugging code within the Sentry SDK.
* Note that the success of this depends on tree shaking being enabled in your build tooling.
*
* Setting this option to `true` will disable features like the SDK's `debug` option.
*/
excludeDebugStatements?: boolean;

/**
* 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;

/**
* If set to `true`, the plugin will attempt to tree-shake (remove) code related to the Sentry SDK's Session Replay Shadow DOM recording functionality.
* Note that the success of this depends on tree shaking being enabled in your build tooling.
*
* This option is safe to be used when you do not want to capture any Shadow DOM activity via Sentry Session Replay.
*/
excludeReplayShadowDom?: boolean;

/**
* If set to `true`, the plugin will attempt to tree-shake (remove) code related to the Sentry SDK's Session Replay `iframe` recording functionality.
* Note that the success of this depends on tree shaking being enabled in your build tooling.
*
* You can safely do this when you do not want to capture any `iframe` activity via Sentry Session Replay.
*/
excludeReplayIframe?: boolean;

/**
* If set to `true`, the plugin will attempt to tree-shake (remove) code related to the Sentry SDK's Session Replay's Compression Web Worker.
* Note that the success of this depends on tree shaking being enabled in your build tooling.
*
* **Notice:** You should only do use this option if you manually host a compression worker and configure it in your Sentry Session Replay integration config via the `workerUrl` option.
*/
excludeReplayWorker?: boolean;
};

type InstrumentationOptions = {
Expand Down Expand Up @@ -138,6 +171,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;
};
4 changes: 4 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,7 @@ describe('sentryAstro integration', () => {
project: 'my-project',
telemetry: false,
debug: false,
bundleSizeOptimizations: {},
sourcemaps: {
assets: ['out/**/*'],
},
Expand All @@ -82,6 +83,7 @@ describe('sentryAstro integration', () => {
project: 'my-project',
telemetry: false,
debug: false,
bundleSizeOptimizations: {},
sourcemaps: {
assets: ['dist/**/*'],
},
Expand Down Expand Up @@ -114,6 +116,7 @@ describe('sentryAstro integration', () => {
project: 'my-project',
telemetry: false,
debug: false,
bundleSizeOptimizations: {},
sourcemaps: {
assets: ['{.vercel,dist}/**/*'],
},
Expand Down Expand Up @@ -151,6 +154,7 @@ describe('sentryAstro integration', () => {
project: 'my-project',
telemetry: true,
debug: false,
bundleSizeOptimizations: {},
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