How to set the user on server side with @sentry/nextjs
?
#8777
-
Hi, Any of you have an example to set the user of the current request before sending it to the Sentry server? I found https://docs.sentry.io/platforms/node/configuration/async-context/ but it's showing a global example like an Express.js server. In my case I do want to understand what's the best fit with Next.js. Thank you, EDIT: should I just use https://nextjs.org/docs/app/building-your-application/routing/middleware with |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
Hey @sneko The nextjs sdk if set up with automatic instrumentation should be isolating request context for api routes! You shouldn't have to setup anything up or use Using middleware like you mentioned doesn't actually work, because in Next.js middleware (and any other edge runtime features) basically runs in its own process. Any context you give to Sentry in middleware is only available for things happening inside the middleware. You'll have to add a helper that you then wrap every api route/route handler with. See vercel/next.js#15286 (comment). There is no way to do this in a central place with the Next.js framework - it's a limitation of Next. https://dev.to/sneakysensei/nextjs-api-routes-global-error-handling-and-clean-code-practices-3g9p might be useful to read too. Using |
Beta Was this translation helpful? Give feedback.
Hey @sneko
The nextjs sdk if set up with automatic instrumentation should be isolating request context for api routes! You shouldn't have to setup anything up or use
Sentry.runWithAsyncContext
unless you are using a custom server.Using middleware like you mentioned doesn't actually work, because in Next.js middleware (and any other edge runtime features) basically runs in its own process. Any context you give to Sentry in middleware is only available for things happening inside the middleware.
You'll have to add a helper that you then wrap every api route/route handler with. See vercel/next.js#15286 (comment). There is no way to do this in a central place with the Next.js framework - it'…