|
| 1 | +import { SPAN_STATUS_ERROR, SPAN_STATUS_OK, startInactiveSpan } from '@sentry/core'; |
| 2 | +import { SPAN_ORIGIN_AUTO_RESOURCE_EXPO_IMAGE } from './origin'; |
| 3 | +import { describeUrl, sanitizeUrl, traceAsyncOperation } from './utils'; |
| 4 | + |
| 5 | +/** |
| 6 | + * Internal interface for expo-image's ImageSource. |
| 7 | + * We define this to avoid a hard dependency on expo-image. |
| 8 | + */ |
| 9 | +interface ExpoImageSource { |
| 10 | + uri?: string; |
| 11 | + headers?: Record<string, string>; |
| 12 | + width?: number | null; |
| 13 | + height?: number | null; |
| 14 | + cacheKey?: string; |
| 15 | +} |
| 16 | + |
| 17 | +/** |
| 18 | + * Internal interface for expo-image's ImageLoadOptions. |
| 19 | + * We define this to avoid a hard dependency on expo-image. |
| 20 | + */ |
| 21 | +interface ExpoImageLoadOptions { |
| 22 | + maxWidth?: number; |
| 23 | + maxHeight?: number; |
| 24 | + onError?(error: Error, retry: () => void): void; |
| 25 | +} |
| 26 | + |
| 27 | +/** |
| 28 | + * Internal interface for expo-image's ImageRef. |
| 29 | + * We define this to avoid a hard dependency on expo-image. |
| 30 | + */ |
| 31 | +interface ExpoImageRef { |
| 32 | + readonly width: number; |
| 33 | + readonly height: number; |
| 34 | + readonly scale: number; |
| 35 | + readonly mediaType: string | null; |
| 36 | + readonly isAnimated?: boolean; |
| 37 | +} |
| 38 | + |
| 39 | +/** |
| 40 | + * Represents the expo-image `Image` class with its static methods. |
| 41 | + * We only describe the methods that we instrument. |
| 42 | + */ |
| 43 | +export interface ExpoImage { |
| 44 | + // eslint-disable-next-line @typescript-eslint/no-explicit-any |
| 45 | + prefetch(urls: string | string[], cachePolicyOrOptions?: any): Promise<boolean>; |
| 46 | + loadAsync(source: ExpoImageSource | string | number, options?: ExpoImageLoadOptions): Promise<ExpoImageRef>; |
| 47 | + clearMemoryCache?(): Promise<boolean>; |
| 48 | + clearDiskCache?(): Promise<boolean>; |
| 49 | +} |
| 50 | + |
| 51 | +/** |
| 52 | + * Wraps expo-image's `Image` class to add automated performance monitoring. |
| 53 | + * |
| 54 | + * This function instruments `Image.prefetch` and `Image.loadAsync` static methods |
| 55 | + * to create performance spans that measure how long image prefetching and loading take. |
| 56 | + * |
| 57 | + * @param imageClass - The `Image` class from `expo-image` |
| 58 | + * @returns The same class with instrumented static methods |
| 59 | + * |
| 60 | + * @example |
| 61 | + * ```typescript |
| 62 | + * import { Image } from 'expo-image'; |
| 63 | + * import * as Sentry from '@sentry/react-native'; |
| 64 | + * |
| 65 | + * Sentry.wrapExpoImage(Image); |
| 66 | + * ``` |
| 67 | + */ |
| 68 | +export function wrapExpoImage<T extends ExpoImage>(imageClass: T): T { |
| 69 | + if (!imageClass) { |
| 70 | + return imageClass; |
| 71 | + } |
| 72 | + |
| 73 | + if ((imageClass as T & { __sentryWrapped?: boolean }).__sentryWrapped) { |
| 74 | + return imageClass; |
| 75 | + } |
| 76 | + |
| 77 | + wrapPrefetch(imageClass); |
| 78 | + wrapLoadAsync(imageClass); |
| 79 | + |
| 80 | + (imageClass as T & { __sentryWrapped?: boolean }).__sentryWrapped = true; |
| 81 | + |
| 82 | + return imageClass; |
| 83 | +} |
| 84 | + |
| 85 | +function wrapPrefetch<T extends ExpoImage>(imageClass: T): void { |
| 86 | + if (!imageClass.prefetch) { |
| 87 | + return; |
| 88 | + } |
| 89 | + |
| 90 | + const originalPrefetch = imageClass.prefetch.bind(imageClass); |
| 91 | + |
| 92 | + // eslint-disable-next-line @typescript-eslint/no-explicit-any |
| 93 | + imageClass.prefetch = ((urls: string | string[], cachePolicyOrOptions?: any): Promise<boolean> => { |
| 94 | + const urlList = Array.isArray(urls) ? urls : [urls]; |
| 95 | + const urlCount = urlList.length; |
| 96 | + const firstUrl = urlList[0] || 'unknown'; |
| 97 | + const description = urlCount === 1 ? describeUrl(firstUrl) : `${urlCount} images`; |
| 98 | + |
| 99 | + const span = startInactiveSpan({ |
| 100 | + op: 'resource.image.prefetch', |
| 101 | + name: `Image prefetch ${description}`, |
| 102 | + attributes: { |
| 103 | + 'sentry.origin': SPAN_ORIGIN_AUTO_RESOURCE_EXPO_IMAGE, |
| 104 | + 'image.url_count': urlCount, |
| 105 | + ...(urlCount === 1 ? { 'image.url': sanitizeUrl(firstUrl) } : undefined), |
| 106 | + }, |
| 107 | + }); |
| 108 | + |
| 109 | + try { |
| 110 | + return originalPrefetch(urls, cachePolicyOrOptions) |
| 111 | + .then(result => { |
| 112 | + if (result) { |
| 113 | + span?.setStatus({ code: SPAN_STATUS_OK }); |
| 114 | + } else { |
| 115 | + span?.setStatus({ code: SPAN_STATUS_ERROR, message: 'prefetch_failed' }); |
| 116 | + } |
| 117 | + span?.end(); |
| 118 | + return result; |
| 119 | + }) |
| 120 | + .catch((error: unknown) => { |
| 121 | + span?.setStatus({ code: SPAN_STATUS_ERROR, message: String(error) }); |
| 122 | + span?.end(); |
| 123 | + throw error; |
| 124 | + }); |
| 125 | + } catch (error) { |
| 126 | + span?.setStatus({ code: SPAN_STATUS_ERROR, message: String(error) }); |
| 127 | + span?.end(); |
| 128 | + throw error; |
| 129 | + } |
| 130 | + }) as T['prefetch']; |
| 131 | +} |
| 132 | + |
| 133 | +function wrapLoadAsync<T extends ExpoImage>(imageClass: T): void { |
| 134 | + if (!imageClass.loadAsync) { |
| 135 | + return; |
| 136 | + } |
| 137 | + |
| 138 | + const originalLoadAsync = imageClass.loadAsync.bind(imageClass); |
| 139 | + |
| 140 | + imageClass.loadAsync = (( |
| 141 | + source: ExpoImageSource | string | number, |
| 142 | + options?: ExpoImageLoadOptions, |
| 143 | + ): Promise<ExpoImageRef> => { |
| 144 | + const description = describeSource(source); |
| 145 | + |
| 146 | + const imageUrl = |
| 147 | + typeof source === 'string' ? source : typeof source === 'object' && source.uri ? source.uri : undefined; |
| 148 | + |
| 149 | + return traceAsyncOperation( |
| 150 | + { |
| 151 | + op: 'resource.image.load', |
| 152 | + name: `Image load ${description}`, |
| 153 | + attributes: { |
| 154 | + 'sentry.origin': SPAN_ORIGIN_AUTO_RESOURCE_EXPO_IMAGE, |
| 155 | + ...(imageUrl ? { 'image.url': sanitizeUrl(imageUrl) } : undefined), |
| 156 | + }, |
| 157 | + }, |
| 158 | + () => originalLoadAsync(source, options), |
| 159 | + ); |
| 160 | + }) as T['loadAsync']; |
| 161 | +} |
| 162 | + |
| 163 | +function describeSource(source: ExpoImageSource | string | number): string { |
| 164 | + if (typeof source === 'number') { |
| 165 | + return `asset #${source}`; |
| 166 | + } |
| 167 | + if (typeof source === 'string') { |
| 168 | + return describeUrl(source); |
| 169 | + } |
| 170 | + if (source.uri) { |
| 171 | + return describeUrl(source.uri); |
| 172 | + } |
| 173 | + return 'unknown source'; |
| 174 | +} |
0 commit comments