A way to check that a build's diagnostic reporting works end to end, per
[mobile diagnostic
reporting](https://github.com/block/buzz/blob/main/docs/mobile-diagnostic-reporting.md).
A build with a reporting destination shows a Diagnostics card in
Settings with **Send test report** and **Test crash**. A build without
one, which includes every build from this repository alone, never shows
it and still reports nothing.
| Before | After |
| --- | --- |
| 
| 
|

### What's in it
- The reporter gains `reporting` (whether the build has a destination),
`sendTestReport()` and `crashForTest()`. The do-nothing reporter returns
false and does nothing.
- **Send test report** sends a fixed `DiagnosticTestError` tagged with a
new `testReport` operation. It passes through the same filter as every
report.
- **Test crash** asks for confirmation, then crashes in native code
through the SDK, so native crash reports can be checked too. The report
goes out the next time the app opens.
- The spec gains a "A way to check a build" decision.
### Regressions
None expected: builds without a destination render Settings exactly as
before.
### Validation
Added widget tests for the card being absent without reporting, sending
a test report, and the crash happening only once confirmed, plus a test
of what a sent test report contains and one that a second tap sends a
second report. The screenshots come from simulator builds of main and
this branch, both with the same placeholder destination.
A full-app simulator run at dec267f (placeholder destination) passed:
channel list, opening and scrolling a channel, search, the other
Settings sections, two test reports (a toast each time), cancelling Test
crash, confirming it (a native crash in the Sentry SDK) and relaunching
signed in.
---------
Signed-off-by: Tom Brow <tomb@block.xyz>
Co-authored-by: studio <dc2fc481343672df31a689840c09299972e704dac592e0da6f18486646a7ee3e@localhost>
16 KiB
Mobile diagnostic reporting
Status: step one agreed, not yet implemented.
Terminology is defined in CONTEXT.md. The decisions behind this spec are in ADR 0007 and ADR 0008.
Problem statement
When the mobile app crashes or misbehaves, nobody who can fix it finds out. Uncaught errors are dropped, failures are caught and shown as a generic message or nothing, and there is no record of how often the app's connections to its backends fail. A member who reports "it was slow" or "it crashed" cannot be helped without their phone in hand, and a backend problem that affects many members at once is invisible from the client side.
Solution
A build that is given a reporting destination does diagnostic reporting: it sends crashes, unexpected errors and networking measurements to that destination, tagged with the member who hit them. A build without a reporting destination sends nothing and behaves exactly as today. Reports never contain what people write or the names of the places they write it.
User stories
- As a developer, I want every crash of the app reported with a readable stack trace, so that I can fix crashes I never saw myself.
- As a developer, I want every uncaught Dart and Flutter error reported, so that errors the app currently drops become visible.
- As a developer, I want a report when the relay sends something the app cannot decode, so that protocol mismatches are found quickly.
- As a developer, I want a report when the relay rejects something the app sent as invalid, so that client bugs in what we send are found.
- As a developer, I want a report when a Builderlab or push gateway response cannot be understood, so that contract drift is caught.
- As a developer, I want a report when data saved on the device cannot be read back, so that storage corruption and migration bugs surface.
- As a developer, I want each report to carry the trail of recent networking events, so that I can see what the connection was doing before the fault.
- As a developer, I want each report to carry the member's public key, so that I can follow up with the person who hit it.
- As a developer, I want timeouts, disconnects, being offline, rate limits and refused sign-ins counted and not reported as errors, so that the error list contains only bugs someone can fix.
- As a developer, I want every network request and relay connection event counted with its backend, endpoint, outcome and kind of failure, so that I can see which backend and which operation is failing.
- As a developer, I want request durations measured per backend and endpoint, so that slowness shows up as well as failure.
- As a developer, I want those counts to carry the member, so that I can answer "who hit this failure, and when" without a separate log.
- As a person on call, I want to be alerted when the failure rate for a backend or endpoint spikes, so that an outage seen only by clients is noticed.
- As a distributor of an internal build, I want to turn reporting on by supplying one build-time setting, so that my organization's reports go to my own destination.
- As a distributor, I want stack traces from my release builds to be readable, so that reports are usable.
- As someone building the app from source, I want a build with no reporting destination to send nothing and start no reporting code, so that the open-source app reports to no one by default.
- As a member, I want reports to never include message content, channel or community names, private keys or other secrets, so that diagnosing faults does not expose what I write.
- As a member, I want reporting to never slow the app or block what I am doing, so that diagnosis does not cost me anything.
- As a developer, I want signing out or switching accounts to change who later reports are attributed to, so that reports are never tagged with the wrong member.
- As a reviewer, I want all reporting to go through one small interface, so that I can check what leaves the device in one place.
- As a developer, I want freezes of the main thread reported with their stack (on iOS when the app is blocked for long enough, and on both platforms when the system ends the app for not responding), so that I can fix freezes that never show up as crashes.
Implementation decisions
- One reporting module. All app code reports through a single interface with three operations: report an error, count a networking event (with an optional duration), and leave a breadcrumb. It has two implementations: one backed by the Sentry Flutter SDK and one that does nothing. Nothing outside this module imports the SDK.
- The reporting destination is a build-time setting, named with the implementation to match the existing
BUZZ_build settings. When it is absent, the app uses the do-nothing implementation and never initializes the SDK. There is no check on the kind of build, no in-app switch and no notice (ADR 0007). - Crashes and uncaught errors are captured by the SDK's own handlers for native crashes, Flutter framework errors and uncaught asynchronous Dart errors, installed at startup around the app.
- Explicit error reports are added at the places where the app meets something that should never happen: a relay frame or query response that cannot be decoded, a relay rejection whose reason says the app sent something invalid (except a refusal the app already handles as an expected outcome, such as an archived channel or a channel name that is taken, which is counted), an account or push gateway response that cannot be understood, saved data that cannot be read back, and an event subscriber that throws while handling an event.
- Routine failures are counted, never reported. Timeouts, disconnects, unreachable or insecure hosts, rate limits, refused or expired sign-ins and server errors increment a count. That includes the relay's own errors, such as a query that timed out or too many subscriptions. Of the reasons a relay gives for a rejection, only the one that marks the request as invalid is reported, with the exception above; every other reason is counted.
- Networking measurements. Every request through the app's shared HTTP client and every relay connection event (connected, disconnected, reconnect attempt, request timed out, subscription closed by the relay) is counted with four attributes: backend, endpoint, outcome and kind of failure. The backend is the host name of the server the app was talking to. Endpoints are names from a fixed list, never raw paths. Durations are recorded for requests and for time to connect. HTTP is measured where the app's shared HTTP client already classifies transport failures; relay connections are measured by hand in the relay session, since the SDK documents no WebSocket instrumentation.
- Counts are handed to the SDK as they happen, including while the network is down. The app does no buffering of its own, and the SDK's default limit on unsent data is kept. Accepted consequence: in a long outage the oldest unsent reports can be dropped.
- No log lines in step one. The counts carry the member and the time. The first implementation task confirms that the destination can filter counts by member; if it cannot, the fallback is one log line per outage, sent after it ends.
- Breadcrumbs come from a fixed list: networking events, written by the app, and the app moving to the foreground or background, written by the SDK. The SDK's other automatic breadcrumbs, which could carry content or addresses, are turned off or filtered.
- Who a report is about. The member's public key is set as the report's user when a session starts and cleared when it ends. When the session is a Builderlab account, the Builderlab's origin and the account's subject identifier are added, never its email address. Every report also carries the app's own random per-install identifier, created on first launch and kept in a file that device backups and transfers skip, so a phone restored from another gets its own, as a separate field, signed in or not, so reports from one device can be matched to the members who used it, and a member's reports to the devices they used. With no member set, for example before sign-in, the SDK fills the report's user with its own random per-install identifier, on Dart and native reports alike. On iOS, native crash and hang reports also carry the SDK's device app hash: a hash of the identifier iOS gives the app's vendor, the hardware model and the bundle identifier, stable across launches and across a reinstall while another app from the same vendor stays installed. The Dart filter drops it, so Dart reports don't carry it. The SDK also sends session records, so the destination can show a crash-free rate. A session begins each time the app starts or returns after 30 seconds or more in the background. The SDK sends a record when it begins, an update each time an error is counted, and a last record when it ends. Together they carry when the session started, how many errors it had, how it ended (normally, crashed or abnormally), the time from start to end (not time in active use), the release, the environment and the SDK's identifier.
- One filter before anything leaves. Errors, breadcrumbs and counts from Dart all pass through a single filter that removes URL query strings and fragments, drops any attribute not on the allowed list, replaces every error's own text with fixed wording, drops what the SDK adds about the failing widget or platform call, and keeps only the app, operating system, device and runtime details among the report's contexts, so the SDK's accessibility settings, locale and the device's own name stay on the device. An error keeps its type and stack trace. The SDK's options for sending personal data, screenshots, view hierarchies and session recordings stay off.
- Main-thread hangs are captured by the SDK's own hang detection, at the SDK's default thresholds. The app's Dart code runs on the platform's main thread, so Dart work that blocks it shows up here. On iOS, the SDK reports the main thread being blocked, fully or mostly, for 2 seconds or more while the app is active, with the main thread's stack taken during the hang, including freezes the app recovers from. A hang that is still going when the app is killed, for example by the system watchdog, is sent on the next launch as a fatal hang. The SDK's separate watchdog-termination tracking also reports, on the next launch, a run that ended while the app was in the foreground with nothing else to explain it; it never reports on simulators or for kills in the background. App hang detection is off while a debugger is attached (sentry-cocoa 9.35.0). On Android the SDK reports only freezes the system itself declared "app not responding". On Android 11 and later it reports only one that ended the process, read from the system's exit records on the next launch: the most recent unreported one, and only when the system kept a usable thread dump. Before Android 11 it reports when the main thread has been blocked for 5 seconds and the system has marked the app not responding, never with a debugger attached (sentry-java 8.64.0). So with these defaults, a freeze the app recovers from is reported on iOS, and on Android only before Android 11 and only once the system has marked the app not responding. Dart frames in a hang's stack are readable only with the Dart code's debug symbols at the destination, the same as for crashes.
- Native reports skip that filter. Native crashes and app hangs are captured and sent by the native SDKs, which never call the Dart filter. They carry stack traces and, for a native exception, its reason text. No app code puts content in a native exception today, and the SDK's native network breadcrumbs, which would carry request addresses, are off.
- Readable stack traces need the build's debug symbols at the destination. Uploading them belongs to the release process of whoever distributes the build; this repository documents the requirement and adds nothing organization-specific.
- A way to check a build. A build that reports shows a Diagnostics card in Settings with two actions: send a test report (a fixed test error with its own operation, through the same filter as every report) and a test crash in native code, behind a confirmation. A build without a destination never shows it. Whoever distributes a reporting build can then confirm, before relying on it, that reports arrive with readable stack traces.
- Both platforms. Step one covers the main app on iOS and Android. Background work that runs the app's Dart code in its own engine, as decided for Android push handling in ADR 0006, starts the reporting module the same way once it exists. Dart errors are captured in the main app's Flutter engine only: the iOS sheets that start their own engine (channel settings and the profile photo editor) are not covered, though native crashes while they are open still are.
- Reporting never blocks the app. A call into the reporting module returns at once and nothing waits on the destination. A failure inside reporting is dropped and never reaches the code that made the call.
Testing decisions
- A good test here asserts what would be reported for something the user or the network does, never how the reporting module is called internally.
- Tests replace the reporting interface with a fake that records what it was given. This is the only new seam.
- Journey tests extend the existing relay and account journeys with steps such as: a malformed relay frame produces exactly one error report; a timeout or disconnect is counted with the right backend, endpoint and failure kind and produces no report; signing out changes who later reports are about.
- One table-driven test covers the filter: each row is an input (a URL with a query string, an attribute not on the list, a free-text relay message) and what must come out.
- One test checks that a build with no reporting destination never starts the SDK.
- One test checks that a reporting call that fails does not fail or delay the operation that made it.
- The SDK itself is not tested. Prior art: the existing widget tests that override boundary providers for the relay, HTTP and storage.
Out of scope
- The iOS notification service extension and the iOS share extension.
- Log lines, traces and spans, and joining mobile reports to relay traces.
- Holding counts back in the app while the network is down. The SDK's own store of unsent data is the only buffer.
- Requests that bypass the shared HTTP client, such as those made by native media players and image loaders.
- Alert rules, dashboards and the destination's own configuration, which belong to whoever owns the destination.
- Diagnostic reporting in a public build. Nothing prevents it, but what a report carries was chosen for internal builds. Turning it on there first needs the member's public key and the server host names removed, the rest of the list reviewed, and a way to tell people (ADR 0007). Because identity and every attribute pass through one module, that is a small code change.
- Usage or behavior data of any kind, beyond the SDK's session records described above.
- Fixing the bugs the reports will reveal, including two found while planning: one subscriber that throws can lose the rest of a batch of events, and a catch-all in account requests presents every error as an outage.
Further notes
- Depends on the iOS move to Swift Package Manager, since the current Sentry SDK installs on iOS only that way (ADR 0008).
- Not yet verified: whether the destination can filter counts by member, and how Android's store of unsent data behaves.
- A relay's host name can identify a community. It is reported anyway so that two relays can be told apart; an internal build talks only to its own organization's relay, so the name tells the destination's owner nothing new.