You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
This repository was archived by the owner on Oct 13, 2026. It is now read-only.
Repository navigation
This repository was archived by the owner on Oct 13, 2026. It is now read-only.
Replay data isn't available to tunnel middleware if Sentry tunnel is enabled #562
// src/server-middleware/sentry.tunnel.tsimportaxiosfrom'axios';importexpressfrom'express';// https://github.com/getsentry/examples/blob/a9f50106988ce2d21e41ab8584ebaccef4988709/tunneling/nextjs/pages/api/tunnel.js// Change host appropriately if you run your own Sentry instance.constsentryHost='.sentry.io';// Set knownProjectIds to an array with your Sentry project IDs which you// want to accept through this proxy.constknownProjectIds=['<removed>'];constmiddleware=express();middleware.use(express.text({limit: '50mb'}));middleware.all('/',async(req,res)=>{try{constenvelope=req.body;constpieces=envelope.split('\n');constheader=JSON.parse(pieces[0]);const{ host, pathname }=newURL(header.dsn);if(!host.endsWith(sentryHost)){thrownewError(`invalid host: ${host}`);}constprojectId=pathname.slice(1);if(!knownProjectIds.includes(projectId)){thrownewError(`invalid project id: ${projectId}`);}consturl=`https://${host}/api/${projectId}/envelope/`;constresponse=awaitaxios.post(url,envelope,{timeout: 10000});res.end(JSON.stringify(response.data));}catch(e){console.error(e);res.statusCode=400;res.end(JSON.stringify({status: 'invalid request'}));}});exportdefaultmiddleware;
Steps to reproduce
Add server middleware to handle Sentry requests such src/server-middleware/sentry.tunnel.ts above
Update Sentry configuration to the settings above
Start application
Throw a new error anywhere in an application mounted() function in order to trigger a Sentry error
Notice that in the middleware, req.body is just an empty object
What is Expected?
There should be some kind of data representing the replay event in req.body in the tunnel middleware to forward to Sentry
What is actually happening?
req.body is just an empty object in the tunnel middleware, so there's no replay data to forward to Sentry
I would guess that Sentry payload doesn't have text/* type so express.text probably is not parsing it. You should probably inspect the request that Sentry makes on the client side and adjust the parsing middleware so that it handles that type of payload.
Thanks for the suggestion @rchl. That did seem to get the issue closer to being resolved, but there was still a missing piece that I didn't figure out. Seeing as I'm just evaluating the Session Replay feature, I didn't spend too much time continuing down that road. I found that the setting below solved the issue for now and Session Replay worked using the tunnel as expected. I found this from a similar issue in another repo
Of course the [projectId] and [xxx] needs to be replaced with actual data from the DSN.
(This setup is not as dynamic as the one suggested by Sentry where Sentry origin and project ID is parsed automatically from the request URL but it might be good enough.)
Activity
rchl commentedon Apr 19, 2023
It hardly looks like a problem with this module.
I would guess that Sentry payload doesn't have
text/*type soexpress.textprobably is not parsing it. You should probably inspect the request that Sentry makes on the client side and adjust the parsing middleware so that it handles that type of payload.rchl commentedon Apr 19, 2023
Based on some fix I did in sentry-testkit, maybe a middleware like
bodyParser.text({ type: () => true })would be more effective. See https://github.com/zivl/sentry-testkit/blob/d5d0685e561f7bd3ed387c745365ef64593eb36b/src/localServerApi.ts#L23-L25ryan-cahill commentedon Apr 20, 2023
Thanks for the suggestion @rchl. That did seem to get the issue closer to being resolved, but there was still a missing piece that I didn't figure out. Seeing as I'm just evaluating the Session Replay feature, I didn't spend too much time continuing down that road. I found that the setting below solved the issue for now and Session Replay worked using the tunnel as expected. I found this from a similar issue in another repo
rchl commentedon Apr 20, 2023
As explained by @lforst in the linked issue:
express.textis essentiallybody-parserso same issue applies here.I would suggest trying
@nuxtjs/proxymodule instead of a custom server middleware.Something like this might work:
Of course the
[projectId]and[xxx]needs to be replaced with actual data from the DSN.(This setup is not as dynamic as the one suggested by Sentry where Sentry origin and project ID is parsed automatically from the request URL but it might be good enough.)