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

fix(nextjs): Disable server instrumentation on Vercel #4255

Merged
merged 7 commits into from
Dec 10, 2021

Conversation

iker-barriocanal
Copy link
Contributor

@iker-barriocanal iker-barriocanal commented Dec 10, 2021

This work has been done by @lobsterkatie.

Resolves #4207 and #4103.

On Vercel, the server instrumentation has no effect. Errors and transactions are captured through the custom _error.js page and the withSentry handler. Not instrumenting the server (to not import any modules from next) when an app is deployed on Vercel should fix the issue.

@iker-barriocanal iker-barriocanal requested review from lobsterkatie, a team and sl0thentr0py and removed request for a team December 10, 2021 10:23
@iker-barriocanal iker-barriocanal linked an issue Dec 10, 2021 that may be closed by this pull request
9 tasks
@github-actions
Copy link
Contributor

github-actions bot commented Dec 10, 2021

size-limit report

Path Base Size (24b600f) Current Size Change
@sentry/browser - CDN Bundle (gzipped) 22.45 KB 22.45 KB +0.01% 🔺
@sentry/browser - Webpack 23.29 KB 23.29 KB 0%
@sentry/react - Webpack 23.32 KB 23.32 KB 0%
@sentry/nextjs Client - Webpack 48.08 KB 48.08 KB 0%
@sentry/browser + @sentry/tracing - CDN Bundle (gzipped) 29.99 KB 29.98 KB -0.01% 🔽

Copy link
Contributor

@rhcarvalho rhcarvalho left a comment

Choose a reason for hiding this comment

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

I suggest renaming the PR to "Disable server instrumentation on Next 12", which seems to be closer to the intent.

I'm missing what of this current patch makes it specific to Next 12 vs disabling the instrumentation for any version.

(And missing tests)

packages/nextjs/src/index.server.ts Outdated Show resolved Hide resolved
packages/nextjs/src/index.server.ts Outdated Show resolved Hide resolved
packages/nextjs/src/index.server.ts Outdated Show resolved Hide resolved
// or
// `node /var/runtime/index.js`.
const isBuild = new RegExp('build').test(process.argv.toString());
const isVercel = !!process.env.VERCEL;
Copy link
Contributor

Choose a reason for hiding this comment

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

Shall we look for VERCEL=1 specifically? !!process.env.VERCEL means isVercel is true as long as the environment variable is defined (even for values that would be surprising to consider true, such as VERCEL=0, VERCEL=no and VERCEL=false).

https://vercel.com/docs/concepts/projects/environment-variables#system-environment-variables

Copy link
Contributor

Choose a reason for hiding this comment

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

Let's ignore this for now as it's been like this for sometime already and we can address it later.

// `node next start`
// or
// `node /var/runtime/index.js`.
const isBuild = new RegExp('build').test(process.argv.toString());
Copy link
Contributor

Choose a reason for hiding this comment

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

The pattern is a fixed string, there's no need to use a RegExp -- we could use String.indexOf or String.includes.

Also, process.argv.toString() seems brittle -- this could produce the incorrect output if "build" appears somewhere on the path / args by chance.

Suggested change
const isBuild = new RegExp('build').test(process.argv.toString());
const isBuild = process.argv.toString().indexOf('build') >= 0;

I could give a better suggestion once I understand what exactly was the intent here (are we trying to cover the first and second cases of the comment?).

The condition to check whether the `vercel` tag is added to vercel is resolved in `index.server.ts`
at the module level. The `VERCEL` env var should be set (or not) when the module is imported,
instead of importing it and then initing the app.
@iker-barriocanal iker-barriocanal changed the title fix(nextjs): Don't crash the server on Next.js v12 fix(nextjs): Disable server instrumentation on Vercel Dec 10, 2021
@iker-barriocanal
Copy link
Contributor Author

I'm missing what of this current patch makes it specific to Next 12 vs disabling the instrumentation for any version.

Nothing, it disables server instrumentation on every version. It's relevant to Next 12 because it's the first version that crashes with that instrumentation.

});
// TODO: test `vercel` tag when running on Vercel
// Can't just add the test and set env variables, since the value in `index.server.ts`
// is resolved when importing.
Copy link
Contributor

@rhcarvalho rhcarvalho Dec 10, 2021

Choose a reason for hiding this comment

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

We can patch the value of isVercel (doesn't have to be now)

…om:getsentry/sentry-javascript into kmclb-nextjs-no-instrumentserver-on-vercel
@iker-barriocanal iker-barriocanal merged commit 7e7e5f2 into master Dec 10, 2021
@iker-barriocanal iker-barriocanal deleted the kmclb-nextjs-no-instrumentserver-on-vercel branch December 10, 2021 13:03
onurtemizkan pushed a commit that referenced this pull request Dec 19, 2021
On Vercel, the server instrumentation has no effect. Errors and transactions are captured through the [custom `_error.js` page](https://docs.sentry.io/platforms/javascript/guides/nextjs/manual-setup/#create-a-custom-_error-page) and the [`withSentry` handler](https://docs.sentry.io/platforms/javascript/guides/nextjs/#configure). Not instrumenting the server (to not import any modules from `next`) when an app is deployed on Vercel should fix the issue.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
4 participants