Commit b14edae
authored
chore: add advanced charts traces cp-7.76.0 (#29497)
## **Description**
Adds Sentry performance traces for token-overview advanced chart
visibility (time from series change until skeleton clears), with
separate trace types for dashboards to distinguish initial load / asset
or currency changes from time-range-only updates.
Also fixes a bug in metrics consent detection during early Sentry
initialization.
## Changes
### Advanced Chart Performance Tracing
**AdvancedChart component:**
- Added optional `onSkeletonHidden` callback that fires once when
loading/layout settles and the chart is ready
- Guarded by refs to reset when series key or HTML content changes
**PriceAdvanced component:**
- Starts `trace()` when `ohlcvSeriesKey` changes
- Ends trace via `endTrace()` when:
- Skeleton hides (chart ready)
- Chart error occurs
- Trace is superseded by a newer series
- Falls back to legacy chart
- `getAdvancedChartVisibilityTraceRequest()` selects appropriate
`TraceName` and `TraceOperation` based on what changed:
- Same asset + currency with only range change →
`TokenOverviewAdvancedChartTimeRangeVisible` trace
- Otherwise → `TokenOverviewAdvancedChartInitialVisible` trace
**trace.ts:**
- Added new `TraceName` values:
- `TokenOverviewAdvancedChartInitialVisible` - for initial load or
asset/currency changes
- `TokenOverviewAdvancedChartTimeRangeVisible` - for time range selector
changes only
- Added new `TraceOperation` values:
- `token_overview.advanced_chart` - initial load or asset/currency
change
- `token_overview.advanced_chart_time_range` - time range change only
### Metrics Consent Fix
**Problem:**
The original implementation attempted to read from
`analytics.isEnabled()` during Sentry initialization in `index.js:45`.
However, this occurs before Engine and Redux are initialized, causing
`analytics.isEnabled()` to incorrectly return `false` even for opted-in
users (it checks in-memory state that doesn't exist yet).
**Solution:**
`hasMetricsConsent()` now reads directly from AnalyticsController's
persisted state in FilesystemStorage (`persist:AnalyticsController`)
before falling back to the legacy `METRICS_OPT_IN` storage key. This
approach:
- Works before Engine/Redux initialization (FilesystemStorage is always
available)
- Reads the actual persisted opt-in value from disk (`{"optedIn":
true/false}`)
- Follows the same pattern as `ControllerStorage.getAllPersistedState()`
in `persistConfig/index.ts`
- Avoids dependency on deprecated `METRICS_OPT_IN` as the primary source
**MetaMetricsAndDataCollectionSection:**
- Calls `updateCachedConsent()` when user toggles analytics
participation
- Ensures in-memory consent cache stays synchronized with
AnalyticsController state
## **Changelog**
<!--
If this PR is not End-User-Facing and should not show up in the
CHANGELOG, you can choose to either:
1. Write `CHANGELOG entry: null`
2. Label with `no-changelog`
If this PR is End-User-Facing, please write a short User-Facing
description in the past tense like:
`CHANGELOG entry: Added a new tab for users to see their NFTs`
`CHANGELOG entry: Fixed a bug that was causing some NFTs to flicker`
(This helps the Release Engineer do their job more quickly and
accurately)
-->
CHANGELOG entry: Adds tracing for advanced charts
## **Related issues**
Fixes: https://consensyssoftware.atlassian.net/browse/ASSETS-3129
## **Manual testing steps**
```gherkin
Feature: Advanced Chart Performance Tracing
Scenario: user opens token details and chart loads successfully
Given the user is on the home screen
When user taps on a token to open token details
Then a single "Starting sampled root span" log appears for "Token Overview Advanced Chart Initial Visible"
And a single "Finishing" log appears for the same span ID
And no duplicate "Token Overview" traces appear
Scenario: user navigates back before chart finishes loading
Given the user is on token details with the chart still loading (skeleton visible)
When user quickly navigates back to the home screen
Then a "Finishing" log appears for the "Token Overview Advanced Chart Initial Visible" span
And the finish log appears after the navigation back (after "[UserInteraction]" log)
Scenario: user navigates back after chart has loaded
Given the user is on token details and the chart has fully loaded
When user navigates back to the home screen
Then no "Token Overview" trace logs appear during navigation back
Scenario: user changes time range on the chart
Given the user is on token details with the chart loaded on 1D range
When user taps on the 1W time range selector
Then a "Finishing" log appears for the previous "Token Overview Advanced Chart Initial Visible" span
And a new "Starting sampled root span" log appears for "Token Overview Advanced Chart Time Range Visible"
And a "Finishing" log appears for the new span when the chart reloads
```
```
How to verify locally:
1. Enable Sentry trace logging by filtering console output for "Tracing"
2. Navigate to a token's details screen
3. Confirm you see exactly ONE "Starting sampled root span" for
"Token Overview Advanced Chart Initial Visible" followed by ONE
"Finishing" log for the same span ID
4. Navigate back — confirm no additional trace start/finish for that span
5. Re-enter the token, change time range — confirm the old trace ends
with superseded and a new "Time Range Visible" trace starts and finishes
```
## **Screenshots/Recordings**
<!-- If applicable, add screenshots and/or recordings to visualize the
before and after of your change. -->
### **Before**
<!-- [screenshots/recordings] -->
### **After**
<!-- [screenshots/recordings] -->
## **Pre-merge author checklist**
<!--
Every checklist item must be consciously assessed before marking this PR
as
"Ready for review". A checked box means you deliberately considered that
responsibility, not that you literally performed every action listed.
Unchecked boxes are ambiguous: they are not an implicit "N/A" and they
are not
a silent "skip". See `docs/readme/ready-for-review.md` for the full
checklist
semantics.
-->
- [x] I've followed [MetaMask Contributor
Docs](https://github.com/MetaMask/contributor-docs) and [MetaMask Mobile
Coding
Standards](https://github.com/MetaMask/metamask-mobile/blob/main/.github/guidelines/CODING_GUIDELINES.md).
- [x] I've completed the PR template to the best of my ability
- [x] I've included tests if applicable
- [x] I've documented my code using [JSDoc](https://jsdoc.app/) format
if applicable
- [x] I've applied the right labels on the PR (see [labeling
guidelines](https://github.com/MetaMask/metamask-mobile/blob/main/.github/guidelines/LABELING_GUIDELINES.md)).
Not required for external contributors.
#### Performance checks (if applicable)
- [ ] I've tested on Android
- Ideally on a mid-range device; emulator is acceptable
- [ ] I've tested with a power user scenario
- Use these [power-user
SRPs](https://consensyssoftware.atlassian.net/wiki/spaces/TL1/pages/edit-v2/401401446401?draftShareId=9d77e1e1-4bdc-4be1-9ebb-ccd916988d93)
to import wallets with many accounts and tokens
- [ ] I've instrumented key operations with Sentry traces for production
performance metrics
- See [`trace()`](/app/util/trace.ts) for usage and
[`addToken`](/app/components/Views/AddAsset/components/AddCustomToken/AddCustomToken.tsx#L274)
for an example
For performance guidelines and tooling, see the [Performance
Guide](https://consensyssoftware.atlassian.net/wiki/spaces/TL1/pages/400085549067/Performance+Guide+for+Engineers).
## **Pre-merge reviewer checklist**
<!--
Reviewer checklist items follow the same semantics as the author
checklist: an
unchecked box is ambiguous, a checked box means the reviewer consciously
assessed that responsibility. See `docs/readme/ready-for-review.md`.
-->
- [x] I've manually tested the PR (e.g. pull and build branch, run the
app, test code being changed).
- [x] I confirm that this PR addresses all acceptance criteria described
in the ticket it closes and includes the necessary testing evidence such
as recordings and or screenshots.
<!-- CURSOR_SUMMARY -->
---
> [!NOTE]
> **Medium Risk**
> Adds new Sentry tracing lifecycle hooks around the token overview
advanced chart and changes how metrics consent is determined during
early app startup, which could affect whether performance data is
captured or suppressed.
>
> **Overview**
> Adds Sentry performance traces for token overview *advanced chart
visibility*, starting a trace on `ohlcvSeriesKey` changes and ending it
when the chart skeleton disappears, errors, is superseded by a newer
request, falls back to the legacy chart, or unmounts (including
`assetId` and truncated error details).
>
> Extends `AdvancedChart` with an `onSkeletonHidden` callback that fires
once when the native skeleton overlay is removed (resetting on
series/HTML reload), and adds new `TraceName`/`TraceOperation` constants
to distinguish *initial/asset changes* vs *time-range-only* updates.
>
> Fixes early Sentry init consent detection by updating
`hasMetricsConsent()` to read `persist:AnalyticsController` from
`redux-persist-filesystem-storage` (fallback to legacy
`METRICS_OPT_IN`), and keeps the in-memory consent cache in sync via
`updateCachedConsent()` from settings; tests were updated/added for
these behaviors.
>
> <sup>Reviewed by [Cursor Bugbot](https://cursor.com/bugbot) for commit
7ec6bbd. Bugbot is set up for automated
code reviews on this repo. Configure
[here](https://www.cursor.com/dashboard/bugbot).</sup>
<!-- /CURSOR_SUMMARY -->1 parent 4863651 commit b14edae
8 files changed
Lines changed: 579 additions & 3 deletions
File tree
- app
- components
- UI
- AssetOverview/Price
- Charts/AdvancedChart
- __tests__
- Views/Settings/SecuritySettings/Sections/MetaMetricsAndDataCollectionSection
- util
Lines changed: 208 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
11 | 11 | | |
12 | 12 | | |
13 | 13 | | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
14 | 23 | | |
15 | 24 | | |
16 | 25 | | |
| |||
830 | 839 | | |
831 | 840 | | |
832 | 841 | | |
| 842 | + | |
| 843 | + | |
| 844 | + | |
| 845 | + | |
| 846 | + | |
| 847 | + | |
| 848 | + | |
| 849 | + | |
| 850 | + | |
| 851 | + | |
| 852 | + | |
| 853 | + | |
| 854 | + | |
| 855 | + | |
| 856 | + | |
| 857 | + | |
| 858 | + | |
| 859 | + | |
| 860 | + | |
| 861 | + | |
| 862 | + | |
| 863 | + | |
| 864 | + | |
| 865 | + | |
| 866 | + | |
| 867 | + | |
| 868 | + | |
| 869 | + | |
| 870 | + | |
| 871 | + | |
| 872 | + | |
| 873 | + | |
| 874 | + | |
| 875 | + | |
| 876 | + | |
| 877 | + | |
| 878 | + | |
| 879 | + | |
| 880 | + | |
| 881 | + | |
| 882 | + | |
| 883 | + | |
| 884 | + | |
| 885 | + | |
| 886 | + | |
| 887 | + | |
| 888 | + | |
| 889 | + | |
| 890 | + | |
| 891 | + | |
| 892 | + | |
| 893 | + | |
| 894 | + | |
| 895 | + | |
| 896 | + | |
| 897 | + | |
| 898 | + | |
| 899 | + | |
| 900 | + | |
| 901 | + | |
| 902 | + | |
| 903 | + | |
| 904 | + | |
| 905 | + | |
| 906 | + | |
| 907 | + | |
| 908 | + | |
| 909 | + | |
| 910 | + | |
| 911 | + | |
| 912 | + | |
| 913 | + | |
| 914 | + | |
| 915 | + | |
| 916 | + | |
| 917 | + | |
| 918 | + | |
| 919 | + | |
| 920 | + | |
| 921 | + | |
| 922 | + | |
| 923 | + | |
| 924 | + | |
| 925 | + | |
| 926 | + | |
| 927 | + | |
| 928 | + | |
| 929 | + | |
| 930 | + | |
| 931 | + | |
| 932 | + | |
| 933 | + | |
| 934 | + | |
| 935 | + | |
| 936 | + | |
| 937 | + | |
| 938 | + | |
| 939 | + | |
| 940 | + | |
| 941 | + | |
| 942 | + | |
| 943 | + | |
| 944 | + | |
| 945 | + | |
| 946 | + | |
| 947 | + | |
| 948 | + | |
| 949 | + | |
| 950 | + | |
| 951 | + | |
| 952 | + | |
| 953 | + | |
| 954 | + | |
| 955 | + | |
| 956 | + | |
| 957 | + | |
| 958 | + | |
| 959 | + | |
| 960 | + | |
| 961 | + | |
| 962 | + | |
| 963 | + | |
| 964 | + | |
| 965 | + | |
| 966 | + | |
| 967 | + | |
| 968 | + | |
| 969 | + | |
| 970 | + | |
| 971 | + | |
| 972 | + | |
| 973 | + | |
| 974 | + | |
| 975 | + | |
| 976 | + | |
| 977 | + | |
| 978 | + | |
| 979 | + | |
| 980 | + | |
| 981 | + | |
| 982 | + | |
| 983 | + | |
| 984 | + | |
| 985 | + | |
| 986 | + | |
| 987 | + | |
| 988 | + | |
| 989 | + | |
| 990 | + | |
| 991 | + | |
| 992 | + | |
| 993 | + | |
| 994 | + | |
| 995 | + | |
| 996 | + | |
| 997 | + | |
| 998 | + | |
| 999 | + | |
| 1000 | + | |
| 1001 | + | |
| 1002 | + | |
| 1003 | + | |
| 1004 | + | |
| 1005 | + | |
| 1006 | + | |
| 1007 | + | |
| 1008 | + | |
| 1009 | + | |
| 1010 | + | |
| 1011 | + | |
| 1012 | + | |
| 1013 | + | |
| 1014 | + | |
| 1015 | + | |
| 1016 | + | |
| 1017 | + | |
| 1018 | + | |
| 1019 | + | |
| 1020 | + | |
| 1021 | + | |
| 1022 | + | |
| 1023 | + | |
| 1024 | + | |
| 1025 | + | |
| 1026 | + | |
| 1027 | + | |
| 1028 | + | |
| 1029 | + | |
| 1030 | + | |
| 1031 | + | |
| 1032 | + | |
| 1033 | + | |
| 1034 | + | |
| 1035 | + | |
| 1036 | + | |
| 1037 | + | |
| 1038 | + | |
| 1039 | + | |
| 1040 | + | |
833 | 1041 | | |
0 commit comments