Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(nextjs): Add option to automatically tunnel events #6425

Merged
merged 17 commits into from
Dec 13, 2022

Conversation

lforst
Copy link
Member

@lforst lforst commented Dec 6, 2022

Adds the option tunnelRoute to the withSentryConfig() wrapper to automatically tunnel Sentry requests through the Next.js server via a configured route. This is a mechanism to prevent ad-blockers from blocking requests to Sentry.

We are leveraging Next.js rewrites option for tunneling which is essentially just a proxy configuration. It has the upside that when deployed on Vercel it is quite fast.

One thing we needed to take care of was that we're not creating spans/transactions for these proxied requests because they were extremely spammy.

Resolves: #5571

@lforst lforst changed the base branch from master to lforst-generalize-prefix-loader December 9, 2022 12:39
@github-actions
Copy link
Contributor

github-actions bot commented Dec 9, 2022

size-limit report 📦

Path Size
@sentry/browser - ES5 CDN Bundle (gzipped + minified) 19.71 KB (+0.01% 🔺)
@sentry/browser - ES5 CDN Bundle (minified) 61.11 KB (0%)
@sentry/browser - ES6 CDN Bundle (gzipped + minified) 18.5 KB (-0.01% 🔽)
@sentry/browser - ES6 CDN Bundle (minified) 54.64 KB (0%)
@sentry/browser - Webpack (gzipped + minified) 20.28 KB (0%)
@sentry/browser - Webpack (minified) 66.33 KB (0%)
@sentry/react - Webpack (gzipped + minified) 20.3 KB (0%)
@sentry/nextjs Client - Webpack (gzipped + minified) 47.5 KB (+0.42% 🔺)
@sentry/browser + @sentry/tracing - ES5 CDN Bundle (gzipped + minified) 26.7 KB (-0.02% 🔽)
@sentry/browser + @sentry/tracing - ES6 CDN Bundle (gzipped + minified) 25.15 KB (+0.01% 🔺)
@sentry/replay ES6 CDN Bundle (gzipped + minified) 41.73 KB (-0.01% 🔽)
@sentry/replay - Webpack (gzipped + minified) 38 KB (0%)

@lforst lforst marked this pull request as ready for review December 9, 2022 12:53
Copy link
Member

@AbhiPrasad AbhiPrasad left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we get some tests? In particular I would like to see shouldTraceRequest and the options.tunnel = tunnelPath logic tested.

We can prob extract the options.tunnel = tunnelPath into a util function and export it just to be unit tested.

__DEBUG_BUILD__ && logger.info(`Tunneling events to "${tunnelPath}"`);
options.tunnel = tunnelPath;
} else {
__DEBUG_BUILD__ && logger.warn('Provided DSN is not a Sentry SaaS DSN. Will not tunnel events.');
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

l: This should work with self-hosted as well right? Why indicate SaaS?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there a reason to tunnel if you're already on self-hosted? Requests to self-hosted instances are probably not being blocked by ad blockers.

Another reason we limit this is because of next.js rewrite restrictions. For example, the protocol needs to be static.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was going to ask the same thing. It's for sure less likely for someone's self-hosted instance to land on a block-list, but not impossible.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there a reason to tunnel if you're already on self-hosted

Good point, not sure actually, don't know enough to comment on this. @kamilogorek is tunneling a thing for self-hosted users?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's for sure less likely for someone's self-hosted instance to land on a block-list, but not impossible.

I believe if somebody's self-hosted instance is landing on a block-list, they're probably high-profile enough that their proxy route (via this option) will also land on a block list - rendering the option useless anyhow.

I personally would ship this without directly supporting self-hosted. And if we see demand for proxying to other hosts we can pretty easily add something like a tunnelTarget option that would allow for that. It should be perfectly compatible with this change.

I am not gonna go into detail here but there are a few things to consider when letting users choose the host. For example, we would need some special logic to tunnel requests to ingest.sentry.io because you cannot put that string into a tunnel request, since it would be blocked.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

They can also implement this logic themselves if they need to for the self-hosted case. Yeah that sounds reasonable enough for me.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe if somebody's self-hosted instance is landing on a block-list, they're probably high-profile enough that their proxy route (via this option) will also land on a block list - rendering the option useless anyhow.

I personally would ship this without directly supporting self-hosted. And if we see demand for proxying to other hosts we can pretty easily add something like a tunnelTarget option that would allow for that. It should be perfectly compatible with this change.

Yup, that's fair. Let's leave it as is.

Copy link
Member

@lobsterkatie lobsterkatie left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a nice addition!

packages/nextjs/src/config/types.ts Outdated Show resolved Hide resolved
packages/nextjs/src/config/types.ts Outdated Show resolved Hide resolved
packages/nextjs/src/config/withSentryConfig.ts Outdated Show resolved Hide resolved
packages/nextjs/src/config/withSentryConfig.ts Outdated Show resolved Hide resolved
packages/nextjs/src/config/withSentryConfig.ts Outdated Show resolved Hide resolved
packages/nextjs/src/config/withSentryConfig.ts Outdated Show resolved Hide resolved
packages/nextjs/src/config/withSentryConfig.ts Outdated Show resolved Hide resolved
__DEBUG_BUILD__ && logger.info(`Tunneling events to "${tunnelPath}"`);
options.tunnel = tunnelPath;
} else {
__DEBUG_BUILD__ && logger.warn('Provided DSN is not a Sentry SaaS DSN. Will not tunnel events.');
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was going to ask the same thing. It's for sure less likely for someone's self-hosted instance to land on a block-list, but not impossible.

packages/nextjs/src/utils/instrumentServer.ts Outdated Show resolved Hide resolved
Copy link
Member

@lobsterkatie lobsterkatie left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a nice addition!

(Got the mad rainbow unicorn, and now this comment is here twice. But I'm paranoid and don't want to delete it in case it's just a display bug and I end up deleting the original. Let's see if this edit shows up on both...)

Base automatically changed from lforst-generalize-prefix-loader to master December 12, 2022 13:03
Copy link
Member

@lobsterkatie lobsterkatie left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good! All of my remaining suggestions are just about comments, so all L.

packages/nextjs/src/config/withSentryConfig.ts Outdated Show resolved Hide resolved
packages/nextjs/src/config/withSentryConfig.ts Outdated Show resolved Hide resolved
packages/nextjs/src/config/withSentryConfig.ts Outdated Show resolved Hide resolved
packages/nextjs/test/utils/tunnelRoute.test.ts Outdated Show resolved Hide resolved
@lforst lforst merged commit 6e6e241 into master Dec 13, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Make @sentry/nextjs tunnel work via next.confg.js.rewrites
3 participants