Skip to content

Commit c36e75b

Browse files
authored
feat(astro): Deprecate passing runtime config to astro integration (#16839)
Closes #16837 This should not break anything, I actually added a test to verify injecting this works as expected. We now simply console.warn when some runtime config is passed in the astro integration. Also, no more type completion exists for these, but they are still accepted via `Record<string, unknown>`.
1 parent bd6095c commit c36e75b

File tree

4 files changed

+74
-65
lines changed

4 files changed

+74
-65
lines changed

packages/astro/src/integration/index.ts

Lines changed: 35 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,36 @@ export const sentryAstro = (options: SentryOptions = {}): AstroIntegration => {
2323
// Will revisit this later.
2424
const env = process.env;
2525

26+
const {
27+
enabled,
28+
clientInitPath,
29+
serverInitPath,
30+
autoInstrumentation,
31+
sourceMapsUploadOptions,
32+
bundleSizeOptimizations,
33+
debug,
34+
...otherOptions
35+
} = options;
36+
37+
const otherOptionsKeys = Object.keys(otherOptions);
38+
if (otherOptionsKeys.length > 0) {
39+
consoleSandbox(() => {
40+
// eslint-disable-next-line no-console
41+
console.warn(
42+
`[Sentry] You passed in additional options (${otherOptionsKeys.join(
43+
', ',
44+
)}) to the Sentry integration. This is deprecated and will stop working in a future version. Instead, configure the Sentry SDK in your \`sentry.client.config.(js|ts)\` or \`sentry.server.config.(js|ts)\` files.`,
45+
);
46+
});
47+
}
48+
2649
const sdkEnabled = {
27-
client: typeof options.enabled === 'boolean' ? options.enabled : options.enabled?.client ?? true,
28-
server: typeof options.enabled === 'boolean' ? options.enabled : options.enabled?.server ?? true,
50+
client: typeof enabled === 'boolean' ? enabled : enabled?.client ?? true,
51+
server: typeof enabled === 'boolean' ? enabled : enabled?.server ?? true,
2952
};
3053

3154
const sourceMapsNeeded = sdkEnabled.client || sdkEnabled.server;
32-
const { unstable_sentryVitePluginOptions, ...uploadOptions } = options.sourceMapsUploadOptions || {};
55+
const { unstable_sentryVitePluginOptions, ...uploadOptions } = sourceMapsUploadOptions || {};
3356
const shouldUploadSourcemaps = (sourceMapsNeeded && uploadOptions?.enabled) ?? true;
3457

3558
// We don't need to check for AUTH_TOKEN here, because the plugin will pick it up from the env
@@ -72,18 +95,15 @@ export const sentryAstro = (options: SentryOptions = {}): AstroIntegration => {
7295
},
7396
},
7497
...unstable_sentryVitePluginOptions,
75-
debug: options.debug ?? false,
98+
debug: debug ?? false,
7699
sourcemaps: {
77100
assets: uploadOptions.assets ?? [getSourcemapsAssetsGlob(config)],
78101
filesToDeleteAfterUpload:
79102
uploadOptions?.filesToDeleteAfterUpload ?? updatedFilesToDeleteAfterUpload,
80103
...unstable_sentryVitePluginOptions?.sourcemaps,
81104
},
82105
bundleSizeOptimizations: {
83-
...options.bundleSizeOptimizations,
84-
// TODO: with a future version of the vite plugin (probably 2.22.0) this re-mapping is not needed anymore
85-
// ref: https://github.com/getsentry/sentry-javascript-bundler-plugins/pull/582
86-
excludePerformanceMonitoring: options.bundleSizeOptimizations?.excludeTracing,
106+
...bundleSizeOptimizations,
87107
...unstable_sentryVitePluginOptions?.bundleSizeOptimizations,
88108
},
89109
}),
@@ -93,28 +113,24 @@ export const sentryAstro = (options: SentryOptions = {}): AstroIntegration => {
93113
}
94114

95115
if (sdkEnabled.client) {
96-
const pathToClientInit = options.clientInitPath
97-
? path.resolve(options.clientInitPath)
98-
: findDefaultSdkInitFile('client');
116+
const pathToClientInit = clientInitPath ? path.resolve(clientInitPath) : findDefaultSdkInitFile('client');
99117

100118
if (pathToClientInit) {
101-
options.debug && logger.info(`Using ${pathToClientInit} for client init.`);
119+
debug && logger.info(`Using ${pathToClientInit} for client init.`);
102120
injectScript('page', buildSdkInitFileImportSnippet(pathToClientInit));
103121
} else {
104-
options.debug && logger.info('Using default client init.');
122+
debug && logger.info('Using default client init.');
105123
injectScript('page', buildClientSnippet(options || {}));
106124
}
107125
}
108126

109127
if (sdkEnabled.server) {
110-
const pathToServerInit = options.serverInitPath
111-
? path.resolve(options.serverInitPath)
112-
: findDefaultSdkInitFile('server');
128+
const pathToServerInit = serverInitPath ? path.resolve(serverInitPath) : findDefaultSdkInitFile('server');
113129
if (pathToServerInit) {
114-
options.debug && logger.info(`Using ${pathToServerInit} for server init.`);
130+
debug && logger.info(`Using ${pathToServerInit} for server init.`);
115131
injectScript('page-ssr', buildSdkInitFileImportSnippet(pathToServerInit));
116132
} else {
117-
options.debug && logger.info('Using default server init.');
133+
debug && logger.info('Using default server init.');
118134
injectScript('page-ssr', buildServerSnippet(options || {}));
119135
}
120136

@@ -136,7 +152,7 @@ export const sentryAstro = (options: SentryOptions = {}): AstroIntegration => {
136152
}
137153

138154
const isSSR = config && (config.output === 'server' || config.output === 'hybrid');
139-
const shouldAddMiddleware = sdkEnabled.server && options.autoInstrumentation?.requestHandler !== false;
155+
const shouldAddMiddleware = sdkEnabled.server && autoInstrumentation?.requestHandler !== false;
140156

141157
// Guarding calling the addMiddleware function because it was only introduced in [email protected]
142158
// Users on older versions of astro will need to add the middleware manually.

packages/astro/src/integration/snippets.ts

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ export function buildSdkInitFileImportSnippet(filePath: string): string {
1313
* default options.
1414
*/
1515
export function buildClientSnippet(options: SentryOptions): string {
16-
/* eslint-disable deprecation/deprecation */
1716
return `import * as Sentry from "@sentry/astro";
1817
1918
Sentry.init({
@@ -22,7 +21,6 @@ Sentry.init({
2221
replaysSessionSampleRate: ${options.replaysSessionSampleRate ?? 0.1},
2322
replaysOnErrorSampleRate: ${options.replaysOnErrorSampleRate ?? 1.0},
2423
});`;
25-
/* eslint-enable deprecation/deprecation */
2624
}
2725

2826
/**
@@ -37,7 +35,6 @@ Sentry.init({
3735
});`;
3836
}
3937

40-
/* eslint-disable deprecation/deprecation */
4138
const buildCommonInitOptions = (options: SentryOptions): string => `dsn: ${
4239
options.dsn ? JSON.stringify(options.dsn) : 'import.meta.env.PUBLIC_SENTRY_DSN'
4340
},
@@ -47,7 +44,6 @@ const buildCommonInitOptions = (options: SentryOptions): string => `dsn: ${
4744
tracesSampleRate: ${options.tracesSampleRate ?? 1.0},${
4845
options.sampleRate ? `\n sampleRate: ${options.sampleRate},` : ''
4946
}`;
50-
/* eslint-enable deprecation/deprecation */
5147

5248
/**
5349
* We don't include the `BrowserTracing` integration if `bundleSizeOptimizations.excludeTracing` is falsy.
@@ -64,13 +60,9 @@ const buildClientIntegrations = (options: SentryOptions): string => {
6460
}
6561

6662
if (
67-
// eslint-disable-next-line deprecation/deprecation
6863
options.replaysSessionSampleRate == null ||
69-
// eslint-disable-next-line deprecation/deprecation
7064
options.replaysSessionSampleRate ||
71-
// eslint-disable-next-line deprecation/deprecation
7265
options.replaysOnErrorSampleRate == null ||
73-
// eslint-disable-next-line deprecation/deprecation
7466
options.replaysOnErrorSampleRate
7567
) {
7668
integrations.push('Sentry.replayIntegration()');

packages/astro/src/integration/types.ts

Lines changed: 10 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
import type { BrowserOptions } from '@sentry/browser';
2-
import type { Options } from '@sentry/core';
31
import type { SentryVitePluginOptions } from '@sentry/vite-plugin';
42

53
type SdkInitPaths = {
@@ -185,40 +183,14 @@ type SdkEnabledOptions = {
185183
};
186184
};
187185

188-
type DeprecatedRuntimeOptions = Pick<
189-
Options,
190-
'environment' | 'release' | 'dsn' | 'debug' | 'sampleRate' | 'tracesSampleRate'
191-
> &
192-
Pick<BrowserOptions, 'replaysSessionSampleRate' | 'replaysOnErrorSampleRate'> & {
193-
/**
194-
* @deprecated Use the `environment` option in your runtime-specific Sentry.init() call in sentry.client.config.(js|ts) or sentry.server.config.(js|ts) instead.
195-
*/
196-
environment?: string;
197-
/**
198-
* @deprecated Use the `release` option in your runtime-specific Sentry.init() call in sentry.client.config.(js|ts) or sentry.server.config.(js|ts) instead.
199-
*/
200-
release?: string;
201-
/**
202-
* @deprecated Use the `dsn` option in your runtime-specific Sentry.init() call in sentry.client.config.(js|ts) or sentry.server.config.(js|ts) instead.
203-
*/
204-
dsn?: string;
205-
/**
206-
* @deprecated Use the `sampleRate` option in your runtime-specific Sentry.init() call in sentry.client.config.(js|ts) or sentry.server.config.(js|ts) instead.
207-
*/
208-
sampleRate?: number;
209-
/**
210-
* @deprecated Use the `tracesSampleRate` option in your runtime-specific Sentry.init() call in sentry.client.config.(js|ts) or sentry.server.config.(js|ts) instead.
211-
*/
212-
tracesSampleRate?: number;
213-
/**
214-
* @deprecated Use the `replaysSessionSampleRate` option in your Sentry.init() call in sentry.client.config.(js|ts) instead.
215-
*/
216-
replaysSessionSampleRate?: number;
217-
/**
218-
* @deprecated Use the `replaysOnErrorSampleRate` option in your Sentry.init() call in sentry.client.config.(js|ts) instead.
219-
*/
220-
replaysOnErrorSampleRate?: number;
221-
};
186+
/**
187+
* We accept aribtrary options that are passed through to the Sentry SDK.
188+
* This is not recommended and will stop working in a future version.
189+
* Note: Not all options are actually passed through, only a select subset:
190+
* release, environment, dsn, debug, sampleRate, tracesSampleRate, replaysSessionSampleRate, replaysOnErrorSampleRate
191+
* @deprecated This will be removed in a future major.
192+
**/
193+
type DeprecatedRuntimeOptions = Record<string, unknown>;
222194

223195
/**
224196
* A subset of Sentry SDK options that can be set via the `sentryAstro` integration.
@@ -230,7 +202,6 @@ type DeprecatedRuntimeOptions = Pick<
230202
* If you specify a dedicated init file, the SDK options passed to `sentryAstro` will be ignored.
231203
*/
232204
export type SentryOptions = SdkInitPaths &
233-
DeprecatedRuntimeOptions &
234205
InstrumentationOptions &
235206
SdkEnabledOptions & {
236207
/**
@@ -251,4 +222,5 @@ export type SentryOptions = SdkInitPaths &
251222
* If enabled, prints debug logs during the build process.
252223
*/
253224
debug?: boolean;
254-
};
225+
// eslint-disable-next-line deprecation/deprecation
226+
} & DeprecatedRuntimeOptions;

packages/astro/test/integration/index.test.ts

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -304,6 +304,35 @@ describe('sentryAstro integration', () => {
304304
expect(injectScript).toHaveBeenCalledWith('page-ssr', expect.stringContaining('Sentry.init'));
305305
});
306306

307+
it('injects runtime config into client and server init scripts and warns about deprecation', async () => {
308+
const consoleWarnSpy = vi.spyOn(console, 'warn').mockImplementation(() => {});
309+
const integration = sentryAstro({
310+
environment: 'test',
311+
release: '1.0.0',
312+
dsn: 'https://test.sentry.io/123',
313+
debug: true,
314+
bundleSizeOptimizations: {},
315+
});
316+
317+
expect(integration.hooks['astro:config:setup']).toBeDefined();
318+
// @ts-expect-error - the hook exists and we only need to pass what we actually use
319+
await integration.hooks['astro:config:setup']({ updateConfig, injectScript, config, logger: { info: vi.fn() } });
320+
321+
expect(consoleWarnSpy).toHaveBeenCalledWith(
322+
'[Sentry] You passed in additional options (environment, release, dsn) to the Sentry integration. This is deprecated and will stop working in a future version. Instead, configure the Sentry SDK in your `sentry.client.config.(js|ts)` or `sentry.server.config.(js|ts)` files.',
323+
);
324+
325+
expect(injectScript).toHaveBeenCalledTimes(2);
326+
expect(injectScript).toHaveBeenCalledWith('page', expect.stringContaining('Sentry.init'));
327+
expect(injectScript).toHaveBeenCalledWith('page', expect.stringContaining('dsn: "https://test.sentry.io/123"'));
328+
expect(injectScript).toHaveBeenCalledWith('page', expect.stringContaining('release: "1.0.0"'));
329+
expect(injectScript).toHaveBeenCalledWith('page', expect.stringContaining('environment: "test"'));
330+
expect(injectScript).toHaveBeenCalledWith('page-ssr', expect.stringContaining('Sentry.init'));
331+
expect(injectScript).toHaveBeenCalledWith('page-ssr', expect.stringContaining('dsn: "https://test.sentry.io/123"'));
332+
expect(injectScript).toHaveBeenCalledWith('page-ssr', expect.stringContaining('release: "1.0.0"'));
333+
expect(injectScript).toHaveBeenCalledWith('page-ssr', expect.stringContaining('environment: "test"'));
334+
});
335+
307336
it("doesn't inject client init script if `enabled.client` is `false`", async () => {
308337
const integration = sentryAstro({ enabled: { client: false } });
309338

0 commit comments

Comments
 (0)