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): Send events consistently on platforms that don't support streaming #6578

Merged
merged 4 commits into from
Jan 3, 2023

Conversation

lforst
Copy link
Member

@lforst lforst commented Dec 19, 2022

Resolves #6117

This PR ensures that the sending of events consistently works on platforms that don't support streaming. (i.e. Platforms where computation is impossible after a response has been sent.) For now, we're targeting AWS lambdas and Vercel with this.

API Routes

For API routes we're patching res.send, res.json, and res.end to block sending the response until events have been flushed. This comes with the big drawback that we're delaying responses but sending of events is unreliable after either res.send, res.json, or res.end has been called so it is a necessary evil.

Data Fetchers

For data fetchers, we face two problems. First, doing stuff after res.end is again very inconsistent. Secondly, we generally do not know what data fetchers are gonna be called for a particular request so apart from res.end we have no way of knowing when a request has been completed, and thus when we should finish a transaction.

The workaround we came up with for now is to create a transaction for the incoming request and to create individual transactions for each data fetcher that are then attached to the request transaction. That way we don't have to worry to send the transaction(s) in time, we just send them before the data fetcher should return.

This again comes with a drawback: We're sending a lot of transactions which probably will affect people's quota by a not insignificant amount.

@github-actions
Copy link
Contributor

size-limit report 📦

Path Size
@sentry/browser - ES5 CDN Bundle (gzipped + minified) 19.78 KB (-0.01% 🔽)
@sentry/browser - ES5 CDN Bundle (minified) 61.27 KB (0%)
@sentry/browser - ES6 CDN Bundle (gzipped + minified) 18.56 KB (+0.02% 🔺)
@sentry/browser - ES6 CDN Bundle (minified) 54.8 KB (0%)
@sentry/browser - Webpack (gzipped + minified) 20.34 KB (0%)
@sentry/browser - Webpack (minified) 66.48 KB (0%)
@sentry/react - Webpack (gzipped + minified) 20.36 KB (0%)
@sentry/nextjs Client - Webpack (gzipped + minified) 47.56 KB (0%)
@sentry/browser + @sentry/tracing - ES5 CDN Bundle (gzipped + minified) 26.76 KB (+0.02% 🔺)
@sentry/browser + @sentry/tracing - ES6 CDN Bundle (gzipped + minified) 25.19 KB (-0.01% 🔽)
@sentry/replay ES6 CDN Bundle (gzipped + minified) 42.08 KB (0%)
@sentry/replay - Webpack (gzipped + minified) 38.13 KB (0%)

@lforst lforst marked this pull request as ready for review December 19, 2022 13:08
@lforst lforst requested review from AbhiPrasad and Lms24 December 19, 2022 13:08
@lforst lforst marked this pull request as draft December 19, 2022 13:08
@lforst lforst marked this pull request as ready for review December 19, 2022 13:36
@lforst lforst requested review from AbhiPrasad and Lms24 December 19, 2022 13:37
Copy link
Member

@Lms24 Lms24 left a comment

Choose a reason for hiding this comment

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

So as discussed in person, the biggest concern here is an increase in response time of the serverless functions. I'd say we accept this for now in favour of consistency, especially given that theres precedence for this (axiom).
We can always revert, in case this becomes necessary.

@lforst lforst merged commit 371d42d into master Jan 3, 2023
@lforst lforst deleted the lforst-fix-vercel branch January 3, 2023 16:16
@kitsunde
Copy link

kitsunde commented Jan 4, 2023

So as discussed in person, the biggest concern here is an increase in response time of the serverless functions. I'd say we accept this for now in favour of consistency, especially given that theres precedence for this (axiom).

We can always revert, in case this becomes necessary.

Understand that this has been implemented already, but you should be able to subscribe to the shutdown signal when there is a lambda extension to be able to both send concurrently (because the lambda wakes up) during execution, and to drain the remainder on shutdown.

Since lambdas with external extensions gives 2s of shutdown time past serving the request, it might be possible to detect that in the lambda environment too even if sentry's own extension isn't available.

https://docs.aws.amazon.com/lambda/latest/dg/runtimes-extensions-api.html#runtimes-extensions-api-lifecycle

I use this method to batch requests to SQS and not pay the overhead on every call.

@Lms24
Copy link
Member

Lms24 commented Jan 4, 2023

@kitsunde thanks for your input. I'm wondering if we can use this on Vercel as my guess is that our influence on what to do with the Vercel-generated lambdas is rather limited (cc @lforst)

@kitsunde
Copy link

kitsunde commented Jan 4, 2023

@kitsunde thanks for your input. I'm wondering if we can use this on Vercel as my guess is that our influence on what to do with the Vercel-generated lambdas is rather limited (cc @lforst)

I re-read it and realised it seems like this only affects nextjs so not my concern now haha.

But you can register an event handler on the shutdown signal anywhere.

I guess the question now is if you would be able to tell if there's an extension registered so lambda gives you shutdown time, I would assume yes. Certainly if you involve your own in some capacity.

@lforst
Copy link
Member Author

lforst commented Jan 4, 2023

But you can register an event handler on the shutdown signal anywhere.

New to the extension API. Would you care to quickly explain how that would work?

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.

Errors aren't being reported in Next.js data-fetching methods on Vercel
3 participants