Skip to content

fix: transaction timestamp trimming #1916

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

Merged
merged 8 commits into from
Mar 6, 2024
Merged
Show file tree
Hide file tree
Changes from 5 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
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@

## Unreleased

### Fixes

- Fix transaction end timestamp trimming ([#1916](https://github.com/getsentry/sentry-dart/pull/1916))
- Transaction end timestamps are now correctly trimmed to the latest child span end timestamp

### Features

- Use `recordHttpBreadcrumbs` to set iOS `enableNetworkBreadcrumbs` ([#1884](https://github.com/getsentry/sentry-dart/pull/1884))
Expand Down
24 changes: 15 additions & 9 deletions dart/lib/src/sentry_tracer.dart
Original file line number Diff line number Diff line change
Expand Up @@ -109,18 +109,24 @@ class SentryTracer extends ISentrySpan {
}

var _rootEndTimestamp = commonEndTimestamp;

// Trim the end timestamp of the transaction to the very last timestamp of child spans
if (_trimEnd && children.isNotEmpty) {
final childEndTimestamps = children
.where((child) => child.endTimestamp != null)
.map((child) => child.endTimestamp!);

if (childEndTimestamps.isNotEmpty) {
final oldestChildEndTimestamp =
childEndTimestamps.reduce((a, b) => a.isAfter(b) ? a : b);
if (_rootEndTimestamp.isAfter(oldestChildEndTimestamp)) {
_rootEndTimestamp = oldestChildEndTimestamp;
DateTime? latestEndTime;

for (var child in children) {
final childEndTimestamp = child.endTimestamp;
if (childEndTimestamp != null) {
if (latestEndTime == null ||
childEndTimestamp.isAfter(latestEndTime)) {
latestEndTime = child.endTimestamp;
}
}
}

if (latestEndTime != null) {
_rootEndTimestamp = latestEndTime;
}
}

// the callback should run before because if the span is finished,
Expand Down
22 changes: 22 additions & 0 deletions dart/test/sentry_tracer_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -386,6 +386,28 @@ void main() {
expect(sut.endTimestamp, endTimestamp);
});

test('end trimmed to latest child end timestamp', () async {
final sut = fixture.getSut(trimEnd: true);
final rootEndInitial = getUtcDateTime();
final childEnd1 = rootEndInitial;
final childEnd2 = rootEndInitial.add(Duration(seconds: 1));
final childEnd3 = rootEndInitial;

final childA = sut.startChild('operation-a', description: 'description');
final childB = sut.startChild('operation-b', description: 'description');
final childC = sut.startChild('operation-c', description: 'description');

await childA.finish(endTimestamp: childEnd1);
await childB.finish(endTimestamp: childEnd2);
await childC.finish(endTimestamp: childEnd3);

await sut.finish(endTimestamp: rootEndInitial);

expect(sut.endTimestamp, equals(childB.endTimestamp),
reason:
'The root end timestamp should be updated to match the latest child end timestamp.');
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Excellent descriptive error message 👍

});

test('does not add more spans than configured in options', () async {
fixture.hub.options.maxSpans = 2;
final sut = fixture.getSut();
Expand Down