Files
buzz/mobile/lib/shared/diagnostics/sentry_diagnostic_reporter.dart
Tom Brow b544fcea4a feat(mobile): add a diagnostics test report and test crash to Settings (#9676)
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 |
| --- | --- |
| ![Settings bottom,
before](https://github.com/user-attachments/assets/92fdef05-8d63-43db-8df1-32ccb92105c8)
| ![Settings bottom with the Diagnostics
card](https://github.com/user-attachments/assets/a205ab18-ed1e-49d5-8e34-88e29e4c91fa)
|

![Test crash
confirmation](https://github.com/user-attachments/assets/335eb362-3a61-4c8c-8b32-44de548cf788)

### 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>
2026-11-18 14:48:25 -08:00

113 lines
3.2 KiB
Dart

import 'dart:async';
import 'package:flutter/foundation.dart';
import 'package:sentry_flutter/sentry_flutter.dart';
import 'diagnostic_filter.dart';
import 'diagnostic_reporter.dart';
/// Sends diagnostic reports through the Sentry SDK.
///
/// The only file besides startup that imports the SDK. Calls hand their work
/// to the SDK without waiting, and a failure inside reporting is dropped: the
/// spec requires that reporting never fails or delays the caller.
class SentryDiagnosticReporter implements DiagnosticReporter {
SentryDiagnosticReporter({Hub? hub, SentryMetrics? metrics})
: _hub = hub ?? HubAdapter(),
_metricsOverride = metrics;
final Hub _hub;
final SentryMetrics? _metricsOverride;
// Read on each call: the SDK installs its metrics when it starts.
SentryMetrics get _metrics => _metricsOverride ?? Sentry.metrics;
@override
void reportError(
Object error,
StackTrace? stackTrace, {
required DiagnosticOperation operation,
}) => _drop(
() => _hub.captureException(
error,
stackTrace: stackTrace,
withScope: (scope) => scope.setTag('operation', operation.name),
),
);
@override
void countNetworkEvent(NetworkEvent event) => _drop(() {
final attributes = _attributes(event);
_metrics.count('network.event', 1, attributes: attributes);
if (event.duration case final duration?) {
_metrics.distribution(
'network.duration',
duration.inMicroseconds / 1000,
unit: SentryMetricUnit.millisecond,
attributes: attributes,
);
}
return null;
});
@override
void leaveBreadcrumb(DiagnosticBreadcrumb breadcrumb) => _drop(
() => _hub.addBreadcrumb(
Breadcrumb(
category: DiagnosticFilter.networkCategory,
level: breadcrumb.event.outcome == NetworkOutcome.failure
? SentryLevel.warning
: SentryLevel.info,
data: {
for (final entry in _attributes(breadcrumb.event).entries)
entry.key: entry.value.value,
},
),
),
);
@override
void setMember(String? pubkey) => _drop(
() => _hub.configureScope(
(scope) => scope.setUser(pubkey == null ? null : SentryUser(id: pubkey)),
),
);
@override
bool get reporting => true;
@override
void sendTestReport() => reportError(
DiagnosticTestError(),
StackTrace.current,
operation: DiagnosticOperation.testReport,
);
@override
void crashForTest() => _drop(SentryFlutter.nativeCrash);
static Map<String, SentryAttribute> _attributes(NetworkEvent event) => {
'backend': SentryAttribute.string(event.backend),
'endpoint': SentryAttribute.string(event.endpoint.name),
'outcome': SentryAttribute.string(event.outcome.name),
if (event.failure case final failure?)
'failure': SentryAttribute.string(failure.name),
};
static void _drop(Object? Function() send) {
try {
final result = send();
if (result is Future<Object?>) {
unawaited(result.then<Object?>((_) => null, onError: _dropped));
}
} catch (error) {
_dropped(error);
}
}
static Null _dropped(Object error) {
debugPrint('[Diagnostics] report dropped: ${error.runtimeType}');
return null;
}
}