Skip to content

Commit 8a111a9

Browse files
authored
refactor: use native spotlight integrations on Flutter Android, iOS, macOS (#2285)
* update * update changelog * fix tests * fix analyze * update * rm local.properties from git * update naming * update test to use mock platform linux
1 parent eddc70e commit 8a111a9

File tree

11 files changed

+114
-17
lines changed

11 files changed

+114
-17
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
### Enhancements
1010

11+
- Use native spotlight integrations on Flutter Android, iOS, macOS ([#2285](https://github.com/getsentry/sentry-dart/pull/2285))
1112
- Improve app start integration ([#2266](https://github.com/getsentry/sentry-dart/pull/2266))
1213
- Fixes ([#2103](https://github.com/getsentry/sentry-dart/issues/2103))
1314
- Fixes ([#2233](https://github.com/getsentry/sentry-dart/issues/2233))

dart/lib/src/sentry_client.dart

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,14 @@ class SentryClient {
6363
final rateLimiter = RateLimiter(options);
6464
options.transport = HttpTransport(options, rateLimiter);
6565
}
66-
if (options.spotlight.enabled) {
66+
// TODO: Web might change soon to use the JS SDK so we can remove it here later on
67+
final enableFlutterSpotlight = (options.spotlight.enabled &&
68+
(options.platformChecker.isWeb ||
69+
options.platformChecker.platform.isLinux ||
70+
options.platformChecker.platform.isWindows));
71+
// Spotlight in the Flutter layer is only enabled for Web, Linux and Windows
72+
// Other platforms use spotlight through their native SDKs
73+
if (enableFlutterSpotlight) {
6774
options.transport = SpotlightHttpTransport(options, options.transport);
6875
}
6976
return SentryClient._(options);

dart/lib/src/spotlight.dart

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
import 'platform_checker.dart';
2-
31
/// Spotlight configuration class.
42
class Spotlight {
53
/// Whether to enable Spotlight for local development.
@@ -8,14 +6,7 @@ class Spotlight {
86
/// The Spotlight Sidecar URL.
97
/// Defaults to http://10.0.2.2:8969/stream due to Emulator on Android.
108
/// Otherwise defaults to http://localhost:8969/stream.
11-
String url;
12-
13-
Spotlight({required this.enabled, String? url})
14-
: url = url ?? _defaultSpotlightUrl();
15-
}
9+
String? url;
1610

17-
String _defaultSpotlightUrl() {
18-
return (PlatformChecker().platform.isAndroid
19-
? 'http://10.0.2.2:8969/stream'
20-
: 'http://localhost:8969/stream');
11+
Spotlight({required this.enabled, this.url});
2112
}

dart/lib/src/transport/spotlight_http_transport.dart

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ import '../http_client/client_provider.dart'
88
if (dart.library.io) '../http_client/io_client_provider.dart';
99

1010
/// Spotlight HTTP transport decorator that sends Sentry envelopes to both Sentry and Spotlight.
11+
/// This will be used on platforms that do not have native SDK support.
12+
/// Platforms with native SDK support will configure spotlight directly in the native SDK options.
1113
class SpotlightHttpTransport extends Transport {
1214
final SentryOptions _options;
1315
final Transport _transport;
@@ -21,8 +23,8 @@ class SpotlightHttpTransport extends Transport {
2123
}
2224

2325
SpotlightHttpTransport._(this._options, this._transport)
24-
: _requestHandler = HttpTransportRequestHandler(
25-
_options, Uri.parse(_options.spotlight.url));
26+
: _requestHandler = HttpTransportRequestHandler(_options,
27+
Uri.parse(_options.spotlight.url ?? _defaultSpotlightUrl()));
2628

2729
@override
2830
Future<SentryId?> send(SentryEnvelope envelope) async {
@@ -51,3 +53,7 @@ class SpotlightHttpTransport extends Transport {
5153
target: 'Spotlight');
5254
}
5355
}
56+
57+
String _defaultSpotlightUrl() {
58+
return 'http://localhost:8969/stream';
59+
}

dart/test/mocks/mock_platform.dart

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,10 @@ class MockPlatform extends Platform with NoSuchMethodProvider {
2121
return MockPlatform(os: 'linux');
2222
}
2323

24+
factory MockPlatform.windows() {
25+
return MockPlatform(os: 'windows');
26+
}
27+
2428
@override
2529
String operatingSystem;
2630
}

dart/test/sentry_client_test.dart

Lines changed: 62 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1779,12 +1779,72 @@ void main() {
17791779
expect(capturedEnvelope.header.dsn, fixture.options.dsn);
17801780
});
17811781

1782-
test('Spotlight enabled should set transport to SpotlightHttpTransport',
1782+
test(
1783+
'Spotlight enabled should not set transport to SpotlightHttpTransport on iOS',
1784+
() async {
1785+
fixture.options.platformChecker = MockPlatformChecker(
1786+
platform: MockPlatform.iOS(),
1787+
);
1788+
fixture.options.spotlight = Spotlight(enabled: true);
1789+
fixture.getSut();
1790+
1791+
expect(fixture.options.transport is SpotlightHttpTransport, isFalse);
1792+
});
1793+
1794+
test(
1795+
'Spotlight enabled should not set transport to SpotlightHttpTransport on macOS',
1796+
() async {
1797+
fixture.options.platformChecker = MockPlatformChecker(
1798+
platform: MockPlatform.macOS(),
1799+
);
1800+
fixture.options.spotlight = Spotlight(enabled: true);
1801+
fixture.getSut();
1802+
1803+
expect(fixture.options.transport is SpotlightHttpTransport, isFalse);
1804+
});
1805+
1806+
test(
1807+
'Spotlight enabled should not set transport to SpotlightHttpTransport on Android',
1808+
() async {
1809+
fixture.options.platformChecker = MockPlatformChecker(
1810+
platform: MockPlatform.android(),
1811+
);
1812+
fixture.options.spotlight = Spotlight(enabled: true);
1813+
fixture.getSut();
1814+
1815+
expect(fixture.options.transport is SpotlightHttpTransport, isFalse);
1816+
});
1817+
1818+
test(
1819+
'Spotlight enabled should set transport to SpotlightHttpTransport on Web',
1820+
() async {
1821+
fixture.options.platformChecker = MockPlatformChecker(isWebValue: true);
1822+
fixture.options.spotlight = Spotlight(enabled: true);
1823+
fixture.getSut();
1824+
1825+
expect(fixture.options.transport is SpotlightHttpTransport, isTrue);
1826+
});
1827+
1828+
test(
1829+
'Spotlight enabled should set transport to SpotlightHttpTransport on Linux',
1830+
() async {
1831+
fixture.options.platformChecker =
1832+
MockPlatformChecker(platform: MockPlatform.linux());
1833+
fixture.options.spotlight = Spotlight(enabled: true);
1834+
fixture.getSut();
1835+
1836+
expect(fixture.options.transport is SpotlightHttpTransport, isTrue);
1837+
});
1838+
1839+
test(
1840+
'Spotlight enabled should set transport to SpotlightHttpTransport on Windows',
17831841
() async {
1842+
fixture.options.platformChecker =
1843+
MockPlatformChecker(platform: MockPlatform.windows());
17841844
fixture.options.spotlight = Spotlight(enabled: true);
17851845
fixture.getSut();
17861846

1787-
expect(fixture.options.transport is SpotlightHttpTransport, true);
1847+
expect(fixture.options.transport is SpotlightHttpTransport, isTrue);
17881848
});
17891849
});
17901850

flutter/android/src/main/kotlin/io/sentry/flutter/SentryFlutter.kt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,12 @@ class SentryFlutter(
8181
data.getIfNotNull<String>("proguardUuid") {
8282
options.proguardUuid = it
8383
}
84+
data.getIfNotNull<Boolean>("enableSpotlight") {
85+
options.isEnableSpotlight = it
86+
}
87+
data.getIfNotNull<String>("spotlightUrl") {
88+
options.spotlightConnectionUrl = it
89+
}
8490

8591
val nativeCrashHandling = (data["enableNativeCrashHandling"] as? Boolean) ?: true
8692
// nativeCrashHandling has priority over anrEnabled
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<network-security-config>
3+
<domain-config cleartextTrafficPermitted="true">
4+
<!-- Allow cleartext traffic from the emulator to the host machine -->
5+
<!-- See https://developer.android.com/studio/run/emulator-networking for more details -->
6+
<domain includeSubdomains="true">10.0.2.2</domain>
7+
</domain-config>
8+
</network-security-config>

flutter/ios/Classes/SentryFlutter.swift

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,12 @@ public final class SentryFlutter {
7070
if let appHangTimeoutIntervalMillis = data["appHangTimeoutIntervalMillis"] as? NSNumber {
7171
options.appHangTimeoutInterval = appHangTimeoutIntervalMillis.doubleValue / 1000
7272
}
73+
if let spotlightUrl = data["spotlightUrl"] as? String {
74+
options.spotlightUrl = spotlightUrl
75+
}
76+
if let enableSpotlight = data["enableSpotlight"] as? Bool {
77+
options.enableSpotlight = enableSpotlight
78+
}
7379
if let proxy = data["proxy"] as? [String: Any] {
7480
guard let host = proxy["host"] as? String,
7581
let port = proxy["port"] as? Int,

flutter/lib/src/native/sentry_native_channel.dart

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,8 @@ class SentryNativeChannel
7171
'sessionSampleRate': options.experimental.replay.sessionSampleRate,
7272
'onErrorSampleRate': options.experimental.replay.onErrorSampleRate,
7373
},
74+
'enableSpotlight': options.spotlight.enabled,
75+
'spotlightUrl': options.spotlight.url,
7476
});
7577
}
7678

0 commit comments

Comments
 (0)