-
-
Notifications
You must be signed in to change notification settings - Fork 358
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
Merged
Merged
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
3bb6c56
Expo Router improvement: Prefetch route performance measurement with โฆ
alwx 3aeecae
Performance measurement for manual expo router prefetch() call
alwx 9999084
Changelog entry with the usage example
alwx a2f6042
Fix
alwx e15607b
Merge branch 'main' into alwx/feature/prefetch-tracking
alwx bb228b2
`sentry.origin` added, tests adjusted
alwx 8fd95b7
File renamed
alwx 534c62c
Test fixes
alwx e8033fa
Merge branch 'main' into alwx/feature/prefetch-tracking
alwx c194149
Lint fix
alwx File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,90 @@ | ||
| import { SPAN_STATUS_ERROR, SPAN_STATUS_OK, startInactiveSpan } from '@sentry/core'; | ||
| import { SPAN_ORIGIN_AUTO_EXPO_ROUTER_PREFETCH } from './origin'; | ||
|
|
||
| /** | ||
| * 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: { | ||
| 'sentry.origin': SPAN_ORIGIN_AUTO_EXPO_ROUTER_PREFETCH, | ||
| 'route.href': typeof href === 'string' ? href : JSON.stringify(href), | ||
| 'route.name': routeName, | ||
| }, | ||
| }); | ||
|
|
||
| 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; | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.