Skip to content
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# Changelog

## Unreleased

### Fixes

- Filter out app starts with more than 60s ([#2127](https://github.com/getsentry/sentry-java/pull/2127))

## 6.1.3

### Fixes
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ public final class AppStartState {

private static @NotNull AppStartState instance = new AppStartState();

/** We filter out App starts more than 60s */
private static final int MAX_APP_START_MILLIS = 60000;

private @Nullable Long appStartMillis;

private @Nullable Long appStartEndMillis;
Expand Down Expand Up @@ -48,7 +51,21 @@ public synchronized Long getAppStartInterval() {
if (appStartMillis == null || appStartEndMillis == null || coldStart == null) {
return null;
}
return appStartEndMillis - appStartMillis;
final long appStart = appStartEndMillis - appStartMillis;

// We filter out app start more than 60s.
// This could be due to many different reasons.
// If you do the manual init and init the SDK too late and it does not compute the app start end
// in the very first Activity.
// If the process starts but the App isn't in the foreground.
// If the system forked the zygote earlier to accelerate the app start.
// And some unknown reasons that could not be reproduced.
// We've seen app starts with hours, days and even months.
if (appStart >= MAX_APP_START_MILLIS) {
return null;
}

return appStart;
}

public @Nullable Boolean isColdStart() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@
/**
* SentryPerformanceProvider is responsible for collecting data (eg appStart) as early as possible
* as ContentProvider is the only reliable hook for libraries that works across all the supported
* SDK versions. When minSDK is >= 24, we could use Process.getStartUptimeMillis()
* SDK versions. When minSDK is >= 24, we could use Process.getStartUptimeMillis() We could also use
* AppComponentFactory but it depends on androidx.core.app.AppComponentFactory
*/
@ApiStatus.Internal
public final class SentryPerformanceProvider extends ContentProvider
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,4 +77,16 @@ class AppStartStateTest {

assertEquals(400, sut.appStartInterval)
}

@Test
fun `getAppStartInterval returns null if more than 60s`() {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

m: We could add another test with the max app start duration of 59999 ms to validate it is working correctly.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think this is unnecessary, we already test above and lower the boundary.
The comparison is a simple >= with a long value rather than some more complicated calculation.
I can do it but sounds like not needed, your call.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If someone changes the MAX_APP_START_MILLIS to 10000000, no test will fail. Up to you.

val sut = AppStartState.getInstance()

val date = Date()
sut.setAppStartTime(100, date)
sut.setAppStartEnd(60100)
sut.setColdStart(true)

assertNull(sut.appStartInterval)
}
}