Skip to content

Commit 2cc7708

Browse files
authored
doc(sveltekit): Update README with auto instrumentation configuration (#8038)
1 parent 2e6147a commit 2cc7708

File tree

1 file changed

+91
-40
lines changed

1 file changed

+91
-40
lines changed

packages/sveltekit/README.md

Lines changed: 91 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -127,47 +127,25 @@ The Sentry SvelteKit SDK mostly relies on [SvelteKit Hooks](https://kit.svelte.d
127127
// export const handle = sequence(sentryHandle(), yourHandler());
128128
```
129129

130-
### 4. Configuring `load` Functions
130+
### 4. Vite Setup
131131

132-
5. To catch errors and performance data in your universal `load` functions (e.g. in `+page.(js|ts)`), wrap our `wrapLoadWithSentry` function around your load code:
132+
Add `sentrySvelteKit` to your Vite plugins in `vite.config.(js|ts)` file so that the Sentry SDK can apply build-time features.
133+
Make sure that it is added _before_ the `sveltekit` plugin:
133134

134-
```javascript
135-
// +page.(js|ts)
136-
import { wrapLoadWithSentry } from '@sentry/sveltekit';
137-
138-
export const load = wrapLoadWithSentry((event) => {
139-
//... your load code
140-
});
141-
```
142-
143-
6. To catch errors and performance data in your server `load` functions (e.g. in `+page.server.(js|ts)`), wrap our `wrapServerLoadWithSentry` function around your load code:
144-
145-
```javascript
146-
// +page.server.(js|ts)
147-
import { wrapServerLoadWithSentry } from '@sentry/sveltekit';
148-
149-
export const load = wrapServerLoadWithSentry((event) => {
150-
//... your server load code
151-
});
152-
```
153-
154-
### 5. Vite Setup
135+
```javascript
136+
// vite.config.(js|ts)
137+
import { sveltekit } from '@sveltejs/kit/vite';
138+
import { sentrySvelteKit } from '@sentry/sveltekit';
155139

156-
1. Add our `sentrySvelteKit` plugins to your `vite.config.(js|ts)` file so that the Sentry SDK can apply build-time features.
157-
Make sure that it is added before the `sveltekit` plugin:
140+
export default {
141+
plugins: [sentrySvelteKit(), sveltekit()],
142+
// ... rest of your Vite config
143+
};
144+
```
158145

159-
```javascript
160-
// vite.config.(js|ts)
161-
import { sveltekit } from '@sveltejs/kit/vite';
162-
import { sentrySvelteKit } from '@sentry/sveltekit';
163-
164-
export default {
165-
plugins: [sentrySvelteKit(), sveltekit()],
166-
// ... rest of your Vite config
167-
};
168-
```
146+
This adds the [Sentry Vite Plugin](https://github.com/getsentry/sentry-javascript-bundler-plugins/tree/main/packages/vite-plugin) to your Vite config to automatically upload source maps to Sentry.
169147

170-
This adds the [Sentry Vite Plugin](https://github.com/getsentry/sentry-javascript-bundler-plugins/tree/main/packages/vite-plugin) to your Vite config to automatically upload source maps to Sentry.
148+
---
171149

172150
## Uploading Source Maps
173151

@@ -252,14 +230,87 @@ export default {
252230
};
253231
```
254232

233+
## Configure Auto-Instrumentation
234+
235+
The SDK mostly relies on [SvelteKit's hooks](https://kit.svelte.dev/docs/hooks) to collect error and performance data. However, SvelteKit doesn't yet offer a hook for universal or server-only `load` function calls. Therefore, the SDK uses a Vite plugin to auto-instrument `load` functions so that you don't have to add a Sentry wrapper to each function manually. Auto-instrumentation is enabled by default, as soon as you add the `sentrySvelteKit()` function call to your `vite.config.(js|ts)`. However, you can customize the behavior, or disable it entirely. In this case, you can still manually wrap specific `load` functions with the `withSentry` function.
236+
237+
Note: The SDK will only auto-instrument `load` functions in `+page` or `+layout` files that do not yet contain any Sentry code.
238+
If you already have custom Sentry code in such files, you'll have to [manually](#instrument-load-functions-manually) add our wrapper to your `load` functions.
239+
240+
241+
### Customize Auto-instrumentation
242+
243+
By passing the `autoInstrument` option to `sentrySvelteKit` you can disable auto-instrumentation entirely, or customize which `load` functions should be instrumented:
244+
245+
```javascript
246+
// vite.config.(js|ts)
247+
import { sveltekit } from '@sveltejs/kit/vite';
248+
import { sentrySvelteKit } from '@sentry/sveltekit';
249+
250+
export default {
251+
plugins: [
252+
sentrySvelteKit({
253+
autoInstrument: {
254+
load: true, // universal load functions
255+
serverLoad: false, // server-only load functions
256+
},
257+
}),
258+
sveltekit(),
259+
],
260+
// ... rest of your Vite config
261+
};
262+
```
263+
264+
### Disable Auto-instrumentation
265+
266+
If you set the `autoInstrument` option to `false`, the SDK won't auto-instrument any `load` function. You can still [manually instrument](#instrument-load-functions-manually) specific `load` functions.
267+
268+
```javascript
269+
// vite.config.(js|ts)
270+
import { sveltekit } from '@sveltejs/kit/vite';
271+
import { sentrySvelteKit } from '@sentry/sveltekit';
272+
273+
export default {
274+
plugins: [
275+
sentrySvelteKit({
276+
autoInstrument: false;
277+
}),
278+
sveltekit(),
279+
],
280+
// ... rest of your Vite config
281+
};
282+
```
283+
284+
### Instrument `load` Functions Manually
285+
286+
If you don't want to use auto-instrumentation, you can also manually instrument specific `load` functions with our load function wrappers:
287+
288+
To instrument your universal `load` functions in `+(page|layout).(js|ts)`, wrap our `wrapLoadWithSentry` function around your load code:
289+
290+
```javascript
291+
import { wrapLoadWithSentry } from '@sentry/sveltekit';
292+
293+
export const load = wrapLoadWithSentry((event) => {
294+
//... your load code
295+
});
296+
```
297+
298+
To instrument server `load` functions in `+(page|layout).server.(js|ts)`, wrap our `wrapServerLoadWithSentry` function around your load code:
299+
300+
```javascript
301+
import { wrapServerLoadWithSentry } from '@sentry/sveltekit';
302+
303+
export const load = wrapServerLoadWithSentry((event) => {
304+
//... your server load code
305+
});
306+
```
307+
308+
255309
## Known Limitations
256310

257311
This SDK is still under active development.
258-
Take a look at our [SvelteKit SDK Development Roadmap](https://github.com/getsentry/sentry-javascript/issues/6692) to follow the progress:
312+
Take a look at our [SvelteKit SDK Development Roadmap](https://github.com/getsentry/sentry-javascript/issues/6692) to follow the progress.
259313

260314
- **Adapters** other than `@sveltejs/adapter-node` are currently not supported.
261315
We haven't yet tested other platforms like Vercel.
262316
This is on our roadmap but it will come at a later time.
263-
264-
- We're aiming to **simplify SDK setup** in the future so that you don't have to go in and manually add our wrappers to all your `load` functions.
265-
This will be addressed once the SDK supports all Sentry features.

0 commit comments

Comments
 (0)