Skip to content

Commit

Permalink
add ignoreRoutes parameter to SentryNavigatorObserver (#2218)
Browse files Browse the repository at this point in the history
* 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 <giancarlo_buenaflor@yahoo.com>

* add further tests for ignore routes

* fix changelog and move ignoreRoutes to unreleased

---------

Co-authored-by: Giancarlo Buenaflor <giancarlo_buenaflor@yahoo.com>
  • Loading branch information
martinhaintz and buenaflor committed Aug 12, 2024
1 parent 03e4c9b commit 178baee
Show file tree
Hide file tree
Showing 3 changed files with 96 additions and 0 deletions.
11 changes: 11 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,16 @@
# Changelog

## Unreleased

### Features

- Add `ignoreRoutes` parameter to `SentryNavigatorObserver`. ([#2218](https://github.com/getsentry/sentry-dart/pull/2218))
- This will ignore the Routes and prevent the Route from being pushed to the Sentry server.
- Ignored routes will also create no TTID and TTFD spans.
```dart
SentryNavigatorObserver(ignoreRoutes: ["/ignoreThisRoute"]),
```

## 8.7.0

### Features
Expand Down
23 changes: 23 additions & 0 deletions flutter/lib/src/navigation/sentry_navigator_observer.dart
Original file line number Diff line number Diff line change
Expand Up @@ -80,12 +80,14 @@ class SentryNavigatorObserver extends RouteObserver<PageRoute<dynamic>> {
RouteNameExtractor? routeNameExtractor,
AdditionalInfoExtractor? additionalInfoProvider,
@visibleForTesting TimeToDisplayTracker? timeToDisplayTracker,
List<String>? ignoreRoutes,
}) : _hub = hub ?? HubAdapter(),
_enableAutoTransactions = enableAutoTransactions,
_autoFinishAfter = autoFinishAfter,
_setRouteNameAsTransaction = setRouteNameAsTransaction,
_routeNameExtractor = routeNameExtractor,
_additionalInfoProvider = additionalInfoProvider,
_ignoreRoutes = ignoreRoutes ?? [],
_native = SentryFlutter.native {
_isCreated = true;
if (enableAutoTransactions) {
Expand Down Expand Up @@ -113,6 +115,7 @@ class SentryNavigatorObserver extends RouteObserver<PageRoute<dynamic>> {
final RouteNameExtractor? _routeNameExtractor;
final AdditionalInfoExtractor? _additionalInfoProvider;
final SentryNativeBinding? _native;
final List<String> _ignoreRoutes;
static TimeToDisplayTracker? _timeToDisplayTracker;

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

if (_isRouteIgnored(route) ||
previousRoute != null && _isRouteIgnored(previousRoute)) {
return;
}

_setCurrentRouteName(route);
_setCurrentRouteNameAsTransaction(route);

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

if (newRoute != null && _isRouteIgnored(newRoute) ||
oldRoute != null && _isRouteIgnored(oldRoute)) {
return;
}

_setCurrentRouteName(newRoute);
_setCurrentRouteNameAsTransaction(newRoute);

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

if (_isRouteIgnored(route) ||
previousRoute != null && _isRouteIgnored(previousRoute)) {
return;
}

_setCurrentRouteName(previousRoute);
_setCurrentRouteNameAsTransaction(previousRoute);

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

@internal
static const String rootScreenName = 'root /';

bool _isRouteIgnored(Route<dynamic> route) {
return _ignoreRoutes.isNotEmpty &&
_ignoreRoutes.contains(_getRouteName(route));
}
}

/// This class makes it easier to record breadcrumbs for events of Flutters
Expand Down
62 changes: 62 additions & 0 deletions flutter/test/sentry_navigator_observer_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -975,6 +975,66 @@ void main() {
observer.didReplace(newRoute: route(to), oldRoute: route(previous));
expect(hub.scope.transaction, 'to_test');
});

test('ignores Route and prevents recognition of this route for didPush',
() async {
final firstRoute = route(RouteSettings(name: 'default'));
final secondRoute = route(RouteSettings(name: 'testRoute'));

final hub = _MockHub();

final sut = fixture.getSut(hub: hub, ignoreRoutes: ["testRoute"]);

sut.didPush(firstRoute, null);
expect(
SentryNavigatorObserver.currentRouteName, firstRoute.settings.name);
sut.didPush(secondRoute, firstRoute);
expect(
SentryNavigatorObserver.currentRouteName, firstRoute.settings.name);
sut.didPush(firstRoute, secondRoute);
expect(
SentryNavigatorObserver.currentRouteName, firstRoute.settings.name);
});

test('ignores Route and prevents recognition of this route for didPop',
() async {
final firstRoute = route(RouteSettings(name: 'default'));
final secondRoute = route(RouteSettings(name: 'testRoute'));

final hub = _MockHub();

final sut = fixture.getSut(hub: hub, ignoreRoutes: ["testRoute"]);

sut.didPush(firstRoute, null);
expect(
SentryNavigatorObserver.currentRouteName, firstRoute.settings.name);
sut.didPush(secondRoute, firstRoute);
expect(
SentryNavigatorObserver.currentRouteName, firstRoute.settings.name);
sut.didPop(firstRoute, secondRoute);
expect(
SentryNavigatorObserver.currentRouteName, firstRoute.settings.name);
});

test('ignores Route and prevents recognition of this route for didReplace',
() async {
final firstRoute = route(RouteSettings(name: 'default'));
final secondRoute = route(RouteSettings(name: 'testRoute'));

final hub = _MockHub();

final sut = fixture.getSut(hub: hub, ignoreRoutes: ["testRoute"]);

sut.didReplace(newRoute: firstRoute);
expect(
SentryNavigatorObserver.currentRouteName, firstRoute.settings.name);
sut.didReplace(newRoute: secondRoute, oldRoute: firstRoute);
expect(
SentryNavigatorObserver.currentRouteName, firstRoute.settings.name);
sut.didReplace(newRoute: firstRoute, oldRoute: secondRoute);
expect(
SentryNavigatorObserver.currentRouteName, firstRoute.settings.name);
});
});
}

Expand All @@ -987,6 +1047,7 @@ class Fixture {
RouteNameExtractor? routeNameExtractor,
AdditionalInfoExtractor? additionalInfoProvider,
bool enableTimeToFullDisplayTracing = false,
List<String>? ignoreRoutes,
}) {
final frameCallbackHandler = FakeFrameCallbackHandler();
final timeToInitialDisplayTracker =
Expand All @@ -1003,6 +1064,7 @@ class Fixture {
routeNameExtractor: routeNameExtractor,
additionalInfoProvider: additionalInfoProvider,
timeToDisplayTracker: timeToDisplayTracker,
ignoreRoutes: ignoreRoutes,
);
}

Expand Down

0 comments on commit 178baee

Please sign in to comment.