mirror of
https://github.com/block/buzz.git
synced 2026-11-24 18:21:59 +00:00
Step one of [mobile diagnostic
reporting](https://github.com/block/buzz/blob/main/docs/mobile-diagnostic-reporting.md),
second piece: the explicit error reports the spec lists, through the
reporter added in [#9613 diagnostic reporting
module](https://github.com/block/buzz/pull/9613). A build without a
destination (every build from this repository alone) still reports
nothing.
### What's in it
Each report carries its `operation` tag, the error's type and stack
trace, and no error text (the filter replaces it).
- **Event subscriber (`eventSubscriber`):** a subscriber that throws
while the relay session delivers a buffered batch is reported, and the
rest of the batch is still delivered. In a build without a destination
the error still surfaces as uncaught, so it fails the test that hit it
and prints in development, as before. Before, one throw dropped the rest
of the batch, and those events never came back because their delivery
keys were already recorded.
- **Relay frames (`relayFrame`):** a frame the socket can't decode, or
whose body isn't a JSON array, is reported instead of silently skipped.
- **Relay queries (`relayQuery`):** an HTTP query body that can't be
decoded, including the read-state snapshot.
- **Invalid-request rejections (`relayRejection`):** an `OK false` or
`CLOSED` whose reason starts with `invalid:`, reported as a fixed
`RelayInvalidRequest` with no relay text, including an `OK` with no
pending publish (a typing indicator, or a reply after a timeout).
Refusals the app handles as expected outcomes (an archived channel, an
Unarchive of a channel that is no longer archived, an open channel name
that is taken, an agent whose `policy:` refuses being added, a sole
owner's Leave refused as the last owner, a guest's Add Members refused
because only members can add people, an event whose timestamp is outside
the relay's window, so a wrong device clock does not report every
publish) are left out. The long-lived relay session, the short-lived
sockets (hosted admission, the self-hosted probe, one-shot publishes
such as agent registration and push tombstones) and the account
assertion requests made for a community's key (relay access and push
stop assertions) report frames and rejections the same way, except while
reports name a member other than the key they sign with. A removed or
inactive community's tombstone, or the probe of a community being
joined, then reports nothing rather than naming the wrong member. With
no member set (first run, or after the last community is removed) it
reports under the install identifier.
- **Account responses (`accountResponse`):** a Builderlab account
response that isn't a JSON object, or a 2xx exchange or `/v1/auth/me`
response missing its credential, subject or expiry. Non-2xx statuses are
expected outcomes and aren't reported.
- **Push gateway responses (`pushGatewayResponse`):** a malformed push
capability descriptor from the relay. A relay that leaves
`supported_extensions` out or sends none has no push and isn't reported,
and a fetch that finishes after a community switch has begun isn't
reported either, since it would name the next member. Push gateway
replies aren't reported yet.
- **Stored data (`storedData`):** a saved community list that can't be
read back.
- **Member on switch:** switching communities now looks the new member
up in storage, not in the provider's list, which can still be the
previous one while it reloads after sign-in. It reads storage before
tearing anything down, so a failed read leaves the switch unchanged and
retryable.
- **Filter:** also drops a report's additional data (`extra`). Nothing
sets it today.
- The spec's status line now says what is implemented.
- Event delivery moves from `relay_session.dart` into a part file,
unchanged apart from the subscriber guard, so the session stays under
its size limit.
### Regressions
None expected: each change only adds a report where an error was already
caught or skipped. The one behavior change is that a throwing event
subscriber no longer costs the rest of its batch.
### Validation
Added tests for each report site (relay session, socket frames, one-shot
publishes, read-state snapshot, closed-reason policy, account client,
push descriptor and capability provider, community storage), for the
relay session (member changed mid-request), one-shot sockets and
assertion requests reporting nothing while another member is set and
reporting with none set, for the member on switch and a switch whose
read fails then retries, and for the filter dropping additional data and
leaving a message-less event without one.
Full-app run on an iPhone 17 Pro simulator at b4f05ad (debug build, no
reporting destination): signed-in launch and channel list, scrolling
history in several channels, a thread, sending a message and a reaction,
search, switching communities and back, background and foreground with
live updates afterwards, and kill and relaunch with saved communities
restored. No regressions and no exceptions from the changed code.
---------
Signed-off-by: Tom Brow <tomb@block.xyz>
Co-authored-by: studio <dc2fc481343672df31a689840c09299972e704dac592e0da6f18486646a7ee3e@localhost>
507 lines
17 KiB
Dart
507 lines
17 KiB
Dart
import 'dart:async';
|
|
import 'dart:convert';
|
|
import 'dart:io';
|
|
|
|
import 'package:buzz/shared/community/community_provider.dart';
|
|
import 'package:buzz/shared/diagnostics/diagnostic_filter.dart';
|
|
import 'package:buzz/shared/diagnostics/diagnostic_member_binding.dart';
|
|
import 'package:buzz/shared/diagnostics/diagnostic_reporter.dart';
|
|
import 'package:buzz/shared/diagnostics/diagnostic_startup.dart';
|
|
import 'package:buzz/shared/diagnostics/sentry_diagnostic_reporter.dart';
|
|
import 'package:buzz/shared/relay/relay_provider.dart';
|
|
import 'package:flutter/services.dart';
|
|
import 'package:flutter/widgets.dart';
|
|
import 'package:flutter_test/flutter_test.dart';
|
|
import 'package:hooks_riverpod/hooks_riverpod.dart';
|
|
import 'package:nostr/nostr.dart' as nostr;
|
|
// ignore: depend_on_referenced_packages, implementation_imports
|
|
import 'package:sentry/src/deduplication/deduplication_event_processor.dart';
|
|
import 'package:sentry_flutter/sentry_flutter.dart';
|
|
// The SDK's own processors and integrations, so tests see what it really
|
|
// sends rather than hand-built events.
|
|
// ignore: implementation_imports
|
|
import 'package:sentry_flutter/src/breadcrumbs/debug_print_integration.dart';
|
|
// ignore: implementation_imports
|
|
import 'package:sentry_flutter/src/exception/flutter_error_integration.dart';
|
|
// ignore: implementation_imports
|
|
import 'package:sentry_flutter/src/enrichment/flutter_enricher_event_processor.dart';
|
|
// ignore: implementation_imports
|
|
import 'package:sentry_flutter/src/enrichment/widget_event_processor.dart';
|
|
// ignore: implementation_imports
|
|
import 'package:sentry_flutter/src/url_filter/url_filter_event_processor.dart';
|
|
// ignore: implementation_imports
|
|
import 'package:sentry_flutter/src/exception/flutter_exception_event_processor.dart';
|
|
// ignore: implementation_imports
|
|
import 'package:sentry_flutter/src/exception/platform_exception_event_processor.dart';
|
|
|
|
void main() {
|
|
group('what leaves the device', () {
|
|
final options = SentryFlutterOptions();
|
|
configureDiagnostics(options, 'https://key@example.org/1');
|
|
|
|
Breadcrumb? crumb(Breadcrumb input) =>
|
|
options.beforeBreadcrumb!(input, Hint());
|
|
|
|
Future<SentryEvent> event(SentryEvent input) async =>
|
|
(await options.beforeSend!(input, Hint()))!;
|
|
|
|
Future<Map<String, SentryAttribute>> metric(
|
|
Map<String, SentryAttribute> attributes,
|
|
) async => (await options.beforeSendMetric!(
|
|
SentryCounterMetric(
|
|
timestamp: DateTime(2026),
|
|
name: 'network.event',
|
|
value: 1,
|
|
traceId: SentryId.newId(),
|
|
attributes: attributes,
|
|
),
|
|
Hint(),
|
|
))!.attributes;
|
|
|
|
final rows = <(String, Future<Object?> Function(), Object?)>[
|
|
(
|
|
'an HTTP breadcrumb from the SDK is dropped',
|
|
() async => crumb(
|
|
Breadcrumb.http(url: Uri.parse('https://e.org'), method: 'GET'),
|
|
),
|
|
null,
|
|
),
|
|
(
|
|
'a navigation breadcrumb is dropped',
|
|
() async => crumb(Breadcrumb(category: 'navigation', message: 'Chat')),
|
|
null,
|
|
),
|
|
(
|
|
'a lifecycle breadcrumb is kept',
|
|
() async => crumb(
|
|
Breadcrumb(category: 'app.lifecycle', data: {'state': 'paused'}),
|
|
)?.data,
|
|
{'state': 'paused'},
|
|
),
|
|
(
|
|
'a network breadcrumb keeps only its attributes',
|
|
() async => crumb(
|
|
Breadcrumb(
|
|
category: 'network',
|
|
data: {'backend': 'relay.example.org', 'channel': 'secret-room'},
|
|
),
|
|
)?.data,
|
|
{'backend': 'relay.example.org'},
|
|
),
|
|
(
|
|
'a URL in a message loses its query string and fragment',
|
|
() async => (await event(
|
|
SentryEvent(
|
|
message: SentryMessage(
|
|
'failed https://e.org/p?token=abc#frag and wss://r.org/x?y=1',
|
|
),
|
|
),
|
|
)).message?.formatted,
|
|
'failed https://e.org/p and wss://r.org/x',
|
|
),
|
|
(
|
|
'an event without a message gets none',
|
|
() async => (await event(SentryEvent())).message,
|
|
null,
|
|
),
|
|
(
|
|
'additional data is dropped',
|
|
() async => (await event(
|
|
// ignore: deprecated_member_use
|
|
SentryEvent(extra: {'body': 'private-marker'}),
|
|
)).extra, // ignore: deprecated_member_use
|
|
null,
|
|
),
|
|
(
|
|
'a tag not on the list is dropped',
|
|
() async => (await event(
|
|
SentryEvent(
|
|
tags: {
|
|
'operation': 'relayFrame',
|
|
'install.id': 'i',
|
|
'channel': 'x',
|
|
},
|
|
),
|
|
)).tags,
|
|
{'operation': 'relayFrame', 'install.id': 'i'},
|
|
),
|
|
(
|
|
'a count keeps its attributes and the member, nothing else',
|
|
() async => (await metric({
|
|
'backend': SentryAttribute.string('relay.example.org'),
|
|
'user.id': SentryAttribute.string('ab'),
|
|
'path': SentryAttribute.string('/channels/secret'),
|
|
})).keys.toSet(),
|
|
{'backend', 'user.id'},
|
|
),
|
|
];
|
|
|
|
for (final (name, input, expected) in rows) {
|
|
test(name, () async => expect(await input(), expected));
|
|
}
|
|
|
|
test('personal data, screenshots and native breadcrumbs stay off', () {
|
|
expect(options.sendDefaultPii, isFalse);
|
|
expect(options.attachScreenshot, isFalse);
|
|
expect(options.attachViewHierarchy, isFalse);
|
|
expect(options.enableAutoNativeBreadcrumbs, isFalse);
|
|
expect(options.recordHttpBreadcrumbs, isFalse);
|
|
expect(options.replay.sessionSampleRate, isNull);
|
|
expect(options.tracesSampleRate, isNull);
|
|
});
|
|
|
|
test('main-thread hang detection is on at the default thresholds', () {
|
|
final defaults = SentryFlutterOptions();
|
|
expect(options.enableAppHangTracking, isTrue);
|
|
expect(options.anrEnabled, isTrue);
|
|
expect(options.enableWatchdogTerminationTracking, isTrue);
|
|
expect(options.appHangTimeoutInterval, defaults.appHangTimeoutInterval);
|
|
expect(options.anrTimeoutInterval, defaults.anrTimeoutInterval);
|
|
});
|
|
});
|
|
|
|
group('a sent error', () {
|
|
late _MemoryTransport transport;
|
|
late SentryFlutterOptions options;
|
|
late Hub hub;
|
|
|
|
setUp(() {
|
|
transport = _MemoryTransport();
|
|
options = SentryFlutterOptions();
|
|
configureDiagnostics(options, 'https://key@example.org/1');
|
|
// The processors Sentry.init and SentryFlutter.init install.
|
|
options
|
|
..transport = transport
|
|
..sendClientReports = false
|
|
..addEventProcessor(DeduplicationEventProcessor(options))
|
|
..addEventProcessor(FlutterExceptionEventProcessor())
|
|
..addEventProcessor(FlutterEnricherEventProcessor(options))
|
|
..addEventProcessor(WidgetEventProcessor())
|
|
..addEventProcessor(UrlFilterEventProcessor(options))
|
|
..addEventProcessor(PlatformExceptionEventProcessor());
|
|
hub = Hub(options);
|
|
});
|
|
tearDown(() => hub.close());
|
|
|
|
final rows = <(String, Object, String)>[
|
|
('a parse failure', const FormatException('private-marker'), 'Format'),
|
|
('a bad argument', ArgumentError.value('private-marker'), 'Argument'),
|
|
('a server response', StateError('body: private-marker'), 'StateError'),
|
|
(
|
|
'a platform failure',
|
|
PlatformException(
|
|
code: 'failed',
|
|
message: 'open https://e.org/f?token=private-marker',
|
|
),
|
|
'PlatformException',
|
|
),
|
|
];
|
|
for (final (name, error, type) in rows) {
|
|
test('keeps only the type and stack of $name', () async {
|
|
await hub.captureException(error, stackTrace: StackTrace.current);
|
|
final sent = transport.events.single;
|
|
|
|
expect(jsonEncode(sent), isNot(contains('private-marker')));
|
|
final exception = (sent['exception']['values'] as List).last as Map;
|
|
expect(exception['type'], contains(type));
|
|
expect(exception['value'], DiagnosticFilter.removedText);
|
|
expect(exception['stacktrace'], isNotNull);
|
|
});
|
|
}
|
|
|
|
test('a report right after a member change names the new member', () async {
|
|
SentryDiagnosticReporter(hub: hub)
|
|
..setMember('a' * 64)
|
|
..reportError(
|
|
StateError('failed'),
|
|
StackTrace.current,
|
|
operation: DiagnosticOperation.relayFrame,
|
|
);
|
|
await transport.first();
|
|
|
|
expect(transport.events.single['user']['id'], 'a' * 64);
|
|
});
|
|
|
|
test('a test report is a test error from the test operation', () async {
|
|
SentryDiagnosticReporter(hub: hub).sendTestReport();
|
|
await transport.first();
|
|
|
|
final sent = transport.events.single;
|
|
final exception = (sent['exception']['values'] as List).last as Map;
|
|
expect(exception['type'], 'DiagnosticTestError');
|
|
expect(exception['stacktrace'], isNotNull);
|
|
expect(sent['tags']['operation'], 'testReport');
|
|
});
|
|
|
|
test('every test report is sent, not only the first', () async {
|
|
SentryDiagnosticReporter(hub: hub)
|
|
..sendTestReport()
|
|
..sendTestReport();
|
|
await transport.count(2);
|
|
|
|
expect(transport.events, hasLength(2));
|
|
});
|
|
|
|
testWidgets('sends only the allowed contexts', (tester) async {
|
|
tester.platformDispatcher.accessibilityFeaturesTestValue =
|
|
const FakeAccessibilityFeatures(accessibleNavigation: true);
|
|
addTearDown(
|
|
tester.platformDispatcher.clearAccessibilityFeaturesTestValue,
|
|
);
|
|
|
|
await tester.runAsync(
|
|
() => hub.captureException(
|
|
StateError('failed'),
|
|
stackTrace: StackTrace.current,
|
|
),
|
|
);
|
|
|
|
expect(
|
|
(transport.events.single['contexts'] as Map).keys,
|
|
everyElement(
|
|
isIn(['app', 'device', 'os', 'runtime', 'runtimes', 'trace']),
|
|
),
|
|
);
|
|
});
|
|
|
|
test('keeps only listed fields within contexts', () {
|
|
// As the native SDKs report them: the Dart SDK keeps keys it has no
|
|
// field for and writes them back out.
|
|
final event = SentryEvent(
|
|
contexts: Contexts(
|
|
device: SentryDevice.fromJson({
|
|
'name': 'private-marker',
|
|
'model': 'iPhone',
|
|
'locale': 'private-marker',
|
|
'timezone': 'private-marker',
|
|
}),
|
|
app: SentryApp.fromJson({
|
|
'app_name': 'Buzz',
|
|
'device_app_hash': 'private-marker',
|
|
'x': 'private-marker',
|
|
}),
|
|
operatingSystem: SentryOperatingSystem.fromJson({
|
|
'name': 'iOS',
|
|
'x': 'private-marker',
|
|
}),
|
|
runtimes: [
|
|
SentryRuntime.fromJson({'name': 'Dart', 'x': 'private-marker'}),
|
|
],
|
|
),
|
|
);
|
|
|
|
final contexts = DiagnosticFilter.filterEvent(event).contexts;
|
|
|
|
expect(jsonEncode(contexts.toJson()), isNot(contains('private-marker')));
|
|
expect(contexts.device!.model, 'iPhone');
|
|
expect(contexts.app!.name, 'Buzz');
|
|
expect(contexts.operatingSystem!.name, 'iOS');
|
|
});
|
|
|
|
testWidgets('leaves out what a framework error says about the screen', (
|
|
tester,
|
|
) async {
|
|
final integration = FlutterErrorIntegration();
|
|
integration(hub, options);
|
|
addTearDown(integration.close);
|
|
|
|
// A Text without Directionality is a real framework build error.
|
|
await tester.pumpWidget(const Text('private-marker'));
|
|
expect(tester.takeException(), isA<FlutterError>());
|
|
await tester.runAsync(transport.first);
|
|
|
|
expect(
|
|
jsonEncode(transport.events.single),
|
|
isNot(contains('private-marker')),
|
|
);
|
|
});
|
|
});
|
|
|
|
test('device logs keep printing in a build that reports', () async {
|
|
final options = SentryFlutterOptions(checker: _ReleaseChecker());
|
|
configureDiagnostics(options, 'https://key@example.org/1');
|
|
final printed = <String?>[];
|
|
final original = debugPrint;
|
|
debugPrint = (message, {wrapWidth}) => printed.add(message);
|
|
final integration = DebugPrintIntegration();
|
|
try {
|
|
integration(Hub(options), options);
|
|
debugPrint('an existing log line');
|
|
} finally {
|
|
integration.close();
|
|
debugPrint = original;
|
|
}
|
|
expect(printed, ['an existing log line']);
|
|
});
|
|
|
|
for (final (name, destination) in [
|
|
('no reporting destination', ''),
|
|
('a destination the SDK cannot parse', 'https://key@example.org'),
|
|
('a destination without a public key', 'https://example.org/1'),
|
|
('a destination without an HTTP host', 'ftp://key@example.org/1'),
|
|
('a destination with an uppercase scheme', 'HTTPS://key@example.org/1'),
|
|
]) {
|
|
test('a build with $name starts without the SDK', () async {
|
|
DiagnosticReporter? started;
|
|
await runWithDiagnostics(
|
|
destination: destination,
|
|
app: (r) => started = r,
|
|
);
|
|
|
|
expect(started, isA<NoopDiagnosticReporter>());
|
|
expect(Sentry.isEnabled, isFalse);
|
|
});
|
|
}
|
|
|
|
test('an installation keeps one install ID across launches', () async {
|
|
final installation = await Directory.systemTemp.createTemp('install');
|
|
addTearDown(() => installation.delete(recursive: true));
|
|
Future<Directory> storage() async => installation;
|
|
|
|
final first = await diagnosticInstallId(directory: storage);
|
|
expect(first, isNotNull);
|
|
expect(await diagnosticInstallId(directory: storage), first);
|
|
|
|
final other = await Directory.systemTemp.createTemp('install');
|
|
addTearDown(() => other.delete(recursive: true));
|
|
expect(
|
|
await diagnosticInstallId(directory: () async => other),
|
|
isNot(first),
|
|
);
|
|
});
|
|
|
|
test('a reporting call that fails does not reach the caller', () async {
|
|
for (final reporter in [
|
|
SentryDiagnosticReporter(hub: _FailingHub(), metrics: _FailingMetrics()),
|
|
SentryDiagnosticReporter(
|
|
hub: _FailingHub(async: true),
|
|
metrics: _FailingMetrics(),
|
|
),
|
|
]) {
|
|
const event = NetworkEvent(
|
|
backend: 'relay.example.org',
|
|
endpoint: NetworkEndpoint.relayConnection,
|
|
outcome: NetworkOutcome.failure,
|
|
failure: NetworkFailure.timeout,
|
|
duration: Duration(seconds: 1),
|
|
);
|
|
reporter
|
|
..reportError(
|
|
StateError('x'),
|
|
null,
|
|
operation: DiagnosticOperation.relayFrame,
|
|
)
|
|
..countNetworkEvent(event)
|
|
..leaveBreadcrumb(const DiagnosticBreadcrumb(event))
|
|
..setMember('ab');
|
|
}
|
|
// An unhandled asynchronous failure would fail this test.
|
|
await pumpEventQueue();
|
|
});
|
|
|
|
test('signing in, switching and signing out change the member', () async {
|
|
final reporter = _RecordingReporter();
|
|
final container = ProviderContainer(
|
|
overrides: [
|
|
diagnosticReporterProvider.overrideWithValue(reporter),
|
|
activeCommunityProvider.overrideWith((ref) async => null),
|
|
],
|
|
);
|
|
addTearDown(container.dispose);
|
|
container.listen(diagnosticMemberProvider, (_, _) {});
|
|
final relay = container.read(relayConfigProvider.notifier);
|
|
|
|
relay.update(baseUrl: 'https://relay.example.org', nsec: _nsec('1'));
|
|
container.read(diagnosticMemberProvider);
|
|
relay.update(baseUrl: 'https://relay.example.org', nsec: _nsec('2'));
|
|
container.read(diagnosticMemberProvider);
|
|
relay.update(baseUrl: 'https://relay.example.org');
|
|
container.read(diagnosticMemberProvider);
|
|
|
|
expect(reporter.members, [
|
|
null,
|
|
pubkeyFromNsec(_nsec('1')),
|
|
pubkeyFromNsec(_nsec('2')),
|
|
null,
|
|
]);
|
|
expect(reporter.members[1], isNot(reporter.members[2]));
|
|
});
|
|
}
|
|
|
|
String _nsec(String digit) =>
|
|
nostr.Nip19.encode(prefix: nostr.Nip19Prefix.nsec, data: digit * 64);
|
|
|
|
class _RecordingReporter extends NoopDiagnosticReporter {
|
|
final members = <String?>[];
|
|
|
|
@override
|
|
void setMember(String? pubkey) => members.add(pubkey);
|
|
}
|
|
|
|
class _FailingHub extends Fake implements Hub {
|
|
_FailingHub({this.async = false});
|
|
|
|
final bool async;
|
|
|
|
Future<Never> _fail() =>
|
|
async ? Future.error(StateError('down')) : throw StateError('down');
|
|
|
|
@override
|
|
Future<SentryId> captureException(
|
|
dynamic throwable, {
|
|
dynamic stackTrace,
|
|
Hint? hint,
|
|
SentryMessage? message,
|
|
ScopeCallback? withScope,
|
|
}) => _fail();
|
|
|
|
@override
|
|
Future<void> addBreadcrumb(Breadcrumb crumb, {Hint? hint}) => _fail();
|
|
|
|
@override
|
|
Future<void> configureScope(ScopeCallback callback) => _fail();
|
|
}
|
|
|
|
class _FailingMetrics extends Fake implements SentryMetrics {
|
|
@override
|
|
void count(
|
|
String name,
|
|
int value, {
|
|
Map<String, SentryAttribute>? attributes,
|
|
}) => throw StateError('down');
|
|
}
|
|
|
|
class _ReleaseChecker extends RuntimeChecker {
|
|
@override
|
|
bool isDebugMode() => false;
|
|
@override
|
|
bool isReleaseMode() => true;
|
|
@override
|
|
bool isProfileMode() => false;
|
|
}
|
|
|
|
class _MemoryTransport implements Transport {
|
|
final events = <Map<String, dynamic>>[];
|
|
final _first = Completer<void>();
|
|
final _added = StreamController<void>.broadcast();
|
|
|
|
Future<void> first() => _first.future;
|
|
|
|
Future<void> count(int n) async {
|
|
if (events.length >= n) return;
|
|
await _added.stream.firstWhere((_) => events.length >= n);
|
|
}
|
|
|
|
@override
|
|
Future<SentryId?> send(SentryEnvelope envelope) async {
|
|
for (final item in envelope.items) {
|
|
if (item.header.type != 'event') continue;
|
|
final bytes = await item.dataFactory();
|
|
events.add(jsonDecode(utf8.decode(bytes)) as Map<String, dynamic>);
|
|
if (!_first.isCompleted) _first.complete();
|
|
_added.add(null);
|
|
}
|
|
return envelope.header.eventId;
|
|
}
|
|
}
|