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): Let flush finish in API routes #3811

Merged
merged 11 commits into from
Jul 20, 2021

Conversation

lobsterkatie
Copy link
Member

@lobsterkatie lobsterkatie commented Jul 16, 2021

As discussed in more detail in the penultimate paragraph of this PR, when deployed to vercel, API route lambdas have been shutting down too soon for sentry events to get reliably sent to our servers (in spite of the use of flush). This fixes that by wrapping the response's .end() method (which is what triggers the lambda shutdown) such that it waits for flush to finish before emitting its finished signal.

Logs from API routes now consistently look like this:

[GET] /api/hello

Sentry Logger [Log]: [Tracing] Starting http.server transaction - GET /api/hello
Sentry Logger [Log]: [Tracing] Finishing http.server transaction - GET /api/hello
Sentry Logger [Log]: Flushing events...
Sentry Logger [Log]: Done flushing events

Fixes #3806
Fixes #3643
Fixes #3691
Fixes #3748
Fixes #3636

@github-actions
Copy link
Contributor

github-actions bot commented Jul 16, 2021

size-limit report

Path Size
@sentry/browser - CDN Bundle (gzipped) 21.46 KB (-0.01% 🔽)
@sentry/browser - Webpack 22.47 KB (0%)
@sentry/react - Webpack 22.5 KB (0%)
@sentry/browser + @sentry/tracing - CDN Bundle (gzipped) 28.97 KB (0%)

@kamilogorek
Copy link
Contributor

I went through the broken integration tests. They are all failing because of request.url (absolute instead of relative, but you have a note about it in addRequestDataToEvent). We can update tests if that's desired behavior as well.
Linter and unit tests are just overlooked attributes on hub/scope.

@lobsterkatie
Copy link
Member Author

I went through the broken integration tests.

Thanks!

They are all failing because of request.url (absolute instead of relative, but you have a note about it in addRequestDataToEvent). We can update tests if that's desired behavior as well.

Yup, I know that's the underlying problem. But even once I fixed the tests, one is still failing, for mysterious reasons. ¯\(ツ)/¯ I'm sure I'll figure it out. That and the linting stuff I just ran out of time to fix before I turned into a pumpkin last night.

@lobsterkatie lobsterkatie force-pushed the kmclb-nextjs-let-flush-finish-in-api-routes branch 3 times, most recently from db7b735 to d1f1824 Compare July 16, 2021 22:15
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.

Had a partial look because @AbhiPrasad asked :)
I'd beg to avoid adding more API surface to general packages.

I understand scope bleed, but that's a much larger topic, I wouldn't try to mix that and the flush fix in one PR.

packages/nextjs/src/utils/handlers.ts Outdated Show resolved Hide resolved
packages/nextjs/src/utils/handlers.ts Show resolved Hide resolved
packages/nextjs/src/utils/handlers.ts Outdated Show resolved Hide resolved
packages/hub/src/scope.ts Outdated Show resolved Hide resolved
@lobsterkatie
Copy link
Member Author

Recording the results of an IRL convo with @rhcarvalho, @AbhiPrasad, and @kamilogorek.

The scope issue is sufficiently complicated (and wide-reaching) that it will be pushed off to another PR. Here, we'll dial it back to just keeping track of the open transaction.

@lobsterkatie lobsterkatie force-pushed the kmclb-nextjs-let-flush-finish-in-api-routes branch from 4657ca7 to 8edb6f6 Compare July 20, 2021 07:05
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.

Nice!

@lobsterkatie lobsterkatie force-pushed the kmclb-nextjs-let-flush-finish-in-api-routes branch from 8edb6f6 to a4763f5 Compare July 20, 2021 13:47
@lobsterkatie lobsterkatie merged commit 75ae8c7 into master Jul 20, 2021
@lobsterkatie lobsterkatie deleted the kmclb-nextjs-let-flush-finish-in-api-routes branch July 20, 2021 14:40
@maccman
Copy link

maccman commented Sep 25, 2021

I don't quite understand this - Vercel/Next.js supports return promises.

Because of this monkey patching I'm getting:
API resolved without sending a response for /api/meetings?start_at=2021-09-25T04%3A00%3A00.000Z&end_at=2021-09-26T03%3A59%3A59.999Z, this may result in stalled requests.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
5 participants