Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(ttfd): unfinished ttfd should match ttid duration #2347

Merged
merged 11 commits into from
Oct 15, 2024
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ Navigator.push(
- iOS replay integration when only `onErrorSampleRate` is specified ([#2306](https://github.com/getsentry/sentry-dart/pull/2306))
- Fix TTID timing issue ([#2326](https://github.com/getsentry/sentry-dart/pull/2326))
- Start missing TTFD for root screen transaction ([#2332](https://github.com/getsentry/sentry-dart/pull/2332))
- Match TTFD to TTID end timespan if TTFD is unfinished when user navigates to another screen ([#2347](https://github.com/getsentry/sentry-dart/pull/2347))
- Accessing invalid json fields from `fetchNativeAppStart` should return null ([#2340](https://github.com/getsentry/sentry-dart/pull/2340))
- Error when calling `SentryFlutter.reportFullyDisplayed()` twice ([#2339](https://github.com/getsentry/sentry-dart/pull/2339))

Expand Down
10 changes: 8 additions & 2 deletions flutter/lib/src/navigation/sentry_navigator_observer.dart
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import 'package:meta/meta.dart';
import '../native/native_frames.dart';
import '../native/sentry_native_binding.dart';
import 'time_to_display_tracker.dart';
import 'time_to_full_display_tracker.dart';

import '../../sentry_flutter.dart';
import '../event_processor/flutter_enricher_event_processor.dart';
Expand Down Expand Up @@ -309,13 +310,18 @@ class SentryNavigatorObserver extends RouteObserver<PageRoute<dynamic>> {
// Cancel unfinished TTID/TTFD spans, e.g this might happen if the user navigates
// away from the current route before TTFD or TTID is finished.
for (final child in (transaction as SentryTracer).children) {
if (child.finished) continue;

final isTTIDSpan = child.context.operation ==
SentrySpanOperations.uiTimeToInitialDisplay;
final isTTFDSpan =
child.context.operation == SentrySpanOperations.uiTimeToFullDisplay;
if (!child.finished && (isTTIDSpan || isTTFDSpan)) {
if (isTTIDSpan || isTTFDSpan) {
final finishTimestamp = isTTFDSpan
? (ttidEndTimestampProvider() ?? endTimestamp)
: endTimestamp;
await child.finish(
endTimestamp: endTimestamp,
endTimestamp: finishTimestamp,
status: SpanStatus.deadlineExceeded(),
);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ class TimeToFullDisplayTracker {
final options = Sentry.currentHub.options;

// End timestamp provider is only needed when the TTFD timeout is triggered
EndTimestampProvider _endTimestampProvider = ttidEndTimestampProvider();
EndTimestampProvider _endTimestampProvider = ttidEndTimestampProvider;
Completer<void> _completedTTFDTracking = Completer<void>();

Future<void> track({
Expand Down Expand Up @@ -115,5 +115,5 @@ class TimeToFullDisplayTracker {
typedef EndTimestampProvider = DateTime? Function();

@internal
EndTimestampProvider ttidEndTimestampProvider() =>
EndTimestampProvider ttidEndTimestampProvider =
() => TimeToInitialDisplayTracker().endTimestamp;
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@ class Fixture {
final options = defaultTestOptions()
..dsn = fakeDsn
..tracesSampleRate = 1.0;
late final endTimeProvider = ttidEndTimestampProvider();
late final endTimeProvider = ttidEndTimestampProvider;
late final hub = Hub(options);

TimeToInitialDisplayTracker? ttidTracker;
Expand Down
80 changes: 77 additions & 3 deletions flutter/test/sentry_navigator_observer_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -380,6 +380,83 @@ void main() {
.called(1);
});

// e.g when a user navigates to another screen before ttfd or ttid is finished
test('cancelled TTID and TTFD spans do not add measurements', () async {
final initialRoute = route(RouteSettings(name: 'Initial Route'));
final newRoute = route(RouteSettings(name: 'New Route'));

final hub = _MockHub();
final transaction = getMockSentryTracer(finished: false) as SentryTracer;

final mockChildTTID = MockSentrySpan();
final mockChildTTFD = MockSentrySpan();

when(transaction.children).thenReturn([
mockChildTTID,
mockChildTTFD,
]);

when(transaction.measurements).thenReturn(<String, SentryMeasurement>{});

when(mockChildTTID.finished).thenReturn(false);
when(mockChildTTID.context).thenReturn(SentrySpanContext(
operation: SentrySpanOperations.uiTimeToInitialDisplay));
when(mockChildTTID.status).thenReturn(SpanStatus.cancelled());

when(mockChildTTFD.finished).thenReturn(false);
when(mockChildTTFD.context).thenReturn(SentrySpanContext(
operation: SentrySpanOperations.uiTimeToFullDisplay));
when(mockChildTTFD.status).thenReturn(SpanStatus.cancelled());

when(transaction.context)
.thenReturn(SentrySpanContext(operation: 'navigation'));
when(transaction.status).thenReturn(null);
when(transaction.finished).thenReturn(false);

when(transaction.startChild(
'ui.load.initial_display',
description: anyNamed('description'),
startTimestamp: anyNamed('startTimestamp'),
)).thenReturn(MockSentrySpan());

when(hub.getSpan()).thenReturn(transaction);
when(hub.startTransactionWithContext(
any,
startTimestamp: anyNamed('startTimestamp'),
waitForChildren: true,
autoFinishAfter: anyNamed('autoFinishAfter'),
trimEnd: true,
onFinish: anyNamed('onFinish'),
)).thenReturn(transaction);

final sut =
fixture.getSut(hub: hub, autoFinishAfter: Duration(seconds: 5));

// Simulate pushing the initial route
sut.didPush(initialRoute, null);

// Simulate navigating to a new route before TTID and TTFD spans finish
sut.didPush(newRoute, initialRoute);

// Allow async operations to complete
await Future<void>.delayed(const Duration(milliseconds: 100));

// Verify that the TTID and TTFD spans are finished with a cancelled status
verify(mockChildTTID.finish(
endTimestamp: anyNamed('endTimestamp'),
status: SpanStatus.deadlineExceeded()))
.called(1);
verify(mockChildTTFD.finish(
endTimestamp: anyNamed('endTimestamp'),
status: SpanStatus.deadlineExceeded()))
.called(1);

// Verify that the measurements are not added to the transaction
final measurements = transaction.measurements;
expect(measurements.containsKey('time_to_initial_display'), isFalse);
expect(measurements.containsKey('time_to_full_display'), isFalse);
});

test(
'unfinished children will be finished with deadline_exceeded on didPush',
() async {
Expand Down Expand Up @@ -409,10 +486,7 @@ void main() {

final sut = fixture.getSut(hub: hub);

// Push to new screen, e.g app start / root screen
sut.didPush(currentRoute, null);

// Push to screen e.g root to user screen
sut.didPush(currentRoute, null);

await Future<void>.delayed(const Duration(milliseconds: 100));
Expand Down
Loading