Skip to content

Commit e4ca6a8

Browse files
runningcodeclaude
andcommitted
fix(android): Time a warm relaunch within 1 minute from its own activity
Since #4999, destroying the last activity marks the next launch as warm but keeps the process-start anchor unless the process is older than a minute. A quick relaunch was therefore timed from process start: standalone mode re-sent the cold interval as a second warm App Start, and the relaunched screen's TTID spanned the whole session. Refs JAVA-503 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
1 parent 3406bbb commit e4ca6a8

2 files changed

Lines changed: 34 additions & 0 deletions

File tree

‎sentry-android-core/src/main/java/io/sentry/android/core/performance/AppStartMetrics.java‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,9 @@ public enum AppStartType {
8787
private @Nullable TracesSamplingDecision appStartSamplingDecision = null;
8888
private boolean isCallbackRegistered = false;
8989
private volatile boolean shouldSendStartMeasurements = true;
90+
// Set when the app moves to the background, so a relaunch in the same process is timed from its
91+
// own activity rather than from the process start.
92+
private volatile boolean isWarmRelaunchPending = false;
9093
private final AtomicInteger activeActivitiesCounter = new AtomicInteger();
9194
private final AtomicBoolean firstDrawDone = new AtomicBoolean(false);
9295
private final AtomicBoolean headlessAppStartCheckPending = new AtomicBoolean(false);
@@ -413,6 +416,7 @@ public void clear() {
413416
appLaunchedInForeground = null;
414417
isCallbackRegistered = false;
415418
shouldSendStartMeasurements = true;
419+
isWarmRelaunchPending = false;
416420
firstDrawDone.set(false);
417421
activeActivitiesCounter.set(0);
418422
firstIdle = -1;
@@ -677,8 +681,10 @@ public void onActivityCreated(@NonNull Activity activity, @Nullable Bundle saved
677681
// the extended vital measure from the activity while the eager app.start transaction stays
678682
// anchored at process start.
679683
if ((!isAppLaunchedInForeground()
684+
|| isWarmRelaunchPending
680685
|| durationSinceAppStartMillis > TimeUnit.MINUTES.toMillis(1))
681686
&& !appStartExtension.isActive()) {
687+
isWarmRelaunchPending = false;
682688
appStartType = AppStartType.WARM;
683689
shouldSendStartMeasurements = true;
684690
appStartSpan.reset();
@@ -745,6 +751,7 @@ public void onActivityDestroyed(@NonNull Activity activity) {
745751
appStartType = AppStartType.WARM;
746752
appLaunchedInForeground = true;
747753
shouldSendStartMeasurements = true;
754+
isWarmRelaunchPending = true;
748755
firstDrawDone.set(false);
749756
}
750757
}

‎sentry-android-core/src/test/java/io/sentry/android/core/performance/AppStartMetricsTest.kt‎

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import android.os.Handler
99
import android.os.Looper
1010
import android.os.SystemClock
1111
import androidx.test.ext.junit.runners.AndroidJUnit4
12+
import com.google.common.truth.Truth.assertThat
1213
import io.sentry.DateUtils
1314
import io.sentry.IContinuousProfiler
1415
import io.sentry.ITransaction
@@ -569,6 +570,32 @@ class AppStartMetricsTest {
569570
assertTrue(appStartMetrics.shouldSendStartMeasurements(false))
570571
}
571572

573+
@Test
574+
fun `a warm relaunch within 1 minute of process start is timed from the new activity`() {
575+
val metrics = AppStartMetrics.getInstance()
576+
metrics.appStartTimeSpan.setStartedAt(SystemClock.uptimeMillis())
577+
metrics.applicationOnCreateTimeSpan.setStartedAt(SystemClock.uptimeMillis())
578+
metrics.applicationOnCreateTimeSpan.setStoppedAt(SystemClock.uptimeMillis() + 100)
579+
metrics.contentProviderOnCreateTimeSpans.add(TimeSpan().apply { setStartedAt(1) })
580+
581+
val coldActivity = mock<Activity>()
582+
metrics.onActivityCreated(coldActivity, null)
583+
metrics.appStartTimeSpan.setStoppedAt(SystemClock.uptimeMillis() + 800)
584+
metrics.onAppStartSpansSent()
585+
metrics.onActivityDestroyed(coldActivity)
586+
587+
val relaunchUptimeMs = SystemClock.uptimeMillis() + TimeUnit.SECONDS.toMillis(25)
588+
SystemClock.setCurrentTimeMillis(relaunchUptimeMs)
589+
metrics.onActivityCreated(mock<Activity>(), null)
590+
591+
assertThat(metrics.appStartType).isEqualTo(AppStartMetrics.AppStartType.WARM)
592+
assertThat(metrics.appStartTimeSpan.startUptimeMs).isEqualTo(relaunchUptimeMs)
593+
assertThat(metrics.appStartTimeSpan.hasStopped()).isFalse()
594+
assertThat(metrics.applicationOnCreateTimeSpan.hasStarted()).isFalse()
595+
assertThat(metrics.contentProviderOnCreateTimeSpans).isEmpty()
596+
assertThat(metrics.shouldSendStartMeasurements(false)).isTrue()
597+
}
598+
572599
@Test
573600
fun `provider sets both appstart and sdk init start + end times`() {
574601
val metrics = AppStartMetrics.getInstance()

0 commit comments

Comments
 (0)