-
-
Notifications
You must be signed in to change notification settings - Fork 360
Auto tracing for expo-image and expo-asset #5718
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 7 commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
2b27409
Wrappers + auto tracing for expo-image and expo-assets
alwx 47436b8
Changelog entry
alwx 56eac67
Merge branch 'main' into alex/feature/expo-asset
alwx 033ca74
Fixes
alwx c5fedca
Try catch fixes
alwx 687393c
Moving traceAsyncOperation to a separate function
alwx d8020a6
removing Span from there
alwx 19a624f
smallish fix + an AI generated test
alwx d916fe8
lint fix
alwx f735b4e
Fix for position of the entry in CHANGELOG.md
alwx 0e0a0f1
Fixes
alwx cd229f0
Merge branch 'main' into alex/feature/expo-asset
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
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,99 @@ | ||
| import { SPAN_ORIGIN_AUTO_RESOURCE_EXPO_ASSET } from './origin'; | ||
| import { describeUrl, traceAsyncOperation } from './utils'; | ||
|
|
||
| /** | ||
| * Internal interface for expo-asset's Asset instance. | ||
| * We define this to avoid a hard dependency on expo-asset. | ||
| */ | ||
| export interface ExpoAssetInstance { | ||
| name: string; | ||
| type: string; | ||
| hash: string | null; | ||
| uri: string; | ||
| localUri: string | null; | ||
| width: number | null; | ||
| height: number | null; | ||
| downloaded: boolean; | ||
| downloadAsync(): Promise<ExpoAssetInstance>; | ||
| } | ||
|
|
||
| /** | ||
| * Represents the expo-asset `Asset` class with its static methods. | ||
| * We only describe the methods that we instrument. | ||
| */ | ||
| export interface ExpoAsset { | ||
| loadAsync(moduleId: number | number[] | string | string[]): Promise<ExpoAssetInstance[]>; | ||
| fromModule(virtualAssetModule: number | string): ExpoAssetInstance; | ||
| } | ||
|
|
||
| /** | ||
| * Wraps expo-asset's `Asset` class to add automated performance monitoring. | ||
| * | ||
| * This function instruments `Asset.loadAsync` static method | ||
| * to create performance spans that measure how long asset loading takes. | ||
| * | ||
| * @param assetClass - The `Asset` class from `expo-asset` | ||
| * @returns The same class with instrumented static methods | ||
| * | ||
| * @example | ||
| * ```typescript | ||
| * import { Asset } from 'expo-asset'; | ||
| * import * as Sentry from '@sentry/react-native'; | ||
| * | ||
| * Sentry.wrapExpoAsset(Asset); | ||
| * ``` | ||
| */ | ||
| export function wrapExpoAsset<T extends ExpoAsset>(assetClass: T): T { | ||
| if (!assetClass) { | ||
| return assetClass; | ||
| } | ||
|
|
||
| if ((assetClass as T & { __sentryWrapped?: boolean }).__sentryWrapped) { | ||
| return assetClass; | ||
| } | ||
|
|
||
| wrapLoadAsync(assetClass); | ||
|
|
||
| (assetClass as T & { __sentryWrapped?: boolean }).__sentryWrapped = true; | ||
|
|
||
| return assetClass; | ||
| } | ||
|
|
||
| function wrapLoadAsync<T extends ExpoAsset>(assetClass: T): void { | ||
| if (!assetClass.loadAsync) { | ||
| return; | ||
| } | ||
|
|
||
| const originalLoadAsync = assetClass.loadAsync.bind(assetClass); | ||
|
|
||
| assetClass.loadAsync = ((moduleId: number | number[] | string | string[]): Promise<ExpoAssetInstance[]> => { | ||
| const moduleIds = Array.isArray(moduleId) ? moduleId : [moduleId]; | ||
| const assetCount = moduleIds.length; | ||
| const description = describeModuleIds(moduleIds); | ||
|
|
||
| return traceAsyncOperation( | ||
| { | ||
| op: 'resource.asset', | ||
| name: `Asset load ${description}`, | ||
| attributes: { | ||
| 'sentry.origin': SPAN_ORIGIN_AUTO_RESOURCE_EXPO_ASSET, | ||
| 'asset.count': assetCount, | ||
| }, | ||
| }, | ||
| () => originalLoadAsync(moduleId), | ||
| ); | ||
| }) as T['loadAsync']; | ||
| } | ||
|
|
||
| function describeModuleIds(moduleIds: (number | string)[]): string { | ||
| if (moduleIds.length === 1) { | ||
| const id = moduleIds[0]; | ||
| if (typeof id === 'string') { | ||
| return describeUrl(id); | ||
| } | ||
| return `asset #${id}`; | ||
| } | ||
| return `${moduleIds.length} assets`; | ||
| } | ||
|
|
||
|
|
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,174 @@ | ||
| import { SPAN_STATUS_ERROR, SPAN_STATUS_OK, startInactiveSpan } from '@sentry/core'; | ||
| import { SPAN_ORIGIN_AUTO_RESOURCE_EXPO_IMAGE } from './origin'; | ||
| import { describeUrl, traceAsyncOperation } from './utils'; | ||
|
|
||
| /** | ||
| * Internal interface for expo-image's ImageSource. | ||
| * We define this to avoid a hard dependency on expo-image. | ||
| */ | ||
| interface ExpoImageSource { | ||
| uri?: string; | ||
| headers?: Record<string, string>; | ||
| width?: number | null; | ||
| height?: number | null; | ||
| cacheKey?: string; | ||
| } | ||
|
|
||
| /** | ||
| * Internal interface for expo-image's ImageLoadOptions. | ||
| * We define this to avoid a hard dependency on expo-image. | ||
| */ | ||
| interface ExpoImageLoadOptions { | ||
| maxWidth?: number; | ||
| maxHeight?: number; | ||
| onError?(error: Error, retry: () => void): void; | ||
| } | ||
|
|
||
| /** | ||
| * Internal interface for expo-image's ImageRef. | ||
| * We define this to avoid a hard dependency on expo-image. | ||
| */ | ||
| interface ExpoImageRef { | ||
| readonly width: number; | ||
| readonly height: number; | ||
| readonly scale: number; | ||
| readonly mediaType: string | null; | ||
| readonly isAnimated?: boolean; | ||
| } | ||
|
|
||
| /** | ||
| * Represents the expo-image `Image` class with its static methods. | ||
| * We only describe the methods that we instrument. | ||
| */ | ||
| export interface ExpoImage { | ||
| // eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
| prefetch(urls: string | string[], cachePolicyOrOptions?: any): Promise<boolean>; | ||
| loadAsync(source: ExpoImageSource | string | number, options?: ExpoImageLoadOptions): Promise<ExpoImageRef>; | ||
| clearMemoryCache?(): Promise<boolean>; | ||
| clearDiskCache?(): Promise<boolean>; | ||
| } | ||
|
|
||
| /** | ||
| * Wraps expo-image's `Image` class to add automated performance monitoring. | ||
| * | ||
| * This function instruments `Image.prefetch` and `Image.loadAsync` static methods | ||
| * to create performance spans that measure how long image prefetching and loading take. | ||
| * | ||
| * @param imageClass - The `Image` class from `expo-image` | ||
| * @returns The same class with instrumented static methods | ||
| * | ||
| * @example | ||
| * ```typescript | ||
| * import { Image } from 'expo-image'; | ||
| * import * as Sentry from '@sentry/react-native'; | ||
| * | ||
| * Sentry.wrapExpoImage(Image); | ||
| * ``` | ||
| */ | ||
| export function wrapExpoImage<T extends ExpoImage>(imageClass: T): T { | ||
| if (!imageClass) { | ||
| return imageClass; | ||
| } | ||
|
|
||
| if ((imageClass as T & { __sentryWrapped?: boolean }).__sentryWrapped) { | ||
| return imageClass; | ||
| } | ||
|
|
||
| wrapPrefetch(imageClass); | ||
| wrapLoadAsync(imageClass); | ||
|
|
||
| (imageClass as T & { __sentryWrapped?: boolean }).__sentryWrapped = true; | ||
|
|
||
| return imageClass; | ||
| } | ||
|
|
||
| function wrapPrefetch<T extends ExpoImage>(imageClass: T): void { | ||
| if (!imageClass.prefetch) { | ||
| return; | ||
| } | ||
|
|
||
| const originalPrefetch = imageClass.prefetch.bind(imageClass); | ||
|
|
||
| // eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
| imageClass.prefetch = ((urls: string | string[], cachePolicyOrOptions?: any): Promise<boolean> => { | ||
| const urlList = Array.isArray(urls) ? urls : [urls]; | ||
| const urlCount = urlList.length; | ||
| const firstUrl = urlList[0] || 'unknown'; | ||
| const description = urlCount === 1 ? describeUrl(firstUrl) : `${urlCount} images`; | ||
|
|
||
| const span = startInactiveSpan({ | ||
| op: 'resource.image.prefetch', | ||
| name: `Image prefetch ${description}`, | ||
| attributes: { | ||
| 'sentry.origin': SPAN_ORIGIN_AUTO_RESOURCE_EXPO_IMAGE, | ||
| 'image.url_count': urlCount, | ||
| ...(urlCount === 1 ? { 'image.url': firstUrl } : undefined), | ||
|
alwx marked this conversation as resolved.
Outdated
|
||
| }, | ||
| }); | ||
|
|
||
| try { | ||
| return originalPrefetch(urls, cachePolicyOrOptions) | ||
| .then(result => { | ||
| if (result) { | ||
| span?.setStatus({ code: SPAN_STATUS_OK }); | ||
| } else { | ||
| span?.setStatus({ code: SPAN_STATUS_ERROR, message: 'prefetch_failed' }); | ||
| } | ||
| span?.end(); | ||
| return result; | ||
| }) | ||
| .catch((error: unknown) => { | ||
| span?.setStatus({ code: SPAN_STATUS_ERROR, message: String(error) }); | ||
| span?.end(); | ||
| throw error; | ||
| }); | ||
| } catch (error) { | ||
| span?.setStatus({ code: SPAN_STATUS_ERROR, message: String(error) }); | ||
| span?.end(); | ||
| throw error; | ||
| } | ||
| }) as T['prefetch']; | ||
| } | ||
|
|
||
| function wrapLoadAsync<T extends ExpoImage>(imageClass: T): void { | ||
| if (!imageClass.loadAsync) { | ||
| return; | ||
| } | ||
|
|
||
| const originalLoadAsync = imageClass.loadAsync.bind(imageClass); | ||
|
|
||
| imageClass.loadAsync = (( | ||
| source: ExpoImageSource | string | number, | ||
| options?: ExpoImageLoadOptions, | ||
| ): Promise<ExpoImageRef> => { | ||
| const description = describeSource(source); | ||
|
|
||
| const imageUrl = | ||
| typeof source === 'string' ? source : typeof source === 'object' && source.uri ? source.uri : undefined; | ||
|
|
||
| return traceAsyncOperation( | ||
| { | ||
| op: 'resource.image.load', | ||
| name: `Image load ${description}`, | ||
| attributes: { | ||
| 'sentry.origin': SPAN_ORIGIN_AUTO_RESOURCE_EXPO_IMAGE, | ||
| ...(imageUrl ? { 'image.url': imageUrl } : undefined), | ||
| }, | ||
| }, | ||
| () => originalLoadAsync(source, options), | ||
| ); | ||
| }) as T['loadAsync']; | ||
| } | ||
|
|
||
| function describeSource(source: ExpoImageSource | string | number): string { | ||
| if (typeof source === 'number') { | ||
| return `asset #${source}`; | ||
| } | ||
| if (typeof source === 'string') { | ||
| return describeUrl(source); | ||
| } | ||
| if (source.uri) { | ||
| return describeUrl(source.uri); | ||
| } | ||
| return 'unknown source'; | ||
| } | ||
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.