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

ref(nextjs): Improve app router routing instrumentation accuracy #13695

Merged
merged 9 commits into from
Sep 20, 2024

Conversation

lforst
Copy link
Member

@lforst lforst commented Sep 13, 2024

Improves the Next.js routing instrumentation by patching the Next.js router and instrumenting window popstates.

A few details on this PR that might explain weird-looking logic:

  • The patching of the router is in a setInterval because Next.js may take a while to write the router to the window object and we don't have a cue when that has happened.
  • We are using a combination of patching router.back/router.forward and the popstate event to emit a properly named transaction, because router.back and router.forward aren't passed any useful strings we could use as txn names.
  • Because there is a slight delay between the router.back/router.forward calls and the popstate event, we temporarily give the navigation span an invalid name that we use as an indicator to drop if one may leak through.

Copy link
Contributor

github-actions bot commented Sep 19, 2024

size-limit report 📦

Path Size % Change Change
@sentry/browser 22.52 KB - -
@sentry/browser - with treeshaking flags 21.3 KB - -
@sentry/browser (incl. Tracing) 34.8 KB - -
@sentry/browser (incl. Tracing, Replay) 71.26 KB - -
@sentry/browser (incl. Tracing, Replay) - with treeshaking flags 61.7 KB - -
@sentry/browser (incl. Tracing, Replay with Canvas) 75.61 KB - -
@sentry/browser (incl. Tracing, Replay, Feedback) 88.39 KB - -
@sentry/browser (incl. Tracing, Replay, Feedback, metrics) 90.23 KB - -
@sentry/browser (incl. metrics) 26.83 KB - -
@sentry/browser (incl. Feedback) 39.66 KB - -
@sentry/browser (incl. sendFeedback) 27.19 KB - -
@sentry/browser (incl. FeedbackAsync) 31.96 KB - -
@sentry/react 25.28 KB - -
@sentry/react (incl. Tracing) 37.77 KB - -
@sentry/vue 26.72 KB - -
@sentry/vue (incl. Tracing) 36.67 KB - -
@sentry/svelte 22.66 KB - -
CDN Bundle 23.83 KB - -
CDN Bundle (incl. Tracing) 36.56 KB - -
CDN Bundle (incl. Tracing, Replay) 71.02 KB - -
CDN Bundle (incl. Tracing, Replay, Feedback) 76.33 KB - -
CDN Bundle - uncompressed 69.81 KB - -
CDN Bundle (incl. Tracing) - uncompressed 108.44 KB - -
CDN Bundle (incl. Tracing, Replay) - uncompressed 220.21 KB - -
CDN Bundle (incl. Tracing, Replay, Feedback) - uncompressed 233.43 KB - -
@sentry/nextjs (client) 37.73 KB +0.54% +206 B 🔺
@sentry/sveltekit (client) 35.37 KB - -
@sentry/node 121.77 KB +0.01% +2 B 🔺
@sentry/node - without tracing 93.41 KB - -
@sentry/aws-serverless 103.11 KB - -

View base workflow run

@lforst lforst marked this pull request as ready for review September 19, 2024 15:44
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.

Looks good!

};
function transactionNameifyRouterArgument(target: string): string {
try {
return new URL(target, 'http://some-random-base.com/').pathname;
Copy link
Member

Choose a reason for hiding this comment

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

q: Why do we need that exactly? Couldn't we just take the target?

Copy link
Member Author

Choose a reason for hiding this comment

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

So users could be passing basically any string to router.push(), including absolute URLs to the current origin. Taking the absolute URLs wouldn't be good, so we somehow need to parse out the path. Parsing urls is really hard so this is the easiest/best way I found.

@lforst lforst merged commit 97974ba into develop Sep 20, 2024
142 checks passed
@lforst lforst deleted the lforst-better-app-router-routing-instrumentation branch September 20, 2024 07:52
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.

2 participants