diff --git a/develop-docs/feature/profiling/perfetto.md b/develop-docs/feature/profiling/perfetto.md index 7a7ad4a40d..fe0941e482 100644 --- a/develop-docs/feature/profiling/perfetto.md +++ b/develop-docs/feature/profiling/perfetto.md @@ -90,7 +90,10 @@ by default. Applications can temporarily opt into the legacy `Debug`-based with the deprecated `enableLegacyProfiling` option (manifest key `io.sentry.profiling.enable-legacy-profiling`). The legacy profiler can cause Android Runtime crashes and will be removed in version 10. Only **continuous profiling** is supported on the -Perfetto path — transaction-based and app-start profiling are not. +Perfetto path — transaction-based and app-start profiling are not. When both transaction and +continuous profiling are configured on API 35 and newer, continuous UI Profiling takes precedence +and the transaction profiling options are ignored. Below API 35, transaction profiling keeps its +existing precedence when the legacy profiler is explicitly enabled. ### Capturing chunks diff --git a/sentry-android-core/src/main/java/io/sentry/android/core/AndroidOptionsInitializer.java b/sentry-android-core/src/main/java/io/sentry/android/core/AndroidOptionsInitializer.java index d0a151ea5f..d58a21d64e 100644 --- a/sentry-android-core/src/main/java/io/sentry/android/core/AndroidOptionsInitializer.java +++ b/sentry-android-core/src/main/java/io/sentry/android/core/AndroidOptionsInitializer.java @@ -312,8 +312,25 @@ private static void setupProfiler( final @Nullable ITransactionProfiler appStartTransactionProfiler, final @Nullable IContinuousProfiler appStartContinuousProfiler, final @NotNull CompositePerformanceCollector performanceCollector) { - final boolean isTransactionProfilingRequested = + final boolean isPerfettoAvailable = + buildInfoProvider.getSdkInfoVersion() >= Build.VERSION_CODES.VANILLA_ICE_CREAM; + boolean isTransactionProfilingRequested = options.isProfilingEnabled() || options.getProfilesSampleRate() != null; + final @Nullable Double profileSessionSampleRate = options.getProfileSessionSampleRate(); + if (isPerfettoAvailable + && profileSessionSampleRate != null + && profileSessionSampleRate > 0 + && isTransactionProfilingRequested) { + options + .getLogger() + .log( + SentryLevel.WARNING, + "Both transaction and UI profiling are configured. UI profiling takes precedence " + + "on API 35 and newer. profilesSampleRate and profilesSampler will be ignored."); + options.setProfilesSampleRate(null); + options.setProfilesSampler(null); + isTransactionProfilingRequested = false; + } if (isTransactionProfilingRequested && options.isEnableLegacyProfiling()) { options .getLogger() @@ -373,7 +390,7 @@ private static void setupProfiler( final @NotNull SentryFrameMetricsCollector frameMetricsCollector = Objects.requireNonNull( options.getFrameMetricsCollector(), "options.getFrameMetricsCollector is required"); - if (buildInfoProvider.getSdkInfoVersion() >= Build.VERSION_CODES.VANILLA_ICE_CREAM) { + if (isPerfettoAvailable) { final @NotNull Context appContext = ContextUtils.getApplicationContext(context); options.setContinuousProfiler( new PerfettoContinuousProfiler( diff --git a/sentry-android-core/src/test/java/io/sentry/android/core/AndroidOptionsInitializerTest.kt b/sentry-android-core/src/test/java/io/sentry/android/core/AndroidOptionsInitializerTest.kt index 8291c52461..100918795c 100644 --- a/sentry-android-core/src/test/java/io/sentry/android/core/AndroidOptionsInitializerTest.kt +++ b/sentry-android-core/src/test/java/io/sentry/android/core/AndroidOptionsInitializerTest.kt @@ -432,6 +432,26 @@ class AndroidOptionsInitializerTest { assertTrue(fixture.sentryOptions.continuousProfiler is PerfettoContinuousProfiler) } + @Config(sdk = [35]) + @Test + fun `init on API 35+ prefers UI profiling when both profiling modes are configured`() { + fixture.initSut( + configureOptions = { + profilesSampleRate = 1.0 + profilesSampler = mock() + profileSessionSampleRate = 1.0 + isEnableLegacyProfiling = true + }, + ) + + assertEquals(NoOpTransactionProfiler.getInstance(), fixture.sentryOptions.transactionProfiler) + assertTrue(fixture.sentryOptions.continuousProfiler is PerfettoContinuousProfiler) + assertTrue(fixture.sentryOptions.isContinuousProfilingEnabled) + assertFalse(fixture.sentryOptions.isProfilingEnabled) + assertNull(fixture.sentryOptions.profilesSampleRate) + assertNull(fixture.sentryOptions.profilesSampler) + } + @Config(sdk = [35]) @Test fun `init on API 35+ reports PerfettoContinuousProfiling integration`() { @@ -472,6 +492,23 @@ class AndroidOptionsInitializerTest { assertTrue(fixture.sentryOptions.continuousProfiler is NoOpContinuousProfiler) } + @Config(sdk = [33]) + @Test + fun `init below API 35 keeps transaction profiling precedence when both modes are configured`() { + fixture.initSut( + configureOptions = { + profilesSampleRate = 1.0 + profileSessionSampleRate = 1.0 + isEnableLegacyProfiling = true + }, + ) + + assertTrue(fixture.sentryOptions.transactionProfiler is AndroidTransactionProfiler) + assertEquals(NoOpContinuousProfiler.getInstance(), fixture.sentryOptions.continuousProfiler) + assertTrue(fixture.sentryOptions.isProfilingEnabled) + assertFalse(fixture.sentryOptions.isContinuousProfilingEnabled) + } + @Test fun `init with profilesSampleRate and legacy profiling enabled sets transaction profiler`() { fixture.initSut( diff --git a/sentry/src/main/java/io/sentry/SentryOptions.java b/sentry/src/main/java/io/sentry/SentryOptions.java index 9c2f5f2b98..e0e48c0f86 100644 --- a/sentry/src/main/java/io/sentry/SentryOptions.java +++ b/sentry/src/main/java/io/sentry/SentryOptions.java @@ -2332,9 +2332,9 @@ public void setProfilesSampleRate(final @Nullable Double profilesSampleRate) { } /** - * Returns the session sample rate. Default is null (disabled). ProfilesSampleRate takes - * precedence over this. To enable continuous profiling, don't set profilesSampleRate or - * profilesSampler, or set them to null. + * Returns the session sample rate. Default is null (disabled). On Android API 35 and newer, a + * positive session sample rate takes precedence over {@code profilesSampleRate} and {@code + * profilesSampler}. On other platforms, transaction profiling takes precedence. * * @return the sample rate */ @@ -2343,9 +2343,9 @@ public void setProfilesSampleRate(final @Nullable Double profilesSampleRate) { } /** - * Set the session sample rate. Default is null (disabled). ProfilesSampleRate takes precedence - * over this. To enable continuous profiling, don't set profilesSampleRate or profilesSampler, or - * set them to null. + * Set the session sample rate. Default is null (disabled). On Android API 35 and newer, a + * positive session sample rate takes precedence over {@code profilesSampleRate} and {@code + * profilesSampler}. On other platforms, transaction profiling takes precedence. */ public void setProfileSessionSampleRate(final @Nullable Double profileSessionSampleRate) { if (!SampleRateUtils.isValidContinuousProfilesSampleRate(profileSessionSampleRate)) { @@ -2398,7 +2398,8 @@ public void setStartProfilerOnAppStart(final boolean startProfilerOnAppStart) { * used for continuous profiling regardless of this setting) as well as transaction-based * profiling ({@code profilesSampleRate}/{@code profilesSampler}) on all devices, since * transaction-based profiling always relies on the legacy profiler and is not supported by - * Perfetto. + * Perfetto. When both profiling modes are configured on API 35 and newer, continuous profiling + * takes precedence. * * @return true if legacy profiling is enabled. The default is false. * @deprecated The legacy profiler can cause Android Runtime crashes and will be removed in @@ -2415,7 +2416,8 @@ public boolean isEnableLegacyProfiling() { * continuous profiling on devices below API 35 as well as transaction-based profiling ({@code * profilesSampleRate}/{@code profilesSampler}) on all devices. On API 35+ devices, Android's * {@code ProfilingManager} / Perfetto is always used for continuous profiling and this setting - * has no effect on it. + * has no effect on it. When both profiling modes are configured on API 35 and newer, continuous + * profiling takes precedence. * * @param enableLegacyProfiling true to enable legacy profiling. * @deprecated The legacy profiler can cause Android Runtime crashes and will be removed in