Files
buzz/mobile/lib/shared/diagnostics/diagnostic_startup.dart
Tom Brow ec1b786ef0 fix(mobile): diagnostic reporting follow-ups (#9808)
Follow-ups to the mobile diagnostic reporting work in
docs/mobile-diagnostic-reporting.md.

## Changes

- **Install ID repair:** if the install ID file holds something that
isn't text, the app now replaces it with a new ID, as it already does
for an empty file. Before, every report went out without an install ID.
A read that fails (for example, no file descriptor free) still keeps the
file, so a passing error never replaces a good ID.
- **Clock-window refusals are not reported as errors:** the spec treats
relay refusals for a timestamp outside the relay's clock window as
expected (counting them is still pending). The app only recognized one
of the relay's wordings ("timestamp too far from server time"), so
refusals for canvas events, relay-admin commands, leave requests and
observer frames were reported as invalid requests. The five wordings are
matched from the start of the reason, so unrelated `invalid:` reasons
that mention a timestamp, such as the feedback timestamp overflow or one
that echoes a value, are still reported.
- **Diagnostics card:** rows get subtitles, and the divider indent
matches the neighbouring Settings cards.
- **Comments:** explain why push stop gates its current-identity request
on the community key, and correct a test comment about who drops a
repeated error object.

## Validation

New tests cover:
- member attribution on relay sessions, including a missing key and an
answer that arrives after the member changed;
- push stop reports while signed out;
- reporter wiring for hosted admission and the self-hosted probe;
- frames the socket can't decode;
- a table of relay refusal reasons, each either reported or not, for
both `OK` and `CLOSED`;
- an install ID file whose reads fail keeps its ID.

Full-app run on an iPhone 17 Pro simulator at 61b04f6: startup, channel
list, sending and reacting, Settings, the Diagnostics card and Send test
report, and install ID repair (a file that isn't valid UTF-8 and an
empty file each got a new ID that stayed the same on the next launch).
Not covered: a relay clock-window refusal, which the live relay would
not produce.

## Diagnostics card

| Before | After |
| --- | --- |
| ![Diagnostics card
before](https://github.com/user-attachments/assets/b8fa7100-3efd-4c36-ac4d-006d487f4902)
| ![Diagnostics card after, with row
subtitles](https://github.com/user-attachments/assets/9ca7e95b-6061-4efa-b476-d7bb4c113a9b)
|

---------

Signed-off-by: Tom Brow <tomb@block.xyz>
Co-authored-by: studio <dc2fc481343672df31a689840c09299972e704dac592e0da6f18486646a7ee3e@localhost>
2026-11-20 12:49:57 -08:00

151 lines
5.9 KiB
Dart

import 'dart:async';
import 'dart:convert';
import 'dart:io';
import 'package:flutter/foundation.dart';
import 'package:flutter/services.dart';
import 'package:path_provider/path_provider.dart';
import 'package:sentry_flutter/sentry_flutter.dart';
import 'package:uuid/uuid.dart';
import 'diagnostic_filter.dart';
import 'diagnostic_reporter.dart';
import 'sentry_diagnostic_reporter.dart';
/// The build's reporting destination, a Sentry DSN supplied at build time.
/// Builds made from this repository alone have none and report nothing.
const reportingDestination = String.fromEnvironment('BUZZ_SENTRY_DSN');
/// Runs [app] with the reporter this build calls for.
///
/// Without a [destination] the SDK is never initialized: a reporting library
/// given an empty destination can still install its hooks (ADR 0007). With
/// one, the SDK's own handlers capture native crashes, Flutter framework
/// errors and uncaught asynchronous Dart errors from then on. A destination
/// the SDK cannot parse is treated as none, so a configuration mistake never
/// stops the app from starting.
Future<void> runWithDiagnostics({
required String destination,
required FutureOr<void> Function(DiagnosticReporter reporter) app,
}) async {
if (!_isUsable(destination)) return app(const NoopDiagnosticReporter());
final installId = await diagnosticInstallId();
await SentryFlutter.init(
(options) => configureDiagnostics(options, destination),
appRunner: () {
// A scope tag also reaches the native SDKs, so native crashes and
// hangs carry it too.
if (installId != null) {
Sentry.configureScope(
(scope) => scope.setTag(DiagnosticFilter.installIdTag, installId),
);
}
return app(SentryDiagnosticReporter());
},
);
}
const _installIdFile = 'diagnostics_install_id';
/// A random identifier for this installation, so reports from one device can
/// be matched up across sign-ins and sign-outs. Created on first use and kept
/// in a file that device backups and transfers leave behind, so a phone
/// restored from another gets its own: on Android in the files directory,
/// excluded by the backup rules, and on iOS in an Application Support folder
/// the app marks as excluded from backup. Null if storage fails, since
/// reporting must never stop the app from starting.
Future<String?> diagnosticInstallId({
Future<Directory> Function()? directory,
}) async {
try {
final dir = await (directory ?? _installIdDirectory)();
final file = File('${dir.path}/$_installIdFile');
if (await file.exists()) {
// A failed read goes to the catch below and keeps the file: replacing it
// after a passing error would split this device's reports.
final bytes = await file.readAsBytes();
try {
final saved = utf8.decode(bytes).trim();
if (saved.isNotEmpty) return saved;
} on FormatException {
// Not text: a damaged file is replaced below, not a reason to send
// reports without an install ID.
}
}
final created = const Uuid().v4();
await dir.create(recursive: true);
await file.writeAsString(created, flush: true);
return created;
} on Object catch (error) {
debugPrint('[Diagnostics] no install ID: ${error.runtimeType}');
return null;
}
}
Future<Directory> _installIdDirectory() async {
if (!Platform.isIOS) return getApplicationSupportDirectory();
final path = await const MethodChannel(
'buzz/diagnostics',
).invokeMethod<String>('installIdDirectory');
if (path == null) throw StateError('no install ID directory');
return Directory(path);
}
bool _isUsable(String destination) {
if (destination.isEmpty) return false;
try {
final dsn = Dsn.parse(destination);
// The native SDKs also need an HTTP(S) host and a public key, which the
// Dart parser lets through; they would fail after Dart installs its
// handlers. The scheme is checked on the raw text because Dart lowercases
// it and Cocoa does not.
if (!(destination.startsWith('https://') ||
destination.startsWith('http://')) ||
(dsn.uri?.host ?? '').isEmpty ||
dsn.publicKey.isEmpty ||
dsn.projectId.isEmpty) {
throw const FormatException();
}
return true;
} on Object catch (error) {
// Dsn.parse throws ArgumentError or FormatException; the message would
// repeat the destination, so only the type is printed.
debugPrint(
'[Diagnostics] reporting off: unusable destination '
'(${error.runtimeType})',
);
return false;
}
}
/// Applies the reporting settings from `docs/mobile-diagnostic-reporting.md`.
void configureDiagnostics(SentryFlutterOptions options, String destination) {
options
..dsn = destination
..sendDefaultPii = false
..attachScreenshot = false
..attachViewHierarchy = false
// Breadcrumbs come only from the app's fixed list and the SDK's
// foreground/background entries; native ones can carry addresses.
..enableAutoNativeBreadcrumbs = false
// On iOS this is Cocoa's own network breadcrumbs, which keep each
// request's query and fragment and reach native crash reports, which
// never pass through the filter below.
..recordHttpBreadcrumbs = false
// Otherwise the SDK replaces debugPrint in non-debug builds with one that
// records a breadcrumb and prints nothing.
..enablePrintBreadcrumbs = false
..enableUserInteractionBreadcrumbs = false
..enableAppLifecycleBreadcrumbs = true
// Main-thread hangs, at the SDK's default thresholds. Dart runs on the
// main thread, so blocking Dart work is what these catch.
..enableAppHangTracking = true
..anrEnabled = true
..enableWatchdogTerminationTracking = true
..beforeSend = ((event, hint) => DiagnosticFilter.filterEvent(event))
..beforeBreadcrumb = ((crumb, hint) =>
DiagnosticFilter.filterBreadcrumb(crumb))
..beforeSendMetric = ((metric, hint) =>
DiagnosticFilter.filterMetric(metric));
}