-
Notifications
You must be signed in to change notification settings - Fork 3.5k
[NoQA] Add memory usage breadcrumbs and context to Sentry spans #81344
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
mjasikowski
merged 9 commits into
Expensify:main
from
callstack-internal:callstack-internal/szymonzalarski/sentry/track-memory-usage-in-sentry-attributes-fix
Feb 9, 2026
+859
−8
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
bfb74b4
Add memory usage breadcrumbs and context to Sentry spans
szymonzalarski98 f52a566
Lint and prettier fixes
szymonzalarski98 1063fe6
Fix for mobile memory usage
szymonzalarski98 cb7014d
Merge branch 'main' into callstack-internal/szymonzalarski/sentry/tra…
szymonzalarski98 d85297f
Fix for iOS
szymonzalarski98 5d10e37
PR fixes
szymonzalarski98 975f8e6
Android usage data fixes
szymonzalarski98 d3c8819
Prettier fix
szymonzalarski98 8db254a
Separate getMemoryLogLevel and formatMemoryBreadcrumb methods
szymonzalarski98 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
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
12 changes: 12 additions & 0 deletions
12
src/libs/telemetry/formatMemoryBreadcrumb/index.android.ts
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,12 @@ | ||
| import type {MemoryInfo} from '../getMemoryInfo/types'; | ||
|
|
||
| /** | ||
| * Format breadcrumb message for Android platform | ||
| * @param memoryInfo - Memory information object | ||
| * @param usedMemoryMB - Used memory in MB (from memoryInfo.usedMemoryMB) | ||
| * @returns Formatted breadcrumb message | ||
| */ | ||
| export default function formatMemoryBreadcrumb(memoryInfo: MemoryInfo, usedMemoryMB: number | null): string { | ||
| const usagePercent = memoryInfo.usagePercentage?.toFixed(0) ?? '?'; | ||
| return `RAM Check: ${usedMemoryMB ?? '?'}MB used (${usagePercent}% device RAM)`; | ||
| } | ||
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,11 @@ | ||
| import type {MemoryInfo} from '../getMemoryInfo/types'; | ||
|
|
||
| /** | ||
| * Format breadcrumb message for iOS platform | ||
| * @param memoryInfo - Memory information object | ||
| * @param usedMemoryMB - Used memory in MB (from memoryInfo.usedMemoryMB) | ||
| * @returns Formatted breadcrumb message | ||
| */ | ||
| export default function formatMemoryBreadcrumb(_memoryInfo: MemoryInfo, usedMemoryMB: number | null): string { | ||
| return `RAM Check: ${usedMemoryMB ?? '?'}MB used (iOS - no limit API)`; | ||
| } | ||
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,12 @@ | ||
| import type {MemoryInfo} from '../getMemoryInfo/types'; | ||
|
|
||
| /** | ||
| * Format breadcrumb message for web platform | ||
| * @param memoryInfo - Memory information object | ||
| * @param usedMemoryMB - Used memory in MB (from memoryInfo.usedMemoryMB) | ||
| * @returns Formatted breadcrumb message | ||
| */ | ||
| export default function formatMemoryBreadcrumb(memoryInfo: MemoryInfo, usedMemoryMB: number | null): string { | ||
| const maxMB = memoryInfo.maxMemoryBytes ? Math.round(memoryInfo.maxMemoryBytes / (1024 * 1024)) : '?'; | ||
| return `RAM Check: ${usedMemoryMB ?? '?'}MB / ${maxMB}MB limit`; | ||
| } | ||
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,67 @@ | ||
| import {Platform} from 'react-native'; | ||
| import DeviceInfo from 'react-native-device-info'; | ||
| import type {MemoryInfo} from './types'; | ||
|
|
||
| const BYTES_PER_MB = 1024 * 1024; | ||
|
|
||
| const normalizeMemoryValue = (value: number | null | undefined): number | null => { | ||
| if (value === null || value === undefined || value < 0) { | ||
| return null; | ||
| } | ||
| return value; | ||
| }; | ||
|
|
||
| const getMemoryInfo = async (): Promise<MemoryInfo> => { | ||
| try { | ||
| const totalMemoryBytesRaw = DeviceInfo.getTotalMemorySync?.() ?? null; | ||
| const totalMemoryBytes = normalizeMemoryValue(totalMemoryBytesRaw); | ||
|
|
||
| const [usedMemory, maxMemory] = await Promise.allSettled([DeviceInfo.getUsedMemory(), Platform.OS === 'android' ? DeviceInfo.getMaxMemory() : Promise.resolve(null)]); | ||
|
|
||
| const usedMemoryBytesRaw = usedMemory.status === 'fulfilled' ? usedMemory.value : null; | ||
| const usedMemoryBytes = normalizeMemoryValue(usedMemoryBytesRaw); | ||
|
|
||
| const maxMemoryBytesRaw = maxMemory.status === 'fulfilled' ? maxMemory.value : null; | ||
| const maxMemoryBytes = normalizeMemoryValue(maxMemoryBytesRaw); | ||
|
|
||
| // Calculate usage percentage based on appropriate metric per platform | ||
| // Android: Use % of device RAM (RSS / totalMemory) - temporary until native onTrimMemory module | ||
| // This is more adaptive than absolute MB and scales with device capabilities | ||
| // iOS: No calculation (use absolute MB thresholds) | ||
| let usagePercentage: number | null = null; | ||
| if (Platform.OS === 'android' && usedMemoryBytes !== null && totalMemoryBytes !== null && totalMemoryBytes > 0) { | ||
| usagePercentage = parseFloat(((usedMemoryBytes / totalMemoryBytes) * 100).toFixed(2)); | ||
| } | ||
|
|
||
| const freeMemoryBytes = totalMemoryBytes !== null && usedMemoryBytes !== null ? totalMemoryBytes - usedMemoryBytes : null; | ||
| const freeMemoryMB = freeMemoryBytes !== null ? Math.round(freeMemoryBytes / BYTES_PER_MB) : null; | ||
| const freeMemoryPercentage = | ||
| totalMemoryBytes !== null && usedMemoryBytes !== null && totalMemoryBytes > 0 ? parseFloat((((totalMemoryBytes - usedMemoryBytes) / totalMemoryBytes) * 100).toFixed(2)) : null; | ||
|
|
||
| return { | ||
| usedMemoryBytes, | ||
| usedMemoryMB: usedMemoryBytes !== null ? Math.round(usedMemoryBytes / BYTES_PER_MB) : null, | ||
| totalMemoryBytes, | ||
| maxMemoryBytes, | ||
| usagePercentage, | ||
| freeMemoryBytes, | ||
| freeMemoryMB, | ||
| freeMemoryPercentage, | ||
| platform: Platform.OS, | ||
| }; | ||
| } catch (error) { | ||
| return { | ||
| usedMemoryBytes: null, | ||
| usedMemoryMB: null, | ||
| totalMemoryBytes: null, | ||
| maxMemoryBytes: null, | ||
| usagePercentage: null, | ||
| freeMemoryBytes: null, | ||
| freeMemoryMB: null, | ||
| freeMemoryPercentage: null, | ||
| platform: Platform.OS, | ||
| }; | ||
| } | ||
| }; | ||
|
|
||
| export default getMemoryInfo; | ||
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,84 @@ | ||
| import {Platform} from 'react-native'; | ||
| import type {MemoryInfo} from './types'; | ||
|
|
||
| const BYTES_PER_MB = 1024 * 1024; | ||
|
|
||
| // Only works in Chrome/Edge (Chromium browsers) - navigator.deviceMemory and performance.memory are not available in Firefox/Safari | ||
| const getMemoryInfo = async (): Promise<MemoryInfo> => { | ||
| try { | ||
| let totalMemoryBytes: number | null = null; | ||
| let usedMemoryBytes: number | null = null; | ||
| let maxMemoryBytes: number | null = null; | ||
|
|
||
| if (typeof window === 'undefined') { | ||
| return { | ||
| usedMemoryBytes: null, | ||
| usedMemoryMB: null, | ||
| totalMemoryBytes: null, | ||
| maxMemoryBytes: null, | ||
| usagePercentage: null, | ||
| freeMemoryBytes: null, | ||
| freeMemoryMB: null, | ||
| freeMemoryPercentage: null, | ||
| platform: Platform.OS, | ||
| }; | ||
| } | ||
|
|
||
| // We prioritize this API as the source of truth for web memory measurements | ||
| if (window.performance) { | ||
| try { | ||
| const perfMemory = ( | ||
| window.performance as Performance & { | ||
| memory?: { | ||
| usedJSHeapSize?: number; | ||
| totalJSHeapSize?: number; | ||
| jsHeapSizeLimit?: number; | ||
| }; | ||
| } | ||
| ).memory; | ||
|
|
||
| if (perfMemory) { | ||
| if (perfMemory.jsHeapSizeLimit && perfMemory.jsHeapSizeLimit > 0) { | ||
| maxMemoryBytes = perfMemory.jsHeapSizeLimit; | ||
| } | ||
|
|
||
| if (perfMemory.totalJSHeapSize && perfMemory.totalJSHeapSize > 0) { | ||
| totalMemoryBytes = perfMemory.totalJSHeapSize; | ||
| } | ||
|
|
||
| if (perfMemory.usedJSHeapSize && perfMemory.usedJSHeapSize > 0) { | ||
| usedMemoryBytes = perfMemory.usedJSHeapSize; | ||
| } | ||
| } | ||
| } catch (error) { | ||
| // Gracefully degrade - these APIs are not available in Firefox/Safari | ||
| } | ||
| } | ||
|
|
||
| return { | ||
| usedMemoryBytes, | ||
| usedMemoryMB: usedMemoryBytes !== null ? Math.round(usedMemoryBytes / BYTES_PER_MB) : null, | ||
| totalMemoryBytes, | ||
| maxMemoryBytes, | ||
| usagePercentage: usedMemoryBytes !== null && totalMemoryBytes !== null && totalMemoryBytes > 0 ? parseFloat(((usedMemoryBytes / totalMemoryBytes) * 100).toFixed(2)) : null, | ||
| freeMemoryBytes: totalMemoryBytes !== null && usedMemoryBytes !== null ? totalMemoryBytes - usedMemoryBytes : null, | ||
| freeMemoryMB: totalMemoryBytes !== null && usedMemoryBytes !== null ? Math.round((totalMemoryBytes - usedMemoryBytes) / BYTES_PER_MB) : null, | ||
| freeMemoryPercentage: totalMemoryBytes !== null && usedMemoryBytes !== null ? parseFloat((((totalMemoryBytes - usedMemoryBytes) / totalMemoryBytes) * 100).toFixed(2)) : null, | ||
| platform: Platform.OS, | ||
| }; | ||
| } catch (error) { | ||
| return { | ||
| usedMemoryBytes: null, | ||
| usedMemoryMB: null, | ||
| totalMemoryBytes: null, | ||
| maxMemoryBytes: null, | ||
| usagePercentage: null, | ||
| freeMemoryBytes: null, | ||
| freeMemoryMB: null, | ||
| freeMemoryPercentage: null, | ||
| platform: Platform.OS, | ||
| }; | ||
| } | ||
| }; | ||
|
|
||
| export default getMemoryInfo; |
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,14 @@ | ||
| type MemoryInfo = { | ||
| usedMemoryBytes: number | null; | ||
| usedMemoryMB: number | null; | ||
| totalMemoryBytes: number | null; | ||
| maxMemoryBytes: number | null; | ||
| usagePercentage: number | null; | ||
| freeMemoryBytes: number | null; | ||
| freeMemoryMB: number | null; | ||
| freeMemoryPercentage: number | null; | ||
| platform: string; | ||
| }; | ||
|
|
||
| // eslint-disable-next-line import/prefer-default-export -- Single type export is intentional; more types may be added in the future | ||
| export type {MemoryInfo}; |
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,20 @@ | ||
| import type * as Sentry from '@sentry/react-native'; | ||
| import CONST from '@src/CONST'; | ||
| import type {MemoryInfo} from '../getMemoryInfo/types'; | ||
|
|
||
| /** | ||
| * Determine memory log level for Android platform based on device RAM percentage | ||
| * @param memoryInfo - Memory information object | ||
| * @returns Sentry severity level based on memory usage | ||
| */ | ||
| export default function getMemoryLogLevel(memoryInfo: MemoryInfo): Sentry.SeverityLevel { | ||
| if (memoryInfo.usagePercentage !== null) { | ||
| if (memoryInfo.usagePercentage > CONST.TELEMETRY.CONFIG.MEMORY_THRESHOLD_ANDROID_CRITICAL) { | ||
| return 'error'; | ||
| } | ||
| if (memoryInfo.usagePercentage > CONST.TELEMETRY.CONFIG.MEMORY_THRESHOLD_ANDROID_WARNING) { | ||
| return 'warning'; | ||
| } | ||
| } | ||
| return 'info'; | ||
| } | ||
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,20 @@ | ||
| import type * as Sentry from '@sentry/react-native'; | ||
| import CONST from '@src/CONST'; | ||
| import type {MemoryInfo} from '../getMemoryInfo/types'; | ||
|
|
||
| /** | ||
| * Determine memory log level for iOS platform based on absolute MB values | ||
| * @param memoryInfo - Memory information object | ||
| * @returns Sentry severity level based on memory usage | ||
| */ | ||
| export default function getMemoryLogLevel(memoryInfo: MemoryInfo): Sentry.SeverityLevel { | ||
| if (memoryInfo.usedMemoryMB !== null) { | ||
| if (memoryInfo.usedMemoryMB > CONST.TELEMETRY.CONFIG.MEMORY_THRESHOLD_IOS_CRITICAL_MB) { | ||
| return 'error'; | ||
| } | ||
| if (memoryInfo.usedMemoryMB > CONST.TELEMETRY.CONFIG.MEMORY_THRESHOLD_IOS_WARNING_MB) { | ||
| return 'warning'; | ||
| } | ||
| } | ||
| return 'info'; | ||
| } | ||
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,22 @@ | ||
| import type * as Sentry from '@sentry/react-native'; | ||
| import CONST from '@src/CONST'; | ||
| import type {MemoryInfo} from '../getMemoryInfo/types'; | ||
|
|
||
| /** | ||
| * Determine memory log level for web platform based on JS heap percentage | ||
| * @param memoryInfo - Memory information object | ||
| * @returns Sentry severity level based on memory usage | ||
| */ | ||
| export default function getMemoryLogLevel(memoryInfo: MemoryInfo): Sentry.SeverityLevel { | ||
| const usagePercent = memoryInfo.usedMemoryBytes !== null && memoryInfo.maxMemoryBytes !== null ? (memoryInfo.usedMemoryBytes / memoryInfo.maxMemoryBytes) * 100 : null; | ||
|
|
||
| if (usagePercent !== null) { | ||
| if (usagePercent > CONST.TELEMETRY.CONFIG.MEMORY_THRESHOLD_WEB_CRITICAL) { | ||
| return 'error'; | ||
| } | ||
| if (usagePercent > CONST.TELEMETRY.CONFIG.MEMORY_THRESHOLD_WEB_WARNING) { | ||
| return 'warning'; | ||
| } | ||
| } | ||
| return 'info'; | ||
| } | ||
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.