-
-
Notifications
You must be signed in to change notification settings - Fork 357
Expo Router integration improvement: Prefetch route performance measurement with automatically created spans #5606
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
base: main
Are you sure you want to change the base?
Changes from all commits
3bb6c56
3aeecae
9999084
a2f6042
e15607b
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,88 @@ | ||
| import { SPAN_STATUS_ERROR, SPAN_STATUS_OK, startInactiveSpan } from '@sentry/core'; | ||
|
|
||
| /** | ||
| * Type definition for Expo Router's router object | ||
| */ | ||
| export interface ExpoRouter { | ||
| prefetch?: (href: string | { pathname?: string; params?: Record<string, unknown> }) => void | Promise<void>; | ||
| // Other router methods can be added here if needed | ||
| push?: (...args: unknown[]) => void; | ||
| replace?: (...args: unknown[]) => void; | ||
| back?: () => void; | ||
| navigate?: (...args: unknown[]) => void; | ||
| } | ||
|
|
||
| /** | ||
| * Wraps Expo Router. It currently only does one thing: extends prefetch() method | ||
| * to add automated performance monitoring. | ||
| * | ||
| * This function instruments the `prefetch` method of an Expo Router instance | ||
| * to create performance spans that measure how long route prefetching takes. | ||
| * | ||
| * @param router - The Expo Router instance from `useRouter()` hook | ||
| * @returns The same router instance with an instrumented prefetch method | ||
| */ | ||
| export function wrapExpoRouter<T extends ExpoRouter>(router: T): T { | ||
| if (!router?.prefetch) { | ||
| return router; | ||
| } | ||
|
|
||
| // Check if already wrapped to avoid double-wrapping | ||
| if ((router as T & { __sentryPrefetchWrapped?: boolean }).__sentryPrefetchWrapped) { | ||
| return router; | ||
| } | ||
|
|
||
| const originalPrefetch = router.prefetch.bind(router); | ||
|
|
||
| router.prefetch = ((href: Parameters<NonNullable<ExpoRouter['prefetch']>>[0]) => { | ||
| // Extract route name from href for better span naming | ||
| let routeName = 'unknown'; | ||
| if (typeof href === 'string') { | ||
| routeName = href; | ||
| } else if (href && typeof href === 'object' && 'pathname' in href && href.pathname) { | ||
| routeName = href.pathname; | ||
| } | ||
|
|
||
| const span = startInactiveSpan({ | ||
| op: 'navigation.prefetch', | ||
| name: `Prefetch ${routeName}`, | ||
| attributes: { | ||
| 'route.href': typeof href === 'string' ? href : JSON.stringify(href), | ||
| 'route.name': routeName, | ||
| }, | ||
| }); | ||
|
Comment on lines
+46
to
+53
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Bug: The Suggested FixIn Prompt for AI AgentDid we get this right? ๐ / ๐ to inform future reviews.
Comment on lines
+46
to
+53
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Bug: The Suggested FixAdd the Prompt for AI Agent |
||
|
|
||
| try { | ||
| const result = originalPrefetch(href); | ||
|
|
||
| // Handle both promise and synchronous returns | ||
| if (result && typeof result === 'object' && 'then' in result && typeof result.then === 'function') { | ||
| return result | ||
| .then(res => { | ||
| span?.setStatus({ code: SPAN_STATUS_OK }); | ||
| span?.end(); | ||
| return res; | ||
| }) | ||
| .catch((error: unknown) => { | ||
| span?.setStatus({ code: SPAN_STATUS_ERROR, message: String(error) }); | ||
| span?.end(); | ||
| throw error; | ||
| }); | ||
| } else { | ||
| // Synchronous completion | ||
| span?.setStatus({ code: SPAN_STATUS_OK }); | ||
| span?.end(); | ||
| return result; | ||
| } | ||
| } catch (error) { | ||
| span?.setStatus({ code: SPAN_STATUS_ERROR, message: String(error) }); | ||
| span?.end(); | ||
| throw error; | ||
| } | ||
| }) as NonNullable<T['prefetch']>; | ||
|
|
||
| // Mark as wrapped to prevent double-wrapping | ||
| (router as T & { __sentryPrefetchWrapped?: boolean }).__sentryPrefetchWrapped = true; | ||
|
|
||
| return router; | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
q: should we also add the 'origin'?