Skip to content

Commit

Permalink
Apply beforeBreadcrumb on native iOS crumbs (#1914)
Browse files Browse the repository at this point in the history
* apply beforeBreadcrumb on native iOS crumbs

---------

Co-authored-by: Giancarlo Buenaflor <giancarlo_buenaflor@yahoo.com>
  • Loading branch information
denrase and buenaflor authored Mar 7, 2024
1 parent bffc2c5 commit 1d9ee98
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
### Features

- Use `recordHttpBreadcrumbs` to set iOS `enableNetworkBreadcrumbs` ([#1884](https://github.com/getsentry/sentry-dart/pull/1884))
- Apply `beforeBreadcrumb` on native iOS crumbs ([#1914](https://github.com/getsentry/sentry-dart/pull/1914))
- Add `maxQueueSize` to limit the number of unawaited events sent to Sentry ([#1868](https://github.com/getsentry/sentry-dart/pull/1868))

### Improvements
Expand Down
12 changes: 11 additions & 1 deletion flutter/lib/src/integrations/load_contexts_integration.dart
Original file line number Diff line number Diff line change
Expand Up @@ -155,13 +155,23 @@ class _LoadContextsIntegrationEventProcessor implements EventProcessor {
final breadcrumbsJson =
List<Map<dynamic, dynamic>>.from(breadcrumbsList);
final breadcrumbs = <Breadcrumb>[];
final beforeBreadcrumb = _options.beforeBreadcrumb;

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

if (beforeBreadcrumb != null) {
final processedBreadcrumb = beforeBreadcrumb(breadcrumb);
if (processedBreadcrumb != null) {
breadcrumbs.add(processedBreadcrumb);
}
} else {
breadcrumbs.add(breadcrumb);
}
}

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

Expand Down
36 changes: 36 additions & 0 deletions flutter/test/integrations/load_contexts_integration_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,42 @@ void main() {
expect(event.breadcrumbs!.length, 1);
expect(event.breadcrumbs!.first.message, 'event');
});

test('apply beforeBreadcrumb to native breadcrumbs', () async {
fixture.options.enableScopeSync = true;
fixture.options.beforeBreadcrumb = (breadcrumb, {hint}) {
if (breadcrumb?.message == 'native-mutated') {
return breadcrumb?.copyWith(message: 'native-mutated-applied');
} else {
return null;
}
};

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

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

final future = Future.value(loadContexts);
when(fixture.methodChannel.invokeMethod<dynamic>('loadContexts'))
.thenAnswer((_) => future);
// ignore: deprecated_member_use
_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-mutated-applied');
});
});
}

Expand Down

0 comments on commit 1d9ee98

Please sign in to comment.