Skip to content

Commit e423a36

Browse files
committed
Change static vars to member vars
1 parent 3a16179 commit e423a36

File tree

5 files changed

+52
-59
lines changed

5 files changed

+52
-59
lines changed

flutter/lib/src/event_processor/native_app_start_event_processor.dart

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,30 +18,38 @@ class NativeAppStartEventProcessor implements EventProcessor {
1818
@override
1919
Future<SentryEvent?> apply(SentryEvent event, Hint hint) async {
2020
final options = _hub.options;
21-
if (NativeAppStartIntegration.didAddAppStartMeasurement ||
21+
22+
final integrations =
23+
options.integrations.whereType<NativeAppStartIntegration>();
24+
if (integrations.isEmpty) {
25+
return event;
26+
}
27+
final nativeAppStartIntegration = integrations.first;
28+
29+
if (nativeAppStartIntegration.didAddAppStartMeasurement ||
2230
event is! SentryTransaction ||
2331
options is! SentryFlutterOptions) {
2432
return event;
2533
}
2634

2735
AppStartInfo? appStartInfo;
2836
if (!options.autoAppStart) {
29-
final appStartEnd = NativeAppStartIntegration.appStartEnd;
37+
final appStartEnd = nativeAppStartIntegration.appStartEnd;
3038
if (appStartEnd != null) {
31-
appStartInfo = await NativeAppStartIntegration.getAppStartInfo();
39+
appStartInfo = await nativeAppStartIntegration.getAppStartInfo();
3240
appStartInfo?.end = appStartEnd;
3341
} else {
3442
// If autoAppStart is disabled and appStartEnd is not set, we can't add app starts
3543
return event;
3644
}
3745
} else {
38-
appStartInfo = await NativeAppStartIntegration.getAppStartInfo();
46+
appStartInfo = await nativeAppStartIntegration.getAppStartInfo();
3947
}
4048

4149
final measurement = appStartInfo?.toMeasurement();
4250
if (measurement != null) {
4351
event.measurements[measurement.name] = measurement;
44-
NativeAppStartIntegration.didAddAppStartMeasurement = true;
52+
nativeAppStartIntegration.didAddAppStartMeasurement = true;
4553
}
4654

4755
if (appStartInfo != null) {

flutter/lib/src/integrations/native_app_start_integration.dart

Lines changed: 8 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -24,14 +24,14 @@ class NativeAppStartIntegration extends Integration<SentryFlutterOptions> {
2424
/// [SentryFlutterOptions.autoAppStart] is true, or by calling
2525
/// [SentryFlutter.setAppStartEnd]
2626
@internal
27-
static DateTime? appStartEnd;
27+
DateTime? appStartEnd;
2828

2929
/// Flag indicating if app start was already fetched.
30-
static bool _didFetchAppStart = false;
30+
bool _didFetchAppStart = false;
3131

3232
/// Flag indicating if app start measurement was added to the first transaction.
3333
@internal
34-
static bool didAddAppStartMeasurement = false;
34+
bool didAddAppStartMeasurement = false;
3535

3636
/// Timeout duration to wait for the app start info to be fetched.
3737
static const _timeoutDuration = Duration(seconds: 10);
@@ -42,15 +42,14 @@ class NativeAppStartIntegration extends Integration<SentryFlutterOptions> {
4242
/// We filter out App starts more than 60s
4343
static const _maxAppStartMillis = 60000;
4444

45-
static Completer<AppStartInfo?> _appStartCompleter =
46-
Completer<AppStartInfo?>();
47-
static AppStartInfo? _appStartInfo;
45+
Completer<AppStartInfo?> _appStartCompleter = Completer<AppStartInfo?>();
46+
AppStartInfo? _appStartInfo;
4847

4948
@internal
50-
static bool isIntegrationTest = false;
49+
bool isIntegrationTest = false;
5150

5251
@internal
53-
static void setAppStartInfo(AppStartInfo? appStartInfo) {
52+
void setAppStartInfo(AppStartInfo? appStartInfo) {
5453
_appStartInfo = appStartInfo;
5554
if (_appStartCompleter.isCompleted) {
5655
_appStartCompleter = Completer<AppStartInfo?>();
@@ -59,28 +58,14 @@ class NativeAppStartIntegration extends Integration<SentryFlutterOptions> {
5958
}
6059

6160
@internal
62-
static Future<AppStartInfo?> getAppStartInfo() {
61+
Future<AppStartInfo?> getAppStartInfo() {
6362
if (_appStartInfo != null) {
6463
return Future.value(_appStartInfo);
6564
}
6665
return _appStartCompleter.future
6766
.timeout(_timeoutDuration, onTimeout: () => null);
6867
}
6968

70-
@visibleForTesting
71-
static void clearAppStartInfo() {
72-
_appStartInfo = null;
73-
_appStartCompleter = Completer<AppStartInfo?>();
74-
didAddAppStartMeasurement = false;
75-
}
76-
77-
/// Reset state
78-
@visibleForTesting
79-
static void reset() {
80-
appStartEnd = null;
81-
_didFetchAppStart = false;
82-
}
83-
8469
@override
8570
void call(Hub hub, SentryFlutterOptions options) {
8671
if (isIntegrationTest) {

flutter/lib/src/navigation/sentry_navigator_observer.dart

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -350,13 +350,14 @@ class SentryNavigatorObserver extends RouteObserver<PageRoute<dynamic>> {
350350
DateTime startTimestamp = _hub.options.clock();
351351
DateTime? endTimestamp;
352352

353-
if (isAppStart) {
354-
final appStartInfo = await NativeAppStartIntegration.getAppStartInfo();
355-
if (appStartInfo == null) return;
356-
357-
startTimestamp = appStartInfo.start;
358-
endTimestamp = appStartInfo.end;
359-
}
353+
// TODO: Handle in app_start_event_processor
354+
// if (isAppStart) {
355+
// final appStartInfo = await NativeAppStartIntegration.getAppStartInfo();
356+
// if (appStartInfo == null) return;
357+
//
358+
// startTimestamp = appStartInfo.start;
359+
// endTimestamp = appStartInfo.end;
360+
// }
360361

361362
await _startTransaction(route, startTimestamp);
362363

flutter/lib/src/sentry_flutter.dart

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -226,7 +226,11 @@ mixin SentryFlutter {
226226
/// Manually set when your app finished startup. Make sure to set
227227
/// [SentryFlutterOptions.autoAppStart] to false on init.
228228
static void setAppStartEnd(DateTime appStartEnd) {
229-
NativeAppStartIntegration.appStartEnd = appStartEnd;
229+
// ignore: invalid_use_of_internal_member
230+
final integrations = Sentry.currentHub.options.integrations
231+
.whereType<NativeAppStartIntegration>();
232+
integrations
233+
.forEach((integration) => integration.appStartEnd = appStartEnd);
230234
}
231235

232236
static void _setSdk(SentryFlutterOptions options) {

flutter/test/integrations/native_app_start_integration_test.dart

Lines changed: 18 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -31,27 +31,27 @@ void main() {
3131
// This ensures that setAppStartInfo has been called, which happens asynchronously
3232
// in a post-frame callback. Waiting here prevents race conditions in subsequent tests
3333
// that might depend on or modify the app start info.
34-
await NativeAppStartIntegration.getAppStartInfo();
34+
await fixture.sut.getAppStartInfo();
3535
}
3636

3737
group('$NativeAppStartIntegration', () {
3838
late Fixture fixture;
3939

4040
setUp(() {
4141
fixture = Fixture();
42+
fixture.options.addIntegration(fixture.sut);
43+
4244
setupMocks(fixture);
4345
when(fixture.binding.fetchNativeAppStart()).thenAnswer((_) async =>
4446
NativeAppStart(
4547
appStartTime: 0,
4648
pluginRegistrationTime: 10,
4749
isColdStart: true,
4850
nativeSpanTimes: {}));
49-
NativeAppStartIntegration.clearAppStartInfo();
5051
});
5152

5253
test('native app start measurement added to first transaction', () async {
53-
NativeAppStartIntegration.appStartEnd =
54-
DateTime.fromMillisecondsSinceEpoch(10);
54+
fixture.sut.appStartEnd = DateTime.fromMillisecondsSinceEpoch(10);
5555

5656
await registerIntegration(fixture);
5757
final tracer = fixture.createTracer();
@@ -68,8 +68,7 @@ void main() {
6868

6969
test('native app start measurement not added to following transactions',
7070
() async {
71-
NativeAppStartIntegration.appStartEnd =
72-
DateTime.fromMillisecondsSinceEpoch(10);
71+
fixture.sut.appStartEnd = DateTime.fromMillisecondsSinceEpoch(10);
7372

7473
await registerIntegration(fixture);
7574
final tracer = fixture.createTracer();
@@ -86,8 +85,7 @@ void main() {
8685
});
8786

8887
test('measurements appended', () async {
89-
NativeAppStartIntegration.appStartEnd =
90-
DateTime.fromMillisecondsSinceEpoch(10);
88+
fixture.sut.appStartEnd = DateTime.fromMillisecondsSinceEpoch(10);
9189
final measurement = SentryMeasurement.warmAppStart(Duration(seconds: 1));
9290

9391
await registerIntegration(fixture);
@@ -107,8 +105,7 @@ void main() {
107105
});
108106

109107
test('native app start measurement not added if more than 60s', () async {
110-
NativeAppStartIntegration.appStartEnd =
111-
DateTime.fromMillisecondsSinceEpoch(60001);
108+
fixture.sut.appStartEnd = DateTime.fromMillisecondsSinceEpoch(60001);
112109

113110
await registerIntegration(fixture);
114111
final tracer = fixture.createTracer();
@@ -123,11 +120,10 @@ void main() {
123120

124121
test('native app start integration is called and sets app start info',
125122
() async {
126-
NativeAppStartIntegration.appStartEnd =
127-
DateTime.fromMillisecondsSinceEpoch(10);
123+
fixture.sut.appStartEnd = DateTime.fromMillisecondsSinceEpoch(10);
128124

129125
await registerIntegration(fixture);
130-
final appStartInfo = await NativeAppStartIntegration.getAppStartInfo();
126+
final appStartInfo = await fixture.sut.getAppStartInfo();
131127
expect(appStartInfo?.start, DateTime.fromMillisecondsSinceEpoch(0));
132128
expect(appStartInfo?.end, DateTime.fromMillisecondsSinceEpoch(10));
133129
});
@@ -157,6 +153,7 @@ void main() {
157153
fixture = Fixture(
158154
frameCallbackTimeout: NativeAppStartIntegration.timeoutDuration +
159155
const Duration(seconds: 5));
156+
fixture.options.addIntegration(fixture.sut);
160157
fixture.options.autoAppStart = false;
161158

162159
await registerIntegration(fixture);
@@ -179,7 +176,9 @@ void main() {
179176
fixture.options.autoAppStart = false;
180177

181178
await registerIntegration(fixture);
182-
SentryFlutter.setAppStartEnd(DateTime.fromMillisecondsSinceEpoch(10));
179+
180+
fixture.sut.appStartEnd = DateTime.fromMillisecondsSinceEpoch(10);
181+
// SentryFlutter.setAppStartEnd(DateTime.fromMillisecondsSinceEpoch(10));
183182

184183
final tracer = fixture.createTracer();
185184
final transaction = SentryTransaction(tracer);
@@ -192,7 +191,7 @@ void main() {
192191
expect(measurement.value, 10);
193192
expect(measurement.unit, DurationSentryMeasurementUnit.milliSecond);
194193

195-
final appStartInfo = await NativeAppStartIntegration.getAppStartInfo();
194+
final appStartInfo = await fixture.sut.getAppStartInfo();
196195

197196
final appStartSpan = enriched.spans.firstWhereOrNull((element) =>
198197
element.context.description == appStartInfo!.appStartTypeDescription);
@@ -260,10 +259,8 @@ void main() {
260259

261260
setUp(() async {
262261
fixture = Fixture();
263-
NativeAppStartIntegration.clearAppStartInfo();
264-
265-
NativeAppStartIntegration.appStartEnd =
266-
DateTime.fromMillisecondsSinceEpoch(50);
262+
fixture.options.addIntegration(fixture.sut);
263+
fixture.sut.appStartEnd = DateTime.fromMillisecondsSinceEpoch(50);
267264

268265
// dartLoadingEnd needs to be set after engine end (see MockNativeChannel)
269266
SentryFlutter.sentrySetupStartTime =
@@ -281,7 +278,7 @@ void main() {
281278
enriched =
282279
await processor.apply(transaction, Hint()) as SentryTransaction;
283280

284-
final appStartInfo = await NativeAppStartIntegration.getAppStartInfo();
281+
final appStartInfo = await fixture.sut.getAppStartInfo();
285282

286283
coldStartSpan = enriched.spans.firstWhereOrNull((element) =>
287284
element.context.description == appStartInfo?.appStartTypeDescription);
@@ -395,8 +392,7 @@ void main() {
395392
final engineReadyEndtime = DateTime.fromMillisecondsSinceEpoch(
396393
appStartInfoSrc.pluginRegistrationTime.toInt())
397394
.toUtc();
398-
expect(coldStartSpan?.endTimestamp,
399-
NativeAppStartIntegration.appStartEnd?.toUtc());
395+
expect(coldStartSpan?.endTimestamp, fixture.sut.appStartEnd?.toUtc());
400396
expect(pluginRegistrationSpan?.endTimestamp, engineReadyEndtime);
401397
expect(sentrySetupSpan?.endTimestamp,
402398
SentryFlutter.sentrySetupStartTime?.toUtc());
@@ -415,7 +411,6 @@ class Fixture extends IntegrationTestFixture<NativeAppStartIntegration> {
415411
FakeFrameCallbackHandler(
416412
finishAfterDuration: frameCallbackTimeout ??
417413
const Duration(milliseconds: 50)))) {
418-
NativeAppStartIntegration.reset();
419414
hub = MockHub();
420415
// ignore: invalid_use_of_internal_member
421416
when(hub.options).thenReturn(options);

0 commit comments

Comments
 (0)