diff --git a/sentry-android-core/src/main/java/io/sentry/android/core/ActivityLifecycleIntegration.java b/sentry-android-core/src/main/java/io/sentry/android/core/ActivityLifecycleIntegration.java index 6d4a74429e..e5a61bd24c 100644 --- a/sentry-android-core/src/main/java/io/sentry/android/core/ActivityLifecycleIntegration.java +++ b/sentry-android-core/src/main/java/io/sentry/android/core/ActivityLifecycleIntegration.java @@ -74,6 +74,8 @@ public final class ActivityLifecycleIntegration private static final String TRACE_ORIGIN = "auto.ui.activity"; static final String APP_START_SCREEN_DATA = "app.vitals.start.screen"; static final String APP_START_TYPE_DATA = "app.vitals.start.type"; + static final String APP_START_TYPE_COLD = "cold"; + static final String APP_START_TYPE_WARM = "warm"; static final String APP_START_REASON_DATA = "app.vitals.start.reason"; static final String APP_START_TRACE_ORIGIN = "auto.app.start"; @@ -298,6 +300,9 @@ private void startTracing(final @NotNull Activity activity) { appStartSamplingDecision), appStartTransactionOptions); appStartTransaction.setData(APP_START_SCREEN_DATA, activityName); + appStartTransaction.setData( + APP_START_TYPE_DATA, + Boolean.TRUE.equals(coldStart) ? APP_START_TYPE_COLD : APP_START_TYPE_WARM); final @Nullable String appStartReason = AppStartMetrics.getInstance().getAppStartReason(); if (appStartReason != null) { appStartTransaction.setData(APP_START_REASON_DATA, appStartReason); @@ -323,9 +328,12 @@ private void startTracing(final @NotNull Activity activity) { if (isExtensionActive && isAppStart) { // Only the launch activity sets the screen, so a later activity can't overwrite it. A // screen also keeps the processor from classifying the eager app.start as headless. - AppStartMetrics.getInstance() - .getAppStartExtension() - .setData(APP_START_SCREEN_DATA, activityName); + final @NotNull AppStartExtension extension = + AppStartMetrics.getInstance().getAppStartExtension(); + extension.setData(APP_START_SCREEN_DATA, activityName); + extension.setData( + APP_START_TYPE_DATA, + Boolean.TRUE.equals(coldStart) ? APP_START_TYPE_COLD : APP_START_TYPE_WARM); } final @Nullable TransactionContext continuedContext = @@ -1022,6 +1030,14 @@ private void onHeadlessAppStart() { final @NotNull AppStartExtension extension = metrics.getAppStartExtension(); if (extension.isActive()) { + final @NotNull AppStartMetrics.AppStartType appStartType = metrics.getAppStartType(); + if (appStartType != AppStartMetrics.AppStartType.UNKNOWN) { + extension.setData( + APP_START_TYPE_DATA, + appStartType == AppStartMetrics.AppStartType.COLD + ? APP_START_TYPE_COLD + : APP_START_TYPE_WARM); + } extension.finishTransaction(endTime); return; } @@ -1072,6 +1088,14 @@ private void onHeadlessAppStart() { samplingDecision); final @NotNull ITransaction transaction = scopes.startTransaction(txnContext, txnOptions); + final @NotNull AppStartMetrics.AppStartType appStartType = metrics.getAppStartType(); + if (appStartType != AppStartMetrics.AppStartType.UNKNOWN) { + transaction.setData( + APP_START_TYPE_DATA, + appStartType == AppStartMetrics.AppStartType.COLD + ? APP_START_TYPE_COLD + : APP_START_TYPE_WARM); + } final @Nullable String appStartReason = metrics.getAppStartReason(); if (appStartReason != null) { transaction.setData(APP_START_REASON_DATA, appStartReason); diff --git a/sentry-android-core/src/main/java/io/sentry/android/core/PerformanceAndroidEventProcessor.java b/sentry-android-core/src/main/java/io/sentry/android/core/PerformanceAndroidEventProcessor.java index 503bd1d856..7c6a486a9f 100644 --- a/sentry-android-core/src/main/java/io/sentry/android/core/PerformanceAndroidEventProcessor.java +++ b/sentry-android-core/src/main/java/io/sentry/android/core/PerformanceAndroidEventProcessor.java @@ -2,7 +2,9 @@ import static io.sentry.android.core.ActivityLifecycleIntegration.APP_START_COLD; import static io.sentry.android.core.ActivityLifecycleIntegration.APP_START_SCREEN_DATA; +import static io.sentry.android.core.ActivityLifecycleIntegration.APP_START_TYPE_COLD; import static io.sentry.android.core.ActivityLifecycleIntegration.APP_START_TYPE_DATA; +import static io.sentry.android.core.ActivityLifecycleIntegration.APP_START_TYPE_WARM; import static io.sentry.android.core.ActivityLifecycleIntegration.APP_START_WARM; import static io.sentry.android.core.ActivityLifecycleIntegration.STANDALONE_APP_START_OP; import static io.sentry.android.core.ActivityLifecycleIntegration.UI_LOAD_OP; @@ -83,10 +85,11 @@ public SentryEvent process(@NotNull SentryEvent event, @NotNull Hint hint) { } final @NotNull AppStartMetrics appStartMetrics = AppStartMetrics.getInstance(); - // The app start type can flip (e.g. the last activity being destroyed marks the next start - // as warm) while this runs on the timer thread that finished the transaction. Read it once so - // the measurement key, contexts.app and the span attributes below cannot disagree. - final @NotNull AppStartMetrics.AppStartType appStartType = appStartMetrics.getAppStartType(); + // Prefer the start type recorded when the transaction/span was created: AppStartMetrics can + // already have flipped to WARM for the next launch if the launch activity was destroyed + // before this transaction finished. + final @NotNull AppStartMetrics.AppStartType appStartType = + resolveAppStartType(transaction, appStartMetrics); // the app start measurement is only sent once and only if the transaction has // the app.start span, which is automatically created by the SDK. if (hasAppStartSpan(transaction)) { @@ -160,7 +163,9 @@ public SentryEvent process(@NotNull SentryEvent event, @NotNull Hint hint) { transaction.getContexts().setApp(appContext); } final String appStartTypeName = - appStartType == AppStartMetrics.AppStartType.COLD ? "cold" : "warm"; + appStartType == AppStartMetrics.AppStartType.COLD + ? APP_START_TYPE_COLD + : APP_START_TYPE_WARM; appContext.setStartType(appStartTypeName); if (isStandaloneAppStartTxn) { @@ -278,6 +283,30 @@ private static boolean isTimestampWithinSpan( && (target.getTimestamp() == null || timestamp <= target.getTimestamp()); } + private @NotNull AppStartMetrics.AppStartType resolveAppStartType( + final @NotNull SentryTransaction txn, final @NotNull AppStartMetrics appStartMetrics) { + final @NotNull List spans = txn.getSpans(); + for (final @NotNull SentrySpan span : spans) { + if (span.getOp().contentEquals(APP_START_COLD)) { + return AppStartMetrics.AppStartType.COLD; + } else if (span.getOp().contentEquals(APP_START_WARM)) { + return AppStartMetrics.AppStartType.WARM; + } + } + + final @Nullable SpanContext traceContext = txn.getContexts().getTrace(); + if (traceContext != null && STANDALONE_APP_START_OP.equals(traceContext.getOperation())) { + final @Nullable Object recordedType = traceContext.getData().get(APP_START_TYPE_DATA); + if (APP_START_TYPE_COLD.equals(recordedType)) { + return AppStartMetrics.AppStartType.COLD; + } else if (APP_START_TYPE_WARM.equals(recordedType)) { + return AppStartMetrics.AppStartType.WARM; + } + } + + return appStartMetrics.getAppStartType(); + } + private boolean hasAppStartSpan(final @NotNull SentryTransaction txn) { final @NotNull List spans = txn.getSpans(); for (final @NotNull SentrySpan span : spans) { diff --git a/sentry-android-core/src/test/java/io/sentry/android/core/ActivityLifecycleIntegrationTest.kt b/sentry-android-core/src/test/java/io/sentry/android/core/ActivityLifecycleIntegrationTest.kt index bade1af172..5f7642a627 100644 --- a/sentry-android-core/src/test/java/io/sentry/android/core/ActivityLifecycleIntegrationTest.kt +++ b/sentry-android-core/src/test/java/io/sentry/android/core/ActivityLifecycleIntegrationTest.kt @@ -18,6 +18,7 @@ import androidx.test.ext.junit.runners.AndroidJUnit4 import com.google.common.truth.Truth.assertThat import io.sentry.DateUtils import io.sentry.FullyDisplayedReporter +import io.sentry.Hint import io.sentry.IScope import io.sentry.Scope import io.sentry.ScopeCallback @@ -42,6 +43,7 @@ import io.sentry.android.core.performance.AppStartMetrics import io.sentry.android.core.performance.AppStartMetrics.AppStartType import io.sentry.protocol.MeasurementValue import io.sentry.protocol.SentryId +import io.sentry.protocol.SentryTransaction import io.sentry.protocol.TransactionNameSource import io.sentry.test.DeferredExecutorService import io.sentry.test.ManualTimerExecutorService @@ -2652,6 +2654,40 @@ class ActivityLifecycleIntegrationTest { .isEqualTo(TimeUnit.SECONDS.toMillis(29)) } + @Test + fun `standalone cold app start keeps cold type when activity is destroyed before finish`() { + val sut = fixture.getSut { + it.tracesSampleRate = 1.0 + it.isEnableStandaloneAppStartTracing = true + } + setAppStartTime(SentryNanotimeDate(START_MILLIS, 0)) + sut.register(fixture.scopes, fixture.options) + + val activity = mock() + AppStartMetrics.getInstance().onActivityCreated(activity, fixture.bundle) + sut.onActivityCreated(activity, fixture.bundle) + + // AppStartMetrics callback runs before ActivityLifecycleIntegration on destroy + AppStartMetrics.getInstance().onActivityDestroyed(activity) + assertEquals(AppStartType.WARM, AppStartMetrics.getInstance().appStartType) + + sut.onActivityDestroyed(activity) + + val appStartTxn = + fixture.createdTransactions.single { + it.spanContext.operation == ActivityLifecycleIntegration.STANDALONE_APP_START_OP + } + assertEquals("cold", appStartTxn.getData(ActivityLifecycleIntegration.APP_START_TYPE_DATA)) + + val processor = PerformanceAndroidEventProcessor(fixture.options, mock()) + val processed = processor.process(SentryTransaction(appStartTxn), Hint()) + assertEquals("cold", processed.contexts.app?.startType) + assertEquals( + "cold", + processed.contexts.trace?.data?.get(ActivityLifecycleIntegration.APP_START_TYPE_DATA), + ) + } + private fun createAndResumeActivity(sut: ActivityLifecycleIntegration, view: View): Activity { val activity = mock() whenever(activity.findViewById(any())).thenReturn(view) diff --git a/sentry-android-core/src/test/java/io/sentry/android/core/PerformanceAndroidEventProcessorTest.kt b/sentry-android-core/src/test/java/io/sentry/android/core/PerformanceAndroidEventProcessorTest.kt index c6b4b73fa7..07c5037daa 100644 --- a/sentry-android-core/src/test/java/io/sentry/android/core/PerformanceAndroidEventProcessorTest.kt +++ b/sentry-android-core/src/test/java/io/sentry/android/core/PerformanceAndroidEventProcessorTest.kt @@ -262,6 +262,62 @@ class PerformanceAndroidEventProcessorTest { } } + @Test + fun `cold app start span stays cold when AppStartMetrics flips to warm before processing`() { + val sut = fixture.getSut(enablePerformanceV2 = true) + AppStartMetrics.getInstance().apply { + appStartType = AppStartType.WARM + isAppLaunchedInForeground = true + classLoadedUptimeMs = 50 + appStartTimeSpan.apply { + setStartedAt(1) + setStoppedAt(100) + } + applicationOnCreateTimeSpan.apply { + setStartedAt(10) + description = "com.example.App.onCreate" + setStoppedAt(42) + } + } + + var tr = createUiLoadTransactionWithAppStartChildSpan(coldStart = true) + tr = sut.process(tr, Hint()) + + assertThat(tr.measurements).containsKey(MeasurementValue.KEY_APP_START_COLD) + assertThat(tr.measurements).doesNotContainKey(MeasurementValue.KEY_APP_START_WARM) + assertThat(tr.contexts.app!!.startType).isEqualTo("cold") + assertTrue(tr.spans.any { it.op == "application.load" }) + } + + @Test + fun `standalone cold app start stays cold when AppStartMetrics flips to warm before processing`() { + val sut = fixture.getSut(enablePerformanceV2 = true) + AppStartMetrics.getInstance().apply { + appStartType = AppStartType.WARM + isAppLaunchedInForeground = true + classLoadedUptimeMs = 50 + appStartTimeSpan.apply { + setStartedAt(1) + setStoppedAt(100) + } + applicationOnCreateTimeSpan.apply { + setStartedAt(10) + description = "com.example.App.onCreate" + setStoppedAt(42) + } + } + + var tr = createStandaloneAppStartTransaction(appStartScreen = "Activity") + tr.contexts.trace!!.setData(APP_START_TYPE_DATA, "cold") + tr = sut.process(tr, Hint()) + + assertThat(tr.measurements).containsKey(MeasurementValue.KEY_APP_START_COLD) + assertThat(tr.measurements).doesNotContainKey(MeasurementValue.KEY_APP_START_WARM) + assertThat(tr.contexts.app!!.startType).isEqualTo("cold") + assertThat(tr.contexts.trace!!.data[APP_START_TYPE_DATA]).isEqualTo("cold") + assertTrue(tr.spans.any { it.op == "application.load" }) + } + @Test fun `standalone app start sets screen and type on user spans under the extended span`() { val sut = fixture.getSut() @@ -498,7 +554,7 @@ class PerformanceAndroidEventProcessorTest { fun `do not add app start metric twice`() { val sut = fixture.getSut() - var tr1 = createUiLoadTransactionWithAppStartChildSpan() + var tr1 = createUiLoadTransactionWithAppStartChildSpan(coldStart = false) setAppStart(fixture.options, false) tr1 = sut.process(tr1, Hint())