Skip to content

Commit 4a1ca4b

Browse files
Lms24lforst
andauthored
feat(astro): Document middleware and server instrumentation usage (#8581)
Add documentation on how to use and customize the Sentry Astro middleware Co-authored-by: Luca Forstner <[email protected]>
1 parent efa7483 commit 4a1ca4b

File tree

1 file changed

+71
-0
lines changed

1 file changed

+71
-0
lines changed

src/platforms/javascript/guides/astro/manual-setup.mdx

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,77 @@ export default defineConfig({
190190
});
191191
```
192192

193+
## Configure Server Instrumentation
194+
195+
<Alert>
196+
197+
Auto instrumentation only works for Astro 3.5.2 or newer. If you're using an older version, you need to [manually add the Sentry middleware](#manually-add-server-instrumentation) instead.
198+
199+
</Alert>
200+
201+
In SSR or hybrid mode configured Astro apps, the Sentry Astro integration will automatically add an [Astro middleware](https://docs.astro.build/en/guides/middleware/) request handler to your server code. This middleware enhances the data collected by Sentry on the server side by:
202+
203+
- Collecting performance spans for incoming requests
204+
- Enabeling distributed tracing between client and server
205+
- Enhancing captured errors with additional information
206+
207+
### Manually Add Server Instrumentation
208+
209+
For Astro versions below 3.5.2, you need to manually add the Sentry middleware to your `src/middleware.js` file:
210+
211+
```javascript {filename:src/middleware.(js|ts)}
212+
import * as Sentry from "@sentry/astro";
213+
import { sequence } from "astro:middleware";
214+
215+
export const onRequest = sequence(
216+
Sentry.handleRequest()
217+
// other middleware handlers
218+
);
219+
```
220+
221+
If you have multiple request handlers, make sure to add the Sentry middleware as the first handler in the sequence.
222+
223+
### Customize Server Instrumentation
224+
225+
Sentry's Astro middleware allows control over what additional data should be added to the recorded request spans.
226+
227+
To customize the server instrumentation, add the Sentry middleware to your `src/middleware.js` file:
228+
229+
```javascript {filename:src/middleware.(js|ts)}
230+
import * as Sentry from "@sentry/astro";
231+
import { sequence } from "astro:middleware";
232+
233+
export const onRequest = sequence(
234+
Sentry.handleRequest({
235+
trackClientIp: true, // defaults to false
236+
trackHeaders: true, // defaults to false
237+
})
238+
// other middleware handlers
239+
);
240+
```
241+
242+
If you're using Astro 3.5.2 or newer, make sure to also disable auto instrumentation as shown below.
243+
244+
### Disable Auto Server Instrumentation
245+
246+
For Astro 3.5.2 or newer, you can disable the automatic server instrumentation by turning off the `requestHandler` auto instrumentation option:
247+
248+
```javascript {filename:astro.config.mjs}
249+
import { defineConfig } from "astro/config";
250+
import sentry from "@sentry/astro";
251+
252+
export default defineConfig({
253+
integrations: [
254+
sentry({
255+
autoInstrumentation: {
256+
requestHandler: false,
257+
},
258+
}),
259+
],
260+
output: "server",
261+
});
262+
```
263+
193264
## Configure Source Maps Upload
194265

195266
To enable readable stack traces, <PlatformLink to="/sourcemaps">set up source maps upload</PlatformLink> for your production builds.

0 commit comments

Comments
 (0)