Skip to content

Commit 1fa6515

Browse files
committed
sett event.contexts.app.screen to navigator observers current route
1 parent 9ca5625 commit 1fa6515

File tree

5 files changed

+89
-10
lines changed

5 files changed

+89
-10
lines changed

dart/test/protocol/sentry_app_test.dart

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,15 @@ void main() {
66
final testStartTime = DateTime.fromMicrosecondsSinceEpoch(0);
77

88
final sentryApp = SentryApp(
9-
name: 'fixture-name',
10-
version: 'fixture-version',
11-
identifier: 'fixture-identifier',
12-
build: 'fixture-build',
13-
buildType: 'fixture-buildType',
14-
startTime: testStartTime,
15-
deviceAppHash: 'fixture-deviceAppHash',
16-
inForeground: true,
17-
screen: 'fixture-screen'
18-
);
9+
name: 'fixture-name',
10+
version: 'fixture-version',
11+
identifier: 'fixture-identifier',
12+
build: 'fixture-build',
13+
buildType: 'fixture-buildType',
14+
startTime: testStartTime,
15+
deviceAppHash: 'fixture-deviceAppHash',
16+
inForeground: true,
17+
screen: 'fixture-screen');
1918

2019
final sentryAppJson = <String, dynamic>{
2120
'app_name': 'fixture-name',

flutter/lib/src/event_processor/flutter_enricher_event_processor.dart

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import 'package:flutter/foundation.dart';
55
import 'package:flutter/material.dart';
66
import 'package:sentry/sentry.dart';
77

8+
import '../navigation/sentry_navigator_observer.dart';
89
import '../sentry_flutter_options.dart';
910

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

51+
final app = contexts.app;
52+
if (app != null) {
53+
_getAppScreen(contexts, app);
54+
}
55+
5056
// Flutter has a lot of Accessibility Settings available and exposes them
5157
contexts['accessibility'] = _getAccessibilityContext();
5258

@@ -237,4 +243,11 @@ class FlutterEnricherEventProcessor implements EventProcessor {
237243
inForeground: inForeground,
238244
);
239245
}
246+
247+
void _getAppScreen(Contexts contexts, SentryApp app) {
248+
final currentRouteName = SentryNavigatorObserver.currentRouteName;
249+
if (currentRouteName != null) {
250+
contexts.app = app.copyWith(screen: currentRouteName);
251+
}
252+
}
240253
}

flutter/lib/src/navigation/sentry_navigator_observer.dart

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,11 @@ class SentryNavigatorObserver extends RouteObserver<PageRoute<dynamic>> {
8585

8686
ISentrySpan? _transaction;
8787

88+
static String? _currentRouteName;
89+
90+
/// Get the current route of the observer.
91+
static String? get currentRouteName => _currentRouteName;
92+
8893
@override
8994
void didPush(Route<dynamic> route, Route<dynamic>? previousRoute) {
9095
super.didPush(route, previousRoute);
@@ -201,6 +206,7 @@ class SentryNavigatorObserver extends RouteObserver<PageRoute<dynamic>> {
201206
}
202207
},
203208
);
209+
_currentRouteName = name;
204210

205211
// if _enableAutoTransactions is enabled but there's no traces sample rate
206212
if (_transaction is NoOpSentrySpan) {

flutter/test/event_processor/flutter_enricher_event_processor_test.dart

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -322,6 +322,42 @@ void main() {
322322
.length;
323323
expect(ioEnricherCount, 1);
324324
});
325+
326+
testWidgets('adds SentryNavigatorObserver.currentRouteName as app.screen',
327+
(tester) async {
328+
final observer = SentryNavigatorObserver();
329+
final route =
330+
fixture.route(RouteSettings(name: 'fixture-currentRouteName'));
331+
observer.didPush(route, null);
332+
333+
final eventWithContextsApp =
334+
SentryEvent(contexts: Contexts(app: SentryApp()));
335+
336+
final enricher = fixture.getSut(
337+
binding: () => tester.binding,
338+
);
339+
final event = await enricher.apply(eventWithContextsApp);
340+
341+
expect(event?.contexts.app?.screen, 'fixture-currentRouteName');
342+
});
343+
344+
testWidgets(
345+
'does not add SentryNavigatorObserver.currentRouteName if no app',
346+
(tester) async {
347+
final observer = SentryNavigatorObserver();
348+
final route =
349+
fixture.route(RouteSettings(name: 'fixture-currentRouteName'));
350+
observer.didPush(route, null);
351+
352+
final eventWithoutContextsApp = SentryEvent(contexts: Contexts());
353+
354+
final enricher = fixture.getSut(
355+
binding: () => tester.binding,
356+
);
357+
final event = await enricher.apply(eventWithoutContextsApp);
358+
359+
expect(event?.contexts.app?.screen, isNull);
360+
});
325361
});
326362
}
327363

@@ -342,6 +378,11 @@ class Fixture {
342378
)..reportPackages = reportPackages;
343379
return FlutterEnricherEventProcessor(options);
344380
}
381+
382+
PageRoute<dynamic> route(RouteSettings? settings) => PageRouteBuilder<void>(
383+
pageBuilder: (_, __, ___) => Container(),
384+
settings: settings,
385+
);
345386
}
346387

347388
void loadTestPackage() {

flutter/test/sentry_navigator_observer_test.dart

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -368,6 +368,26 @@ void main() {
368368
expect(scope.span, span);
369369
});
370370
});
371+
372+
test('exposes current route name', () {
373+
const name = 'Current Route';
374+
final currentRoute = route(RouteSettings(name: name));
375+
376+
const op = 'navigation';
377+
final hub = _MockHub();
378+
final span = getMockSentryTracer(name: name);
379+
when(span.context).thenReturn(SentrySpanContext(operation: op));
380+
_whenAnyStart(hub, span);
381+
382+
final sut = fixture.getSut(
383+
hub: hub,
384+
autoFinishAfter: Duration(seconds: 5),
385+
);
386+
387+
sut.didPush(currentRoute, null);
388+
389+
expect(SentryNavigatorObserver.currentRouteName, 'Current Route');
390+
});
371391
});
372392

373393
group('RouteObserverBreadcrumb', () {

0 commit comments

Comments
 (0)