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 SentryNavigatorObserver current route to event.app.contexts.viewNames #1545

Merged
merged 33 commits into from
Oct 31, 2023
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
Show all changes
33 commits
Select commit Hold shift + click to select a range
9ca5625
add screen to sentry app
denrase Jul 11, 2023
1fa6515
sett event.contexts.app.screen to navigator observers current route
denrase Jul 11, 2023
90b59e1
add changelog entry
denrase Jul 11, 2023
93debb7
remove failing test, as lifecyle may init new app instance
denrase Jul 11, 2023
8eadcdd
rename property and add more comments
denrase Jul 17, 2023
475ce7a
Merge branch 'main' into feat/app-context-screen
denrase Jul 17, 2023
8e81ee5
Merge branch 'main' into feat/app-context-screen
denrase Jul 17, 2023
b356571
update changelog
denrase Jul 17, 2023
bacf003
Merge branch 'main' into feat/app-context-screen
denrase Jul 17, 2023
2630144
Merge branch 'main' into feat/app-context-screen
denrase Jul 24, 2023
320fe7a
change to array
denrase Jul 31, 2023
5ae2e54
change comment
denrase Jul 31, 2023
2c2e6d2
update comment
denrase Jul 31, 2023
6ac2ec6
update changelog entry
denrase Jul 31, 2023
f2b295c
Merge branch 'main' into feat/app-context-screen
denrase Aug 22, 2023
54283bb
fix changelog
denrase Aug 22, 2023
c7eec5f
Merge branch 'main' into feat/app-context-screen
denrase Aug 29, 2023
1818545
run `dart fix --apply`
denrase Aug 29, 2023
789acd7
Merge branch 'main' into feat/app-context-screen
denrase Sep 4, 2023
8c2704c
Merge branch 'main' into feat/app-context-screen
denrase Sep 5, 2023
295681c
Merge branch 'main' into feat/app-context-screen
denrase Sep 11, 2023
c027226
fix changelog
denrase Sep 11, 2023
8f4e6c8
Merge branch 'main' into feat/app-context-screen
denrase Oct 2, 2023
6da8a51
fix changelog
denrase Oct 2, 2023
445babb
Add missing import
denrase Oct 3, 2023
89c47ee
Merge branch 'main' into feat/app-context-screen
denrase Oct 9, 2023
9e07c77
Update changelog
denrase Oct 9, 2023
3cef7aa
rename method
denrase Oct 9, 2023
5975e5c
Also set current route name on replace
denrase Oct 9, 2023
6300dc4
Merge branch 'main' into feat/app-context-screen
denrase Oct 24, 2023
f977958
mark current route as internal
denrase Oct 24, 2023
cef9cc7
Merge branch 'main' into feat/app-context-screen
denrase Oct 30, 2023
6aacd98
fix changelog
denrase Oct 30, 2023
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
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Changelog

## Unreleased

- Set `SentryNavigatorObserver` current route as `event.app.contexts.screen` ([#1545](https://github.com/getsentry/sentry-dart/pull/1545))

## 7.8.0

### Enhancements
Expand Down
9 changes: 9 additions & 0 deletions dart/lib/src/protocol/sentry_app.dart
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ class SentryApp {
this.deviceAppHash,
this.appMemory,
this.inForeground,
this.screen,
});

/// Human readable application name, as it appears on the platform.
Expand Down Expand Up @@ -48,6 +49,9 @@ class SentryApp {
/// An app is in foreground when it's visible to the user.
final bool? inForeground;

/// The current application screen.
final String? screen;
denrase marked this conversation as resolved.
Show resolved Hide resolved

/// Deserializes a [SentryApp] from JSON [Map].
factory SentryApp.fromJson(Map<String, dynamic> data) => SentryApp(
name: data['app_name'],
Expand All @@ -61,6 +65,7 @@ class SentryApp {
deviceAppHash: data['device_app_hash'],
appMemory: data['app_memory'],
inForeground: data['in_foreground'],
screen: data['screen'],
);

/// Produces a [Map] that can be serialized to JSON.
Expand All @@ -75,6 +80,7 @@ class SentryApp {
if (appMemory != null) 'app_memory': appMemory!,
if (startTime != null) 'app_start_time': startTime!.toIso8601String(),
if (inForeground != null) 'in_foreground': inForeground!,
if (screen != null) 'screen': screen!
};
}

Expand All @@ -88,6 +94,7 @@ class SentryApp {
deviceAppHash: deviceAppHash,
appMemory: appMemory,
inForeground: inForeground,
screen: screen,
);

SentryApp copyWith({
Expand All @@ -100,6 +107,7 @@ class SentryApp {
String? deviceAppHash,
int? appMemory,
bool? inForeground,
String? screen,
}) =>
SentryApp(
name: name ?? this.name,
Expand All @@ -111,5 +119,6 @@ class SentryApp {
deviceAppHash: deviceAppHash ?? this.deviceAppHash,
appMemory: appMemory ?? this.appMemory,
inForeground: inForeground ?? this.inForeground,
screen: screen ?? this.screen,
);
}
21 changes: 12 additions & 9 deletions dart/test/protocol/sentry_app_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +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,
);
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 All @@ -25,6 +25,7 @@ void main() {
'app_start_time': testStartTime.toIso8601String(),
'device_app_hash': 'fixture-deviceAppHash',
'in_foreground': true,
'screen': 'fixture-screen',
};

group('json', () {
Expand Down Expand Up @@ -73,6 +74,7 @@ void main() {
startTime: startTime,
deviceAppHash: 'hash1',
inForeground: true,
screen: 'screen1',
);

expect('name1', copy.name);
Expand All @@ -83,6 +85,7 @@ void main() {
expect(startTime, copy.startTime);
expect('hash1', copy.deviceAppHash);
expect(true, copy.inForeground);
expect('screen1', copy.screen);
});
});
}
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) {
denrase marked this conversation as resolved.
Show resolved Hide resolved
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;
stefanosiano marked this conversation as resolved.
Show resolved Hide resolved

@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;
denrase marked this conversation as resolved.
Show resolved Hide resolved

// 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,24 @@ 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');
});
});
}

Expand All @@ -342,6 +360,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
Loading