mirror of
https://github.com/block/buzz.git
synced 2026-11-28 04:01:45 +00:00
Adds two durations to the mobile networking diagnostics, so work on channel opening and reconnecting has a before and after number from real use: - **Channel open** (`channelOpen`): from asking for a channel's newest history to the messages being shown, or to the failure that stopped it (including a refusal on the live subscription, or one a thread screen reports, which counts as a failed open with no duration when it comes before the history was asked for). Only a channel's first load counts; a catch-up after a reconnect is not an open. An open cut short by a lost connection keeps its clock, so the wait for the reconnect counts. An open that spans a move to the background, or starts there, is not counted, since it measured the time away, and a return after leaving the channel or after the background is a new open. An open the member backed out of still counts, since the load took that long, unless they left while a lost connection was still reconnecting, before its history was asked for again; after a failed open a retry counts as a new one. A refusal that arrives after a move to the background is not counted either: whether a refusal can count is fixed when the load starts. An open is filed under the member whose relay it ran on, and not counted when the app has since moved to another member. - **Relay recovery** (`relayRecovery`): from the last frame a lost connection delivered to the next authenticated connection. Starting at the last frame means the time it took to notice a dead socket counts too, which the existing connect duration misses. The app cannot see pongs, so a quiet connection is taken to have been alive until two ping intervals (60 s) before the loss, and idle time does not inflate the number by more than that: an idle connection that closes cleanly can read up to a minute, since a clean close and a missed ping look the same to the app. Failed attempts in between are part of one recovery. A loss in the background or followed by a move to the background, and a loss carried across a community switch, are not counted. A loss in the first seconds after a return from a brief stay in the background can reach back to before the stay, by at most that same minute. Both durations run on a monotonic clock, so a correction of the device's clock during an open or an outage does not inflate a sample or make it negative. Both go through the existing `network.event` count and `network.duration` distribution with their endpoint name, like the other networking events. ### Regressions None expected. These add no requests: each is one counted event per channel open or per recovered connection, recorded where the app already counts connection events. ### Validation Read from a profile build on an iPhone 12 mini against a hosted relay, with a local log of each event: - Seven channel opens after a cold start: `channelOpen` read 0.36 to 1.48 s per open, each matching the time from the history request to its response in the request log. - Three Wi-Fi drops of about 22 s with a channel open: `relayRecovery` read 30.1, 47.0 and 30.4 s, each spanning the drop, the time until the dead socket was noticed, and the reconnect. `relayConnection` alone read 0.28 to 0.36 s for the same reconnects, so the new duration is what shows the time lost to the dead socket. That run also covered the flows this touches: cold start, opening channels with back navigation, and losing and regaining the network with a channel open. Every open and every reconnect completed. Added tests: - [relay_network_counts_test.dart](mobile/test/shared/relay/relay_network_counts_test.dart): recovery is timed from the last frame, at most two ping intervals back, spans failed attempts, is not moved by a device clock set forward or back during the outage, and is not counted for the background or a community switch; existing journeys now expect a recovery after each loss. - [channel_open_timing_test.dart](mobile/test/features/channels/channel_open_timing_test.dart): an open is timed once, to its history being shown; a reconnect catch-up is not an open; an open spanning a reconnect keeps its start; a member change during the open, or an open started after the app moved to another member, records nothing; backing out still records it; a return after leaving starts a new clock; an open spanning a stay in the background, or started there, records nothing; a live refusal records a failed open, once, with its time when the member already backed out and with no duration when it comes before the history request; a refused retry after a failed open is a new open; a refusal on reconnect ends an open cut short by the lost connection with its full wait, or with no duration if the app was in the background meanwhile; a refusal that arrives before the history is asked for counts in the foreground and records nothing after a move to the background; a refusal reported by a thread screen ends a first open as one refused open, timed once the history was asked for, records nothing after a move to the background, and is not another open once the channel has loaded; a device clock set forward or back during an open does not move its time; a relay deadline is a timeout. Known limits: - The device run predates the final review commits, which changed which opens are counted (background, member switch, refusals) and moved both durations to a monotonic clock, not where an open or recovery starts or ends. - iOS Siri Quick Send tells the relay session the app is back in the foreground while it sends. A Quick Send to a DM whose messages were never loaded this run can count one open, and a connection lost during the send can count a recovery. Both are true first-load and recovery times, at most one open per DM per run. Moving Quick Send onto the background-work hold would remove both, which changes Quick Send and is left out of this PR. - When a saved page of a channel is already on screen, `channelOpen` measures how long fresh history took rather than how long the member waited to see anything, and it ends when the messages are handed to the screen, not at the first frame that draws them. --------- Signed-off-by: Tom Brow <tomb@block.xyz> Co-authored-by: studio <dc2fc481343672df31a689840c09299972e704dac592e0da6f18486646a7ee3e@buzz.block.builderlab.xyz>
314 lines
8.2 KiB
Dart
314 lines
8.2 KiB
Dart
import 'dart:async';
|
|
|
|
import 'package:hooks_riverpod/hooks_riverpod.dart';
|
|
|
|
/// The one interface app code uses for diagnostic reporting.
|
|
///
|
|
/// See `docs/mobile-diagnostic-reporting.md`. Every call returns at once and
|
|
/// never throws: a failure inside reporting is dropped so it cannot reach the
|
|
/// code that made the call.
|
|
abstract interface class DiagnosticReporter {
|
|
/// Reports something that should never happen, such as a frame the app
|
|
/// cannot decode. [operation] names where it happened, from a fixed list.
|
|
void reportError(
|
|
Object error,
|
|
StackTrace? stackTrace, {
|
|
required DiagnosticOperation operation,
|
|
});
|
|
|
|
/// Counts one networking event, with how long it took when known.
|
|
void countNetworkEvent(NetworkEvent event);
|
|
|
|
/// Adds one entry to the trail attached to later reports.
|
|
void leaveBreadcrumb(DiagnosticBreadcrumb breadcrumb);
|
|
|
|
/// Attributes later reports and counts to [pubkey], or to no one.
|
|
void setMember(String? pubkey);
|
|
|
|
/// The member later reports and counts name, as last given to [setMember].
|
|
String? get member;
|
|
|
|
/// How many times [member] has changed. Work started before a change
|
|
/// belongs to nobody after it, even when the same member is back.
|
|
int get memberChanges;
|
|
|
|
/// Whether this build has a reporting destination. Only then does Settings
|
|
/// offer the checks below.
|
|
bool get reporting;
|
|
|
|
/// Sends a [DiagnosticTestError], to check a build's reporting end to end.
|
|
void sendTestReport();
|
|
|
|
/// Crashes the app in native code, to check native crash reports.
|
|
void crashForTest();
|
|
}
|
|
|
|
/// The error a test report carries, so it is easy to tell apart.
|
|
///
|
|
/// Not const: each report needs its own instance, or the SDK's own
|
|
/// deduplication drops a repeat of the last error object.
|
|
class DiagnosticTestError implements Exception {
|
|
DiagnosticTestError();
|
|
|
|
@override
|
|
String toString() => 'DiagnosticTestError';
|
|
}
|
|
|
|
/// Where an explicit error report comes from.
|
|
enum DiagnosticOperation {
|
|
relayFrame,
|
|
relayQuery,
|
|
relayRejection,
|
|
accountResponse,
|
|
pushGatewayResponse,
|
|
storedData,
|
|
eventSubscriber,
|
|
testReport,
|
|
}
|
|
|
|
/// The operation a networking event belongs to, from a fixed list: never a
|
|
/// raw path.
|
|
enum NetworkEndpoint {
|
|
/// A relay WebSocket connecting, or an open one being lost.
|
|
relayConnection,
|
|
|
|
/// A relay WebSocket connection being started again after a loss.
|
|
relayReconnect,
|
|
|
|
/// A request for stored events over the relay's WebSocket.
|
|
relayQuery,
|
|
|
|
/// An event published over the relay's WebSocket.
|
|
relayPublish,
|
|
|
|
/// A live subscription the relay closed.
|
|
relaySubscription,
|
|
|
|
/// An open relay connection coming back after it was lost, timed from the
|
|
/// last frame the lost connection delivered, so the time it took to notice
|
|
/// the loss counts too.
|
|
relayRecovery,
|
|
|
|
/// Opening a channel: its newest history, timed from the request to the
|
|
/// messages being shown.
|
|
channelOpen,
|
|
|
|
/// The relay's HTTP query bridge: `/query`, `/count` and `/profiles/search`.
|
|
relayHttpQuery,
|
|
|
|
/// A server's information document: a GET of its root.
|
|
relayInfo,
|
|
|
|
/// A download through the media connection pool.
|
|
mediaDownload,
|
|
|
|
/// An upload through the media connection pool.
|
|
mediaUpload,
|
|
|
|
/// Joining a community: invites, join policy and admission.
|
|
communityAdmission,
|
|
|
|
/// The Builderlab account service.
|
|
builderlabAccount,
|
|
|
|
/// Any other request through the shared HTTP client.
|
|
otherHttp,
|
|
}
|
|
|
|
/// How a networking event ended.
|
|
enum NetworkOutcome { success, failure }
|
|
|
|
/// Why a networking event failed.
|
|
enum NetworkFailure {
|
|
timeout,
|
|
disconnected,
|
|
offline,
|
|
unreachable,
|
|
insecure,
|
|
rateLimited,
|
|
authRefused,
|
|
serverError,
|
|
relayError,
|
|
relayRefused,
|
|
|
|
/// A request the server refused as invalid or for a missing resource: any
|
|
/// 4xx other than 401, 403, 408 and 429.
|
|
clientError,
|
|
|
|
/// A transport failure that fits no other kind.
|
|
other,
|
|
}
|
|
|
|
/// One counted networking event. [backend] is the server's host name.
|
|
class NetworkEvent {
|
|
const NetworkEvent({
|
|
required this.backend,
|
|
required this.endpoint,
|
|
required this.outcome,
|
|
this.failure,
|
|
this.duration,
|
|
}) : assert((outcome == NetworkOutcome.failure) == (failure != null));
|
|
|
|
final String backend;
|
|
final NetworkEndpoint endpoint;
|
|
final NetworkOutcome outcome;
|
|
final NetworkFailure? failure;
|
|
final Duration? duration;
|
|
}
|
|
|
|
/// A screen the person can be on, from a fixed list the app writes. Only
|
|
/// these names ever leave the device for navigation, never a channel or
|
|
/// community name, an ID, or anything else a route could carry.
|
|
enum DiagnosticScreen {
|
|
channelList,
|
|
channel,
|
|
thread,
|
|
forumPost,
|
|
channelDetails,
|
|
search,
|
|
activity,
|
|
settings,
|
|
appearance,
|
|
profile,
|
|
agents,
|
|
createAgent,
|
|
invites,
|
|
communitySwitcher,
|
|
addCommunity,
|
|
mediaViewer,
|
|
onboarding,
|
|
|
|
/// Any page the app does not name.
|
|
other,
|
|
}
|
|
|
|
/// One entry in the trail of recent events: a networking event or the screen
|
|
/// the person moved to.
|
|
sealed class DiagnosticBreadcrumb {
|
|
const DiagnosticBreadcrumb._();
|
|
|
|
/// A networking event.
|
|
const factory DiagnosticBreadcrumb(NetworkEvent event) = NetworkBreadcrumb;
|
|
|
|
/// The person moved to [screen].
|
|
const factory DiagnosticBreadcrumb.screen(DiagnosticScreen screen) =
|
|
ScreenBreadcrumb;
|
|
}
|
|
|
|
class NetworkBreadcrumb extends DiagnosticBreadcrumb {
|
|
const NetworkBreadcrumb(this.event) : super._();
|
|
|
|
final NetworkEvent event;
|
|
}
|
|
|
|
class ScreenBreadcrumb extends DiagnosticBreadcrumb {
|
|
const ScreenBreadcrumb(this.screen) : super._();
|
|
|
|
final DiagnosticScreen screen;
|
|
}
|
|
|
|
/// Used when the build has no reporting destination.
|
|
class NoopDiagnosticReporter implements DiagnosticReporter {
|
|
const NoopDiagnosticReporter();
|
|
|
|
@override
|
|
void reportError(
|
|
Object error,
|
|
StackTrace? stackTrace, {
|
|
required DiagnosticOperation operation,
|
|
}) {
|
|
// A throwing subscriber is a bug in the app, and the delivery guard
|
|
// catches it. With no destination, surface it the way an uncaught throw
|
|
// would: it fails the test that hit it and prints in development.
|
|
if (operation == DiagnosticOperation.eventSubscriber) {
|
|
Zone.current.handleUncaughtError(error, stackTrace ?? StackTrace.current);
|
|
}
|
|
}
|
|
|
|
@override
|
|
void countNetworkEvent(NetworkEvent event) {}
|
|
|
|
@override
|
|
void leaveBreadcrumb(DiagnosticBreadcrumb breadcrumb) {}
|
|
|
|
@override
|
|
void setMember(String? pubkey) {}
|
|
|
|
@override
|
|
String? get member => null;
|
|
|
|
@override
|
|
int get memberChanges => 0;
|
|
|
|
@override
|
|
bool get reporting => false;
|
|
|
|
@override
|
|
void sendTestReport() {}
|
|
|
|
@override
|
|
void crashForTest() {}
|
|
}
|
|
|
|
/// Reports from work signed by [signer], such as a one-shot socket for a
|
|
/// community that may not be active. They go through while [reporter] names
|
|
/// [signer] or no member (reports then carry the install identifier), and are
|
|
/// dropped while it names another member, who didn't hit them.
|
|
class SignerDiagnosticReporter implements DiagnosticReporter {
|
|
const SignerDiagnosticReporter(this.reporter, {required this.signer});
|
|
|
|
final DiagnosticReporter reporter;
|
|
final String? signer;
|
|
|
|
bool get _current => switch (reporter.member) {
|
|
null => true,
|
|
final member => member == signer,
|
|
};
|
|
|
|
@override
|
|
void reportError(
|
|
Object error,
|
|
StackTrace? stackTrace, {
|
|
required DiagnosticOperation operation,
|
|
}) {
|
|
if (_current) {
|
|
reporter.reportError(error, stackTrace, operation: operation);
|
|
}
|
|
}
|
|
|
|
@override
|
|
void countNetworkEvent(NetworkEvent event) {
|
|
if (_current) reporter.countNetworkEvent(event);
|
|
}
|
|
|
|
@override
|
|
void leaveBreadcrumb(DiagnosticBreadcrumb breadcrumb) {
|
|
if (_current) reporter.leaveBreadcrumb(breadcrumb);
|
|
}
|
|
|
|
/// Signed work never changes who the app's reports name.
|
|
@override
|
|
void setMember(String? pubkey) {}
|
|
|
|
@override
|
|
String? get member => reporter.member;
|
|
|
|
@override
|
|
int get memberChanges => reporter.memberChanges;
|
|
|
|
@override
|
|
bool get reporting => reporter.reporting;
|
|
|
|
@override
|
|
void sendTestReport() => reporter.sendTestReport();
|
|
|
|
@override
|
|
void crashForTest() => reporter.crashForTest();
|
|
}
|
|
|
|
/// The reporter chosen at startup. The root scope overrides it; tests
|
|
/// override it with a fake that records what it was given.
|
|
final diagnosticReporterProvider = Provider<DiagnosticReporter>(
|
|
(ref) => const NoopDiagnosticReporter(),
|
|
);
|