Skip to content

Fix telemetry span duration for request spans - #70908

Closed
lforst wants to merge 3 commits into
vercel:canaryfrom
lforst:lforst-fix-request-span-duration
Closed

lforst wants to merge 3 commits into
vercel:canaryfrom
lforst:lforst-fix-request-span-duration

Conversation

@lforst

@lforst lforst commented Oct 7, 2024

Copy link
Copy Markdown
Contributor

What?

Fixes a bug where telemetry spans for Pages Router requests are way too short.

Why?

The handleRequestImpl function is wrapped with tracer.trace() to trace the duration of Next.js handling a request. The tracer.trace() function immediately starts a span and ends it when the return value of the passed callback resolves.

In the pages router handleRequestImpl more or less immediately resolves, even though the request is still being processed. The started span is immediately ended, not waiting for res.end(), which should be the actual end timestamp of the span.

How?

I adjusted the tracer.trace() function to take an optional manualSpanEnd option, which will make the tracer.trace() function not end the span when the promise resolves. Additionally, we now manually end the request span with the onClose hook.

Relates to #64723

@ijjk ijjk added the type: next label Oct 7, 2024
@ijjk

ijjk commented Oct 7, 2024 •

Copy link
Copy Markdown
Member

Allow CI Workflow Run

  • approve CI run for commit: 8a6f322

Note: this should only be enabled once the PR is ready to go and can only be enabled by a maintainer

(span) => {
if (span) {
const res = args[1]
res.end = new Proxy(res.end, {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Is there no res.on('end', ...)?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Correct me if I am wrong, but res.on('end') will not work on Vercel because the underlying lambdas freeze on res.end() which happens before res.on('end').

If that is not the case anymore it would be amazing because it would simplify the code here a lot.

lforst pushed a commit to getsentry/sentry-javascript that referenced this pull request Oct 8, 2024
…13904)

Drop all spans/transactions emitted by Next.js for pages router API
routes because they are currently super buggy with wrong timestamps. Fix
pending: vercel/next.js#70908

This is just a bit of prep-work so we can at some point disable the
logic where we turn on all Next.js spans and still have high quality
data.
@lforst lforst closed this Jul 17, 2025
@github-actions github-actions Bot added the locked label Aug 1, 2025
@github-actions github-actions Bot locked as resolved and limited conversation to collaborators Aug 1, 2025
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants