Skip to content

Commit 4d763a5

Browse files
authored
fix(ttfd): measurements should only be added for successful ttfd (#2348)
* fix * update * update * update
1 parent 8da6ae0 commit 4d763a5

File tree

3 files changed

+27
-9
lines changed

3 files changed

+27
-9
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@
6868
- Start missing TTFD for root screen transaction ([#2332](https://github.com/getsentry/sentry-dart/pull/2332))
6969
- Accessing invalid json fields from `fetchNativeAppStart` should return null ([#2340](https://github.com/getsentry/sentry-dart/pull/2340))
7070
- Error when calling `SentryFlutter.reportFullyDisplayed()` twice ([#2339](https://github.com/getsentry/sentry-dart/pull/2339))
71+
- TTFD measurements should only be added for successful TTFD spans ([#2348](https://github.com/getsentry/sentry-dart/pull/2348))
7172

7273
### Deprecate
7374

flutter/lib/src/navigation/time_to_full_display_tracker.dart

Lines changed: 24 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ class TimeToFullDisplayTracker {
7171
final endTimestamp = timestamp ?? _endTimestampProvider();
7272

7373
if (ttfdSpan == null ||
74-
ttfdSpan.finished == true ||
74+
ttfdSpan.finished ||
7575
startTimestamp == null ||
7676
endTimestamp == null) {
7777
options.logger(
@@ -83,14 +83,29 @@ class TimeToFullDisplayTracker {
8383
return;
8484
}
8585

86-
_setTTFDMeasurement(startTimestamp, endTimestamp);
87-
await ttfdSpan.finish(
88-
status:
89-
timestamp != null ? SpanStatus.ok() : SpanStatus.deadlineExceeded(),
90-
endTimestamp: endTimestamp,
91-
);
92-
_completedTTFDTracking.complete();
93-
clear();
86+
// If a timestamp is provided, the operation was successful; otherwise, it timed out
87+
final status =
88+
timestamp != null ? SpanStatus.ok() : SpanStatus.deadlineExceeded();
89+
try {
90+
// Should only add measurements if the span is successful
91+
if (status == SpanStatus.ok()) {
92+
_setTTFDMeasurement(startTimestamp, endTimestamp);
93+
}
94+
await ttfdSpan.finish(
95+
status: status,
96+
endTimestamp: endTimestamp,
97+
);
98+
} catch (e, stackTrace) {
99+
options.logger(
100+
SentryLevel.error,
101+
'Failed to finish TTFD span',
102+
exception: e,
103+
stackTrace: stackTrace,
104+
);
105+
} finally {
106+
_completedTTFDTracking.complete();
107+
clear();
108+
}
94109
}
95110

96111
void _setTTFDMeasurement(DateTime startTimestamp, DateTime endTimestamp) {

flutter/test/navigation/time_to_full_display_tracker_test.dart

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ void main() {
4444
final differenceInSeconds =
4545
actualEndTimestamp.difference(expectedEndTimestamp).inSeconds.abs();
4646
expect(differenceInSeconds, lessThanOrEqualTo(1));
47+
expect(transaction.measurements, isNotEmpty);
4748
});
4849

4950
test(
@@ -66,6 +67,7 @@ void main() {
6667
expect(ttfdSpan.status, equals(SpanStatus.deadlineExceeded()));
6768
expect(ttfdSpan.context.description, equals('Current route full display'));
6869
expect(ttfdSpan.origin, equals(SentryTraceOrigins.manualUiTimeToDisplay));
70+
expect(transaction.measurements, isEmpty);
6971
});
7072

7173
test('finishing ttfd twice does not throw', () async {

0 commit comments

Comments
 (0)