Skip to content

add ignoreRoutes parameter to SentryNavigatorObserver #2218

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

Merged
merged 8 commits into from
Aug 12, 2024
Merged
Show file tree
Hide file tree
Changes from 5 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
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@

### 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.
```dart
SentryNavigatorObserver(ignoreRoutes: ["/ignoreThisRoute"]),
```
- Add support for span level measurements. ([#2214](https://github.com/getsentry/sentry-dart/pull/2214))
- Add `ignoreTransactions` and `ignoreErrors` to options ([#2207](https://github.com/getsentry/sentry-dart/pull/2207))
```dart
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
21 changes: 21 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,25 @@ 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', () 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);
});
});
}

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

Expand Down
Loading