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

add ignoreRoutes parameter to SentryNavigatorObserver #2218

Merged
merged 8 commits into from
Aug 12, 2024
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:/getsentry/sentry-dart/pull/2218))
martinhaintz marked this conversation as resolved.
Show resolved Hide resolved
- 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);
martinhaintz marked this conversation as resolved.
Show resolved Hide resolved
});

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
Loading