Skip to content

Commit

Permalink
sett event.contexts.app.screen to navigator observers current route
Browse files Browse the repository at this point in the history
  • Loading branch information
denrase committed Jul 11, 2023
1 parent 9ca5625 commit 1fa6515
Show file tree
Hide file tree
Showing 5 changed files with 89 additions and 10 deletions.
19 changes: 9 additions & 10 deletions dart/test/protocol/sentry_app_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,15 @@ void main() {
final testStartTime = DateTime.fromMicrosecondsSinceEpoch(0);

final sentryApp = SentryApp(
name: 'fixture-name',
version: 'fixture-version',
identifier: 'fixture-identifier',
build: 'fixture-build',
buildType: 'fixture-buildType',
startTime: testStartTime,
deviceAppHash: 'fixture-deviceAppHash',
inForeground: true,
screen: 'fixture-screen'
);
name: 'fixture-name',
version: 'fixture-version',
identifier: 'fixture-identifier',
build: 'fixture-build',
buildType: 'fixture-buildType',
startTime: testStartTime,
deviceAppHash: 'fixture-deviceAppHash',
inForeground: true,
screen: 'fixture-screen');

final sentryAppJson = <String, dynamic>{
'app_name': 'fixture-name',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:sentry/sentry.dart';

import '../navigation/sentry_navigator_observer.dart';
import '../sentry_flutter_options.dart';

typedef WidgetBindingGetter = WidgetsBinding? Function();
Expand Down Expand Up @@ -47,6 +48,11 @@ class FlutterEnricherEventProcessor implements EventProcessor {
app: _getApp(event.contexts.app),
);

final app = contexts.app;
if (app != null) {
_getAppScreen(contexts, app);
}

// Flutter has a lot of Accessibility Settings available and exposes them
contexts['accessibility'] = _getAccessibilityContext();

Expand Down Expand Up @@ -237,4 +243,11 @@ class FlutterEnricherEventProcessor implements EventProcessor {
inForeground: inForeground,
);
}

void _getAppScreen(Contexts contexts, SentryApp app) {
final currentRouteName = SentryNavigatorObserver.currentRouteName;
if (currentRouteName != null) {
contexts.app = app.copyWith(screen: currentRouteName);
}
}
}
6 changes: 6 additions & 0 deletions flutter/lib/src/navigation/sentry_navigator_observer.dart
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,11 @@ class SentryNavigatorObserver extends RouteObserver<PageRoute<dynamic>> {

ISentrySpan? _transaction;

static String? _currentRouteName;

/// Get the current route of the observer.
static String? get currentRouteName => _currentRouteName;

@override
void didPush(Route<dynamic> route, Route<dynamic>? previousRoute) {
super.didPush(route, previousRoute);
Expand Down Expand Up @@ -201,6 +206,7 @@ class SentryNavigatorObserver extends RouteObserver<PageRoute<dynamic>> {
}
},
);
_currentRouteName = name;

// if _enableAutoTransactions is enabled but there's no traces sample rate
if (_transaction is NoOpSentrySpan) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -322,6 +322,42 @@ void main() {
.length;
expect(ioEnricherCount, 1);
});

testWidgets('adds SentryNavigatorObserver.currentRouteName as app.screen',
(tester) async {
final observer = SentryNavigatorObserver();
final route =
fixture.route(RouteSettings(name: 'fixture-currentRouteName'));
observer.didPush(route, null);

final eventWithContextsApp =
SentryEvent(contexts: Contexts(app: SentryApp()));

final enricher = fixture.getSut(
binding: () => tester.binding,
);
final event = await enricher.apply(eventWithContextsApp);

expect(event?.contexts.app?.screen, 'fixture-currentRouteName');
});

testWidgets(
'does not add SentryNavigatorObserver.currentRouteName if no app',
(tester) async {
final observer = SentryNavigatorObserver();
final route =
fixture.route(RouteSettings(name: 'fixture-currentRouteName'));
observer.didPush(route, null);

final eventWithoutContextsApp = SentryEvent(contexts: Contexts());

final enricher = fixture.getSut(
binding: () => tester.binding,
);
final event = await enricher.apply(eventWithoutContextsApp);

expect(event?.contexts.app?.screen, isNull);
});
});
}

Expand All @@ -342,6 +378,11 @@ class Fixture {
)..reportPackages = reportPackages;
return FlutterEnricherEventProcessor(options);
}

PageRoute<dynamic> route(RouteSettings? settings) => PageRouteBuilder<void>(
pageBuilder: (_, __, ___) => Container(),
settings: settings,
);
}

void loadTestPackage() {
Expand Down
20 changes: 20 additions & 0 deletions flutter/test/sentry_navigator_observer_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -368,6 +368,26 @@ void main() {
expect(scope.span, span);
});
});

test('exposes current route name', () {
const name = 'Current Route';
final currentRoute = route(RouteSettings(name: name));

const op = 'navigation';
final hub = _MockHub();
final span = getMockSentryTracer(name: name);
when(span.context).thenReturn(SentrySpanContext(operation: op));
_whenAnyStart(hub, span);

final sut = fixture.getSut(
hub: hub,
autoFinishAfter: Duration(seconds: 5),
);

sut.didPush(currentRoute, null);

expect(SentryNavigatorObserver.currentRouteName, 'Current Route');
});
});

group('RouteObserverBreadcrumb', () {
Expand Down

0 comments on commit 1fa6515

Please sign in to comment.