Skip to content

Commit

Permalink
feat(astro): Add bundleSizeOptimizations vite options to integration (
Browse files Browse the repository at this point in the history
#13250)

The bundler-plugins still refer to the option as
`excludePerformanceMonitoring`
([here](https://github.com/getsentry/sentry-javascript-bundler-plugins/blob/main/packages/bundler-plugin-core/src/types.ts#L260)),
but this is going to be renamed to `excludeTracing`, so I already used
the new naming as discussed with @Lms24 and @mydea.

part of #13013
  • Loading branch information
s1gr1d authored Aug 6, 2024
1 parent 0666eb5 commit b0d4926
Show file tree
Hide file tree
Showing 5 changed files with 147 additions and 69 deletions.
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

0 comments on commit b0d4926

Please sign in to comment.