Skip to content

fix(nextjs): Let flush finish in API routes - #3811

Merged
lobsterkatie merged 11 commits into
masterfrom
kmclb-nextjs-let-flush-finish-in-api-routes
Jul 20, 2021
Merged

fix(nextjs): Let flush finish in API routes#3811
lobsterkatie merged 11 commits into
masterfrom
kmclb-nextjs-let-flush-finish-in-api-routes

Conversation

@lobsterkatie

@lobsterkatielobsterkatie commented Jul 16, 2021

Copy link
Copy Markdown
Member

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

github-actionsBot commented Jul 16, 2021

Copy link
Copy Markdown
Contributor

size-limit report

PathSize
@sentry/browser - CDN Bundle (gzipped)21.46 KB (-0.01% 🔽)
@sentry/browser - Webpack22.47 KB (0%)
@sentry/react - Webpack22.5 KB (0%)
@sentry/browser + @sentry/tracing - CDN Bundle (gzipped)28.97 KB (0%)

Comment threadpackages/nextjs/src/utils/handlers.ts Outdated
Comment threadpackages/nextjs/src/utils/handlers.ts Outdated
@kamilogorek

Copy link
Copy Markdown
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.

Comment threadpackages/nextjs/src/utils/handlers.ts Outdated
@lobsterkatie

Copy link
Copy Markdown
MemberAuthor

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
lobsterkatieforce-pushed the kmclb-nextjs-let-flush-finish-in-api-routes branch 3 times, most recently from db7b735 to d1f1824CompareJuly 16, 2021 22:15

@rhcarvalhorhcarvalho left a comment

Copy link
Copy Markdown
Contributor

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.

Comment threadpackages/nextjs/src/utils/handlers.ts Outdated
Comment threadpackages/nextjs/src/utils/handlers.ts
Comment threadpackages/nextjs/src/utils/handlers.ts Outdated
Comment threadpackages/hub/src/scope.ts Outdated
@lobsterkatie

Copy link
Copy Markdown
MemberAuthor

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
lobsterkatieforce-pushed the kmclb-nextjs-let-flush-finish-in-api-routes branch from 4657ca7 to 8edb6f6CompareJuly 20, 2021 07:05

@AbhiPrasadAbhiPrasad left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Nice!

@lobsterkatie
lobsterkatieforce-pushed the kmclb-nextjs-let-flush-finish-in-api-routes branch from 8edb6f6 to a4763f5CompareJuly 20, 2021 13:47
@lobsterkatie
lobsterkatie merged commit 75ae8c7 into masterJul 20, 2021
@lobsterkatie
lobsterkatie deleted the kmclb-nextjs-let-flush-finish-in-api-routes branch July 20, 2021 14:40
@maccman

Copy link
Copy Markdown

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 freeto join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

5 participants

@lobsterkatie@kamilogorek@maccman@rhcarvalho@AbhiPrasad