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

Remove duplicated breadcrumbs when syncing with iOS/macOS #1283

Merged
merged 16 commits into from
Feb 28, 2023
Merged
Show file tree
Hide file tree
Changes from all 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
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@
- [changelog](https://github.com/getsentry/sentry-java/blob/main/CHANGELOG.md#6131)
- [diff](https://github.com/getsentry/sentry-java/compare/6.13.0...6.13.1)

### Fixes

- Remove duplicated breadcrumbs when syncing with iOS/macOS ([#1283](https://github.com/getsentry/sentry-dart/pull/1283))

## 6.20.1

### Fixes
Expand Down
27 changes: 27 additions & 0 deletions flutter/ios/Classes/SentryFlutterPluginApple.swift
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,28 @@ public class SentryFlutterPluginApple: NSObject, FlutterPlugin {

}

private lazy var iso8601Formatter: DateFormatter = {
let formatter = DateFormatter()
formatter.locale = Locale(identifier: "en_US_POSIX")
formatter.timeZone = TimeZone(abbreviation: "UTC")
formatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ss'Z'"
return formatter
}()

private lazy var iso8601FormatterWithMillisecondPrecision: DateFormatter = {
let formatter = DateFormatter()
formatter.locale = Locale(identifier: "en_US_POSIX")
formatter.timeZone = TimeZone(abbreviation: "UTC")
formatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'"
return formatter
}()

// Replace with `NSDate+SentryExtras` when available.
private func dateFrom(iso8601String: String) -> Date? {
return iso8601FormatterWithMillisecondPrecision.date(from: iso8601String)
?? iso8601Formatter.date(from: iso8601String) // Parse date with low precision formatter for backward compatible
}

// swiftlint:disable:next cyclomatic_complexity
public func handle(_ call: FlutterMethodCall, result: @escaping FlutterResult) {
switch call.method as String {
Expand Down Expand Up @@ -581,6 +603,11 @@ public class SentryFlutterPluginApple: NSObject, FlutterPlugin {
breadcrumbInstance.data = data
}

if let timestampValue = breadcrumb["timestamp"] as? String,
let timestamp = dateFrom(iso8601String: timestampValue) {
breadcrumbInstance.timestamp = timestamp
}

SentrySDK.addBreadcrumb(crumb: breadcrumbInstance)

result("")
Expand Down
22 changes: 10 additions & 12 deletions flutter/lib/src/integrations/load_contexts_integration.dart
Original file line number Diff line number Diff line change
Expand Up @@ -137,21 +137,19 @@ class _LoadContextsIntegrationEventProcessor extends EventProcessor {
}

final breadcrumbsList = infos['breadcrumbs'] as List?;
if (breadcrumbsList != null && breadcrumbsList.isNotEmpty) {
final breadcrumbs = event.breadcrumbs ?? [];
final newBreadcrumbs =
if (breadcrumbsList != null &&
breadcrumbsList.isNotEmpty &&
_options.enableScopeSync) {
final breadcrumbsJson =
List<Map<dynamic, dynamic>>.from(breadcrumbsList);
final breadcrumbs = <Breadcrumb>[];

for (final breadcrumb in newBreadcrumbs) {
final newBreadcrumb = Map<String, dynamic>.from(breadcrumb);
final crumb = Breadcrumb.fromJson(newBreadcrumb);
breadcrumbs.add(crumb);
for (final breadcrumbJson in breadcrumbsJson) {
final breadcrumb = Breadcrumb.fromJson(
Map<String, dynamic>.from(breadcrumbJson),
);
breadcrumbs.add(breadcrumb);
}

breadcrumbs.sort((a, b) {
return a.timestamp.compareTo(b.timestamp);
});

event = event.copyWith(breadcrumbs: breadcrumbs);
}

Expand Down
50 changes: 50 additions & 0 deletions flutter/test/integrations/load_contexts_integration_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import 'package:flutter/services.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:mockito/mockito.dart';
import 'package:package_info_plus/package_info_plus.dart';
import 'package:sentry_flutter/sentry_flutter.dart';
import 'package:sentry_flutter/src/integrations/load_contexts_integration.dart';
Expand Down Expand Up @@ -36,12 +37,61 @@ void main() {
fixture.options.sdk.integrations.contains('loadContextsIntegration'),
true);
});

test('take breadcrumbs from native if scope sync is enabled', () async {
fixture.options.enableScopeSync = true;

final eventBreadcrumb = Breadcrumb(message: 'event');
var event = SentryEvent(breadcrumbs: [eventBreadcrumb]);

final nativeBreadcrumb = Breadcrumb(message: 'native');
Map<String, dynamic> loadContexts = {
'breadcrumbs': [nativeBreadcrumb.toJson()]
};

final future = Future.value(loadContexts);
when(fixture.methodChannel.invokeMethod<dynamic>('loadContexts'))
.thenAnswer((_) => future);
_channel.setMockMethodCallHandler((MethodCall methodCall) async {});

final integration = LoadContextsIntegration(fixture.methodChannel);
integration.call(fixture.hub, fixture.options);
event = (await fixture.options.eventProcessors.first.apply(event))!;

expect(event.breadcrumbs!.length, 1);
expect(event.breadcrumbs!.first.message, 'native');
});

test('take breadcrumbs from event if scope sync is disabled', () async {
fixture.options.enableScopeSync = false;

final eventBreadcrumb = Breadcrumb(message: 'event');
var event = SentryEvent(breadcrumbs: [eventBreadcrumb]);

final nativeBreadcrumb = Breadcrumb(message: 'native');
Map<String, dynamic> loadContexts = {
'breadcrumbs': [nativeBreadcrumb.toJson()]
};

final future = Future.value(loadContexts);
when(fixture.methodChannel.invokeMethod<dynamic>('loadContexts'))
.thenAnswer((_) => future);
_channel.setMockMethodCallHandler((MethodCall methodCall) async {});

final integration = LoadContextsIntegration(fixture.methodChannel);
integration.call(fixture.hub, fixture.options);
event = (await fixture.options.eventProcessors.first.apply(event))!;

expect(event.breadcrumbs!.length, 1);
expect(event.breadcrumbs!.first.message, 'event');
});
});
}

class Fixture {
final hub = MockHub();
final options = SentryFlutterOptions(dsn: fakeDsn);
final methodChannel = MockMethodChannel();

LoadReleaseIntegration getIntegration({PackageLoader? loader}) {
return LoadReleaseIntegration(loader ?? loadRelease);
Expand Down
16 changes: 0 additions & 16 deletions flutter/test/integrations/load_contexts_integrations_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -405,22 +405,6 @@ void main() {

expect(event?.level, SentryLevel.fatal);
});

test('should merge in breadcrumbs sorted by timestamp', () async {
final integration = fixture.getSut();
integration(fixture.hub, fixture.options);

final breadcrumb = Breadcrumb(
message: 'flutter-crumb',
timestamp: DateTime.fromMillisecondsSinceEpoch(1),
);
final e = getEvent(breadcrumbs: [breadcrumb]);
final event = await fixture.options.eventProcessors.first.apply(e);

expect(event?.breadcrumbs?.length, 2);
expect(event?.breadcrumbs?[0].message, 'native-crumb');
expect(event?.breadcrumbs?[1].message, 'flutter-crumb');
});
}

class Fixture {
Expand Down