Commit 264ad0b
authored
feat(nuxt): Implement server middleware instrumentation (getsentry#17796)
This pull request introduces instrumentation for Nuxt middleware,
ensuring that all middleware handlers are automatically wrapped with
tracing and error reporting functionality. The integration is achieved
through build-time transformation.
recap:
* Adds a new build-time Rollup plugin
(`middlewareInstrumentationPlugin`) that automatically wraps all
detected middleware handlers with Sentry instrumentation during the
Nitro build process.
* Implements the `wrapMiddlewareHandler` utility, which wraps middleware
handlers to start a Sentry span, capture request data, record
exceptions, and flush events in serverless environments.
* Updates the Nuxt module setup to inject Sentry middleware imports and
instrumentation hooks during initialization, ensuring the new tracing
logic is included in server builds.
<!-- CURSOR_SUMMARY -->
---
> [!NOTE]
> Adds build-time wrapping of Nuxt server middleware with Sentry spans
and error capture, plus Nuxt 3/4 e2e and unit tests.
>
> - **SDK/Runtime**:
> - Implement `wrapMiddlewareHandlerWithSentry` in
`packages/nuxt/src/runtime/hooks/wrapMiddlewareHandler.ts` to start
spans, set attributes (op, origin, route, method, headers), handle hook
arrays (`onRequest`, `onBeforeResponse`), and capture errors.
> - Add middleware build-time transformation via
`packages/nuxt/src/vite/middlewareConfig.ts` (server import + Rollup
plugin to wrap `defineEventHandler`/`eventHandler`).
> - Integrate instrumentation in `packages/nuxt/src/module.ts` by
calling `addMiddlewareImports` and `addMiddlewareInstrumentation` during
Nitro init when server config is present.
> - **Tests**:
> - Add e2e apps and Playwright tests for Nuxt 3 and Nuxt 4
(`dev-packages/e2e-tests/test-applications/nuxt-{3,4}`) covering span
creation, attributes, parent-child relationships, and error propagation
across hooks and arrays.
> - Add unit tests for wrapper behavior in
`packages/nuxt/test/runtime/hooks/wrapMiddlewareHandler.test.ts`.
>
> <sup>Written by [Cursor
Bugbot](https://cursor.com/dashboard?tab=bugbot) for commit
9a4a1f3. This will update automatically
on new commits. Configure
[here](https://cursor.com/dashboard?tab=bugbot).</sup>
<!-- /CURSOR_SUMMARY -->1 parent 11ea830 commit 264ad0b
File tree
18 files changed
+1716
-0
lines changed- dev-packages/e2e-tests/test-applications
- nuxt-3
- server
- api
- middleware
- tests
- nuxt-4
- server
- api
- middleware
- tests
- packages/nuxt
- src
- runtime/hooks
- vite
- test/runtime/hooks
18 files changed
+1716
-0
lines changedLines changed: 15 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
Lines changed: 6 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
Lines changed: 7 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
Lines changed: 12 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
Lines changed: 36 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
Lines changed: 47 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
| 47 | + | |
0 commit comments