Skip to content

Conversation

@logaretm
Copy link
Collaborator

URLs were missing from server-side transaction events (server components, generation functions) in Next.js. This was previously removed in #18113 because we tried to synchronously access params and searchParams, which cause builds to crash.

This PR approach adds the URL at runtime using a preprocessEvent hook as suggested.

Implementation

  1. Reads http.target (actual request path) and next.route (parameterized route) from the transaction's trace data
  2. Extracts headers from the captured isolation scope's SDK processing metadata
  3. Builds the full URL using the existing getSanitizedRequestUrl() utility
  4. Adds it to normalizedRequest.url so the requestDataIntegration includes it in the event

This works uniformly for both Webpack and Turbopack across all of our supported Next.js versions (13~16), I added missing tests for this case in the versions that did not have it.

Fixes #18115

@linear
Copy link

linear bot commented Nov 17, 2025

isolationScopeData.sdkProcessingMetadata.normalizedRequest =
isolationScopeData.sdkProcessingMetadata.normalizedRequest || {};
isolationScopeData.sdkProcessingMetadata.normalizedRequest.url = url;
}
Copy link

Choose a reason for hiding this comment

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

Bug: Transaction Event Request Data Uses Wrong Scope

The function modifies capturedSpanIsolationScope metadata, but the transaction event's request field is populated from capturedSpanScope. Modifications to the isolation scope won't affect the URL included in the event since requestDataIntegration reads from the same scope used to populate the initial request field. Should modify capturedSpanScope instead.

Fix in Cursor Fix in Web

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Data is merged from the trace data and the isolation scope, also tests pass so I don't think this is valid.

Comment on lines +24 to +33
const isolationScopeData = event.sdkProcessingMetadata?.capturedSpanIsolationScope?.getScopeData();
const headersDict = isolationScopeData?.sdkProcessingMetadata?.normalizedRequest?.headers;

const url = getSanitizedRequestUrl(componentRoute, undefined, headersDict, httpTarget?.toString());

// Add URL to the isolation scope's normalizedRequest so requestDataIntegration picks it up
if (url && isolationScopeData?.sdkProcessingMetadata) {
isolationScopeData.sdkProcessingMetadata.normalizedRequest =
isolationScopeData.sdkProcessingMetadata.normalizedRequest || {};
isolationScopeData.sdkProcessingMetadata.normalizedRequest.url = url;
Copy link

Choose a reason for hiding this comment

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

Bug: setUrlProcessingMetadata runs too late in the beforeSendEvent hook, after requestDataIntegration has processed the event, causing URLs to be missed.
Severity: CRITICAL | Confidence: 0.95

🔍 Detailed Analysis

The setUrlProcessingMetadata function executes in the beforeSendEvent hook, which occurs after requestDataIntegration.processEvent() has already run. Consequently, when setUrlProcessingMetadata attempts to modify isolationScopeData.sdkProcessingMetadata.normalizedRequest.url, requestDataIntegration has already processed the event and populated event.request, or failed to find the URL. This late modification means server-side transaction events will continue to lack URLs, rendering the intended feature non-functional.

💡 Suggested Fix

Move the URL addition logic to an earlier hook like preprocessEvent, or directly set event.request.url within the beforeSendEvent hook instead of modifying scope metadata.

🤖 Prompt for AI Agent
Review the code at the location below. A potential bug has been identified by an AI
agent.
Verify if this is a real issue. If it is, propose a fix; if not, explain why it's not
valid.

Location: packages/nextjs/src/common/utils/setUrlProcessingMetadata.ts#L24-L33

Potential issue: The `setUrlProcessingMetadata` function executes in the
`beforeSendEvent` hook, which occurs *after* `requestDataIntegration.processEvent()` has
already run. Consequently, when `setUrlProcessingMetadata` attempts to modify
`isolationScopeData.sdkProcessingMetadata.normalizedRequest.url`,
`requestDataIntegration` has already processed the event and populated `event.request`,
or failed to find the URL. This late modification means server-side transaction events
will continue to lack URLs, rendering the intended feature non-functional.

Did we get this right? 👍 / 👎 to inform future reviews.

Reference_id: 2725898

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Well tests pass with the fix and fail without it, so the timing looks correct.

@github-actions
Copy link
Contributor

github-actions bot commented Nov 17, 2025

size-limit report 📦

Path Size % Change Change
@sentry/browser 24.62 kB - -
@sentry/browser - with treeshaking flags 23.13 kB - -
@sentry/browser (incl. Tracing) 41.28 kB - -
@sentry/browser (incl. Tracing, Profiling) 45.61 kB - -
@sentry/browser (incl. Tracing, Replay) 79.73 kB - -
@sentry/browser (incl. Tracing, Replay) - with treeshaking flags 69.44 kB - -
@sentry/browser (incl. Tracing, Replay with Canvas) 84.43 kB - -
@sentry/browser (incl. Tracing, Replay, Feedback) 96.64 kB - -
@sentry/browser (incl. Feedback) 41.29 kB - -
@sentry/browser (incl. sendFeedback) 29.29 kB - -
@sentry/browser (incl. FeedbackAsync) 34.21 kB - -
@sentry/react 26.32 kB - -
@sentry/react (incl. Tracing) 43.22 kB - -
@sentry/vue 29.11 kB - -
@sentry/vue (incl. Tracing) 43.08 kB - -
@sentry/svelte 24.64 kB - -
CDN Bundle 26.94 kB - -
CDN Bundle (incl. Tracing) 41.83 kB - -
CDN Bundle (incl. Tracing, Replay) 78.38 kB - -
CDN Bundle (incl. Tracing, Replay, Feedback) 83.85 kB - -
CDN Bundle - uncompressed 78.92 kB - -
CDN Bundle (incl. Tracing) - uncompressed 124.07 kB - -
CDN Bundle (incl. Tracing, Replay) - uncompressed 240.1 kB - -
CDN Bundle (incl. Tracing, Replay, Feedback) - uncompressed 252.87 kB - -
@sentry/nextjs (client) 45.37 kB - -
@sentry/sveltekit (client) 41.67 kB - -
@sentry/node-core 50.91 kB - -
@sentry/node 159.09 kB - -
@sentry/node - without tracing 92.78 kB - -
@sentry/aws-serverless 106.53 kB - -

View base workflow run

@logaretm logaretm requested a review from chargome November 17, 2025 12:39
}
}

setUrlProcessingMetadata(event);
Copy link
Member

Choose a reason for hiding this comment

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

We could gate this already with sendDefaultPii

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Got it, I added a check for it inside the setUrlProcessingMetadata in 6089a70

@github-actions
Copy link
Contributor

node-overhead report 🧳

Note: This is a synthetic benchmark with a minimal express app and does not necessarily reflect the real-world performance impact in an application.

Scenario Requests/s % of Baseline Prev. Requests/s Change %
GET Baseline 8,994 - 9,158 -2%
GET With Sentry 1,360 15% 1,276 +7%
GET With Sentry (error only) 6,023 67% 5,987 +1%
POST Baseline 1,190 - 1,166 +2%
POST With Sentry 523 44% 502 +4%
POST With Sentry (error only) 1,068 90% 1,041 +3%
MYSQL Baseline 3,317 - 3,304 +0%
MYSQL With Sentry 497 15% 425 +17%
MYSQL With Sentry (error only) 2,729 82% 2,662 +3%

View base workflow run

@logaretm logaretm requested a review from chargome November 17, 2025 18:05
Copy link
Member

@chargome chargome left a comment

Choose a reason for hiding this comment

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

Great work! 🚀

@logaretm logaretm merged commit d729cdb into develop Nov 18, 2025
195 checks passed
@logaretm logaretm deleted the awad/js-1124-find-uniform-way-to-add-the-url-to-the-transaction-event branch November 18, 2025 09:40
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.

Find uniform way to add the url to the transaction event in Next.js

3 participants