Skip to content

Commit 178baee

Browse files
add ignoreRoutes parameter to SentryNavigatorObserver (#2218)
* add ignoreRoutes parameter to SentryNavigatorObserver * add unitTest for ignoreRoutes * add changelog entry for ignoreRoutes in the SentryNaviagtorObserver * add for ignore routes, that not TTID and TTFD spans are created Co-authored-by: Giancarlo Buenaflor <[email protected]> * add further tests for ignore routes * fix changelog and move ignoreRoutes to unreleased --------- Co-authored-by: Giancarlo Buenaflor <[email protected]>
1 parent 03e4c9b commit 178baee

File tree

3 files changed

+96
-0
lines changed

3 files changed

+96
-0
lines changed

CHANGELOG.md

+11
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,16 @@
11
# Changelog
22

3+
## Unreleased
4+
5+
### Features
6+
7+
- Add `ignoreRoutes` parameter to `SentryNavigatorObserver`. ([#2218](https://github.com/getsentry/sentry-dart/pull/2218))
8+
- This will ignore the Routes and prevent the Route from being pushed to the Sentry server.
9+
- Ignored routes will also create no TTID and TTFD spans.
10+
```dart
11+
SentryNavigatorObserver(ignoreRoutes: ["/ignoreThisRoute"]),
12+
```
13+
314
## 8.7.0
415

516
### Features

flutter/lib/src/navigation/sentry_navigator_observer.dart

+23
Original file line numberDiff line numberDiff line change
@@ -80,12 +80,14 @@ class SentryNavigatorObserver extends RouteObserver<PageRoute<dynamic>> {
8080
RouteNameExtractor? routeNameExtractor,
8181
AdditionalInfoExtractor? additionalInfoProvider,
8282
@visibleForTesting TimeToDisplayTracker? timeToDisplayTracker,
83+
List<String>? ignoreRoutes,
8384
}) : _hub = hub ?? HubAdapter(),
8485
_enableAutoTransactions = enableAutoTransactions,
8586
_autoFinishAfter = autoFinishAfter,
8687
_setRouteNameAsTransaction = setRouteNameAsTransaction,
8788
_routeNameExtractor = routeNameExtractor,
8889
_additionalInfoProvider = additionalInfoProvider,
90+
_ignoreRoutes = ignoreRoutes ?? [],
8991
_native = SentryFlutter.native {
9092
_isCreated = true;
9193
if (enableAutoTransactions) {
@@ -113,6 +115,7 @@ class SentryNavigatorObserver extends RouteObserver<PageRoute<dynamic>> {
113115
final RouteNameExtractor? _routeNameExtractor;
114116
final AdditionalInfoExtractor? _additionalInfoProvider;
115117
final SentryNativeBinding? _native;
118+
final List<String> _ignoreRoutes;
116119
static TimeToDisplayTracker? _timeToDisplayTracker;
117120

118121
@internal
@@ -141,6 +144,11 @@ class SentryNavigatorObserver extends RouteObserver<PageRoute<dynamic>> {
141144
void didPush(Route<dynamic> route, Route<dynamic>? previousRoute) {
142145
super.didPush(route, previousRoute);
143146

147+
if (_isRouteIgnored(route) ||
148+
previousRoute != null && _isRouteIgnored(previousRoute)) {
149+
return;
150+
}
151+
144152
_setCurrentRouteName(route);
145153
_setCurrentRouteNameAsTransaction(route);
146154

@@ -160,6 +168,11 @@ class SentryNavigatorObserver extends RouteObserver<PageRoute<dynamic>> {
160168
void didReplace({Route<dynamic>? newRoute, Route<dynamic>? oldRoute}) {
161169
super.didReplace(newRoute: newRoute, oldRoute: oldRoute);
162170

171+
if (newRoute != null && _isRouteIgnored(newRoute) ||
172+
oldRoute != null && _isRouteIgnored(oldRoute)) {
173+
return;
174+
}
175+
163176
_setCurrentRouteName(newRoute);
164177
_setCurrentRouteNameAsTransaction(newRoute);
165178

@@ -174,6 +187,11 @@ class SentryNavigatorObserver extends RouteObserver<PageRoute<dynamic>> {
174187
void didPop(Route<dynamic> route, Route<dynamic>? previousRoute) {
175188
super.didPop(route, previousRoute);
176189

190+
if (_isRouteIgnored(route) ||
191+
previousRoute != null && _isRouteIgnored(previousRoute)) {
192+
return;
193+
}
194+
177195
_setCurrentRouteName(previousRoute);
178196
_setCurrentRouteNameAsTransaction(previousRoute);
179197

@@ -376,6 +394,11 @@ class SentryNavigatorObserver extends RouteObserver<PageRoute<dynamic>> {
376394

377395
@internal
378396
static const String rootScreenName = 'root /';
397+
398+
bool _isRouteIgnored(Route<dynamic> route) {
399+
return _ignoreRoutes.isNotEmpty &&
400+
_ignoreRoutes.contains(_getRouteName(route));
401+
}
379402
}
380403

381404
/// This class makes it easier to record breadcrumbs for events of Flutters

flutter/test/sentry_navigator_observer_test.dart

+62
Original file line numberDiff line numberDiff line change
@@ -975,6 +975,66 @@ void main() {
975975
observer.didReplace(newRoute: route(to), oldRoute: route(previous));
976976
expect(hub.scope.transaction, 'to_test');
977977
});
978+
979+
test('ignores Route and prevents recognition of this route for didPush',
980+
() async {
981+
final firstRoute = route(RouteSettings(name: 'default'));
982+
final secondRoute = route(RouteSettings(name: 'testRoute'));
983+
984+
final hub = _MockHub();
985+
986+
final sut = fixture.getSut(hub: hub, ignoreRoutes: ["testRoute"]);
987+
988+
sut.didPush(firstRoute, null);
989+
expect(
990+
SentryNavigatorObserver.currentRouteName, firstRoute.settings.name);
991+
sut.didPush(secondRoute, firstRoute);
992+
expect(
993+
SentryNavigatorObserver.currentRouteName, firstRoute.settings.name);
994+
sut.didPush(firstRoute, secondRoute);
995+
expect(
996+
SentryNavigatorObserver.currentRouteName, firstRoute.settings.name);
997+
});
998+
999+
test('ignores Route and prevents recognition of this route for didPop',
1000+
() async {
1001+
final firstRoute = route(RouteSettings(name: 'default'));
1002+
final secondRoute = route(RouteSettings(name: 'testRoute'));
1003+
1004+
final hub = _MockHub();
1005+
1006+
final sut = fixture.getSut(hub: hub, ignoreRoutes: ["testRoute"]);
1007+
1008+
sut.didPush(firstRoute, null);
1009+
expect(
1010+
SentryNavigatorObserver.currentRouteName, firstRoute.settings.name);
1011+
sut.didPush(secondRoute, firstRoute);
1012+
expect(
1013+
SentryNavigatorObserver.currentRouteName, firstRoute.settings.name);
1014+
sut.didPop(firstRoute, secondRoute);
1015+
expect(
1016+
SentryNavigatorObserver.currentRouteName, firstRoute.settings.name);
1017+
});
1018+
1019+
test('ignores Route and prevents recognition of this route for didReplace',
1020+
() async {
1021+
final firstRoute = route(RouteSettings(name: 'default'));
1022+
final secondRoute = route(RouteSettings(name: 'testRoute'));
1023+
1024+
final hub = _MockHub();
1025+
1026+
final sut = fixture.getSut(hub: hub, ignoreRoutes: ["testRoute"]);
1027+
1028+
sut.didReplace(newRoute: firstRoute);
1029+
expect(
1030+
SentryNavigatorObserver.currentRouteName, firstRoute.settings.name);
1031+
sut.didReplace(newRoute: secondRoute, oldRoute: firstRoute);
1032+
expect(
1033+
SentryNavigatorObserver.currentRouteName, firstRoute.settings.name);
1034+
sut.didReplace(newRoute: firstRoute, oldRoute: secondRoute);
1035+
expect(
1036+
SentryNavigatorObserver.currentRouteName, firstRoute.settings.name);
1037+
});
9781038
});
9791039
}
9801040

@@ -987,6 +1047,7 @@ class Fixture {
9871047
RouteNameExtractor? routeNameExtractor,
9881048
AdditionalInfoExtractor? additionalInfoProvider,
9891049
bool enableTimeToFullDisplayTracing = false,
1050+
List<String>? ignoreRoutes,
9901051
}) {
9911052
final frameCallbackHandler = FakeFrameCallbackHandler();
9921053
final timeToInitialDisplayTracker =
@@ -1003,6 +1064,7 @@ class Fixture {
10031064
routeNameExtractor: routeNameExtractor,
10041065
additionalInfoProvider: additionalInfoProvider,
10051066
timeToDisplayTracker: timeToDisplayTracker,
1067+
ignoreRoutes: ignoreRoutes,
10061068
);
10071069
}
10081070

0 commit comments

Comments
 (0)