Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@

### Breaking Changes

- Android: Disable the legacy `Debug`-based profiler by default. Set `enableLegacyProfiling` or the `io.sentry.profiling.enable-legacy-profiling` manifest option to `true` to continue profiling below API 35 or switch to using [UI Profiling](https://docs.sentry.io/platforms/android/profiling/) ([#6837](https://github.com/getsentry/sentry-java/pull/6837))
- `enableLegacyProfiling` is deprecated and will be removed in version 10. Perfetto continuous profiling on API 35 and newer is unaffected.
- Android: Narrow the SDK's consumer keep rules, so R8 can optimize more of your app ([#6787](https://github.com/getsentry/sentry-java/pull/6787))
- Members of `io.sentry` enums are no longer kept. Enum constants can be renamed or removed.
- Only the name and no-arg constructor of `SentryAndroidOptions` are kept, instead of every member of every `SentryOptions` subclass.
Expand Down
13 changes: 7 additions & 6 deletions develop-docs/feature/profiling/perfetto.md
Original file line number Diff line number Diff line change
Expand Up @@ -84,12 +84,13 @@ flowchart TD
## SDK (getsentry/sentry-java)

On API 35+, [`AndroidOptionsInitializer`](../../../sentry-android-core/src/main/java/io/sentry/android/core/AndroidOptionsInitializer.java)
wires up `PerfettoContinuousProfiler` automatically. On older devices the SDK falls back
to the legacy `Debug`-based [`AndroidContinuousProfiler`](../../../sentry-android-core/src/main/java/io/sentry/android/core/AndroidContinuousProfiler.java),
gated by the `enableLegacyProfiling` option (manifest key
`io.sentry.profiling.enable-legacy-profiling`, defaults to `true`). Only **continuous
profiling** is supported on the Perfetto path — transaction-based and app-start profiling
are not.
wires up `PerfettoContinuousProfiler` automatically. On older devices profiling is disabled
by default. Applications can temporarily opt into the legacy `Debug`-based
[`AndroidContinuousProfiler`](../../../sentry-android-core/src/main/java/io/sentry/android/core/AndroidContinuousProfiler.java)
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.

### Capturing chunks

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -304,37 +304,24 @@ static void initializeIntegrationsAndProcessors(

/** Setup the correct profiler (transaction or continuous) based on the options. */
@SuppressLint("NewApi")
@SuppressWarnings("deprecation")
private static void setupProfiler(
final @NotNull SentryAndroidOptions options,
final @NotNull Context context,
final @NotNull BuildInfoProvider buildInfoProvider,
final @Nullable ITransactionProfiler appStartTransactionProfiler,
final @Nullable IContinuousProfiler appStartContinuousProfiler,
final @NotNull CompositePerformanceCollector performanceCollector) {
if (options.isProfilingEnabled() || options.getProfilesSampleRate() != null) {
final boolean isTransactionProfilingRequested =
options.isProfilingEnabled() || options.getProfilesSampleRate() != null;
if (isTransactionProfilingRequested && options.isEnableLegacyProfiling()) {
options
.getLogger()
.log(
SentryLevel.WARNING,
"The legacy Android profiler is enabled. It is deprecated and will be removed "
+ "in version 10.");
options.setContinuousProfiler(NoOpContinuousProfiler.getInstance());
// Transaction-based profiling always relies on the legacy Debug-based profiler, so it is
// disabled together with legacy profiling. Perfetto profiling only supports continuous
// profiling.
if (!options.isEnableLegacyProfiling()) {
options
.getLogger()
.log(
SentryLevel.WARNING,
"Transaction-based profiling (profilesSampleRate/profilesSampler) is disabled "
+ "because enableLegacyProfiling is false. Transaction-based profiling always "
+ "uses the legacy profiler and is not supported by Perfetto. No profiling "
+ "data will be collected. Use profileSessionSampleRate for continuous "
+ "profiling instead.");
options.setTransactionProfiler(NoOpTransactionProfiler.getInstance());
if (appStartTransactionProfiler != null) {
appStartTransactionProfiler.close();
}
if (appStartContinuousProfiler != null) {
appStartContinuousProfiler.close(true);
}
return;
}
// This is a safeguard, but it should never happen, as the app start profiler should be the
// continuous one.
if (appStartContinuousProfiler != null) {
Expand All @@ -352,54 +339,74 @@ private static void setupProfiler(
options.getFrameMetricsCollector(),
"options.getFrameMetricsCollector is required")));
}
} else {
options.setTransactionProfiler(NoOpTransactionProfiler.getInstance());
// This is a safeguard, but it should never happen, as the app start profiler should be the
// transaction one.
if (appStartTransactionProfiler != null) {
appStartTransactionProfiler.close();
return;
}

options.setTransactionProfiler(NoOpTransactionProfiler.getInstance());
if (appStartTransactionProfiler != null) {
appStartTransactionProfiler.close();
}
if (isTransactionProfilingRequested) {
options
.getLogger()
.log(
SentryLevel.WARNING,
"Transaction-based profiling (profilesSampleRate/profilesSampler) is disabled "
+ "because enableLegacyProfiling is false. Transaction-based profiling always "
+ "uses the legacy profiler and is not supported by Perfetto. To continue "
+ "using it, explicitly set enableLegacyProfiling to true. The legacy profiler "
+ "is deprecated and will be removed in version 10.");
}

if (appStartContinuousProfiler != null && options.isEnableLegacyProfiling()) {
options.setContinuousProfiler(appStartContinuousProfiler);
// If the profiler is running, we start the performance collector too, otherwise we'd miss
// measurements in app launch profiles
final @NotNull SentryId chunkId = appStartContinuousProfiler.getChunkId();
if (appStartContinuousProfiler.isRunning() && !chunkId.equals(SentryId.EMPTY_ID)) {
performanceCollector.start(chunkId.toString());
}
} else {
if (appStartContinuousProfiler != null) {
options.setContinuousProfiler(appStartContinuousProfiler);
// If the profiler is running, we start the performance collector too, otherwise we'd miss
// measurements in app launch profiles
final @NotNull SentryId chunkId = appStartContinuousProfiler.getChunkId();
if (appStartContinuousProfiler.isRunning() && !chunkId.equals(SentryId.EMPTY_ID)) {
performanceCollector.start(chunkId.toString());
}
} else {
final @NotNull SentryFrameMetricsCollector frameMetricsCollector =
Objects.requireNonNull(
options.getFrameMetricsCollector(), "options.getFrameMetricsCollector is required");
if (buildInfoProvider.getSdkInfoVersion() >= Build.VERSION_CODES.VANILLA_ICE_CREAM) {
final @NotNull Context appContext = ContextUtils.getApplicationContext(context);
options.setContinuousProfiler(
new PerfettoContinuousProfiler(
options.getLogger(),
frameMetricsCollector,
() -> options.getExecutorService(),
() ->
new PerfettoProfiler(
appContext, options.getLogger(), options.getExecutorService())));
// Report adoption of the Perfetto continuous profiling backend (API 35+).
addIntegrationToSdkVersion("PerfettoContinuousProfiling");
} else if (options.isEnableLegacyProfiling()) {
options.setContinuousProfiler(
new AndroidContinuousProfiler(
buildInfoProvider,
frameMetricsCollector,
options.getLogger(),
options.getProfilingTracesDirPath(),
options.getProfilingTracesHz(),
() -> options.getExecutorService()));
} else {
options
.getLogger()
.log(
SentryLevel.WARNING,
"enableLegacyProfiling is disabled and device is below API 35. "
+ "No profiling data will be collected.");
}
appStartContinuousProfiler.close(true);
}
final @NotNull SentryFrameMetricsCollector frameMetricsCollector =
Objects.requireNonNull(
options.getFrameMetricsCollector(), "options.getFrameMetricsCollector is required");
if (buildInfoProvider.getSdkInfoVersion() >= Build.VERSION_CODES.VANILLA_ICE_CREAM) {
final @NotNull Context appContext = ContextUtils.getApplicationContext(context);
options.setContinuousProfiler(
new PerfettoContinuousProfiler(
options.getLogger(),
frameMetricsCollector,
() -> options.getExecutorService(),
() ->
new PerfettoProfiler(
appContext, options.getLogger(), options.getExecutorService())));
// Report adoption of the Perfetto continuous profiling backend (API 35+).
addIntegrationToSdkVersion("PerfettoContinuousProfiling");
} else if (options.isEnableLegacyProfiling()) {
options
.getLogger()
.log(
SentryLevel.WARNING,
"The legacy Android profiler is enabled. It is deprecated and will be removed "
+ "in version 10.");
options.setContinuousProfiler(
new AndroidContinuousProfiler(
buildInfoProvider,
frameMetricsCollector,
options.getLogger(),
options.getProfilingTracesDirPath(),
options.getProfilingTracesHz(),
() -> options.getExecutorService()));
} else if (options.isContinuousProfilingEnabled()) {
options
.getLogger()
.log(
SentryLevel.WARNING,
"Continuous profiling is unavailable below API 35 unless the deprecated "
+ "enableLegacyProfiling option is explicitly set to true.");
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -138,11 +138,16 @@ private void launchAppStartProfiler(final @NotNull AppStartMetrics appStartMetri
if (!profilingOptions.isEnableLegacyProfiling()) {
logger.log(
SentryLevel.WARNING,
"enableLegacyProfiling is disabled and device is below API 35. "
+ "App start profiling will not start.");
"App start profiling is unavailable below API 35 unless the deprecated "
+ "enableLegacyProfiling option is explicitly set to true.");
return;
}

logger.log(
SentryLevel.WARNING,
"The legacy Android profiler is enabled. It is deprecated and will be removed in "
+ "version 10.");

if (profilingOptions.isContinuousProfilingEnabled()
&& profilingOptions.isStartProfilerOnAppStart()) {
createAndStartContinuousProfiler(context, profilingOptions, appStartMetrics);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -409,12 +409,11 @@ class AndroidOptionsInitializerTest {
}

@Test
fun `init should set Android continuous profiler`() {
fun `init below API 35 uses no-op profiler by default`() {
fixture.initSut()

assertNotNull(fixture.sentryOptions.transactionProfiler)
assertEquals(fixture.sentryOptions.transactionProfiler, NoOpTransactionProfiler.getInstance())
assertTrue(fixture.sentryOptions.continuousProfiler is AndroidContinuousProfiler)
assertEquals(NoOpTransactionProfiler.getInstance(), fixture.sentryOptions.transactionProfiler)
assertEquals(NoOpContinuousProfiler.getInstance(), fixture.sentryOptions.continuousProfiler)
}

@Config(sdk = [35])
Expand All @@ -424,6 +423,15 @@ class AndroidOptionsInitializerTest {
assertTrue(fixture.sentryOptions.continuousProfiler is PerfettoContinuousProfiler)
}

@Config(sdk = [35])
@Test
fun `init on API 35+ uses Perfetto when transaction profiling is configured by default`() {
fixture.initSut(configureOptions = { profilesSampleRate = 1.0 })

assertEquals(NoOpTransactionProfiler.getInstance(), fixture.sentryOptions.transactionProfiler)
assertTrue(fixture.sentryOptions.continuousProfiler is PerfettoContinuousProfiler)
}

@Config(sdk = [35])
@Test
fun `init on API 35+ reports PerfettoContinuousProfiling integration`() {
Expand Down Expand Up @@ -465,40 +473,50 @@ class AndroidOptionsInitializerTest {
}

@Test
fun `init with profilesSampleRate should set Android transaction profiler`() {
fixture.initSut(configureOptions = { profilesSampleRate = 1.0 })
fun `init with profilesSampleRate and legacy profiling enabled sets transaction profiler`() {
fixture.initSut(
configureOptions = {
profilesSampleRate = 1.0
isEnableLegacyProfiling = true
},
)

assertNotNull(fixture.sentryOptions.transactionProfiler)
assertTrue(fixture.sentryOptions.transactionProfiler is AndroidTransactionProfiler)
assertEquals(fixture.sentryOptions.continuousProfiler, NoOpContinuousProfiler.getInstance())
}

@Test
fun `init with profilesSampleRate 0 should set Android transaction profiler`() {
fixture.initSut(configureOptions = { profilesSampleRate = 0.0 })
fun `init with profilesSampleRate 0 and legacy profiling enabled sets transaction profiler`() {
fixture.initSut(
configureOptions = {
profilesSampleRate = 0.0
isEnableLegacyProfiling = true
},
)

assertNotNull(fixture.sentryOptions.transactionProfiler)
assertTrue(fixture.sentryOptions.transactionProfiler is AndroidTransactionProfiler)
assertEquals(fixture.sentryOptions.continuousProfiler, NoOpContinuousProfiler.getInstance())
}

@Test
fun `init with profilesSampler should set Android transaction profiler`() {
fixture.initSut(configureOptions = { profilesSampler = mock() })
fun `init with profilesSampler and legacy profiling enabled sets transaction profiler`() {
fixture.initSut(
configureOptions = {
profilesSampler = mock()
isEnableLegacyProfiling = true
},
)

assertNotNull(fixture.sentryOptions.transactionProfiler)
assertTrue(fixture.sentryOptions.transactionProfiler is AndroidTransactionProfiler)
assertEquals(fixture.sentryOptions.continuousProfiler, NoOpContinuousProfiler.getInstance())
}

@Test
fun `init with profilesSampleRate and enableLegacyProfiling false noops both profilers`() {
fixture.initSut(
configureOptions = {
profilesSampleRate = 1.0
isEnableLegacyProfiling = false
},
)
fun `init with profilesSampleRate noops both profilers by default`() {
fixture.initSut(configureOptions = { profilesSampleRate = 1.0 })

assertEquals(NoOpTransactionProfiler.getInstance(), fixture.sentryOptions.transactionProfiler)
assertEquals(NoOpContinuousProfiler.getInstance(), fixture.sentryOptions.continuousProfiler)
Expand Down Expand Up @@ -540,7 +558,12 @@ class AndroidOptionsInitializerTest {
fun `init reuses transaction profiler of appStartMetrics, if exists`() {
val appStartProfiler = mock<ITransactionProfiler>()
AppStartMetrics.getInstance().appStartProfiler = appStartProfiler
fixture.initSut(configureOptions = { profilesSampler = mock() })
fixture.initSut(
configureOptions = {
profilesSampler = mock()
isEnableLegacyProfiling = true
},
)

assertEquals(appStartProfiler, fixture.sentryOptions.transactionProfiler)
assertEquals(fixture.sentryOptions.continuousProfiler, NoOpContinuousProfiler.getInstance())
Expand All @@ -554,7 +577,7 @@ class AndroidOptionsInitializerTest {
fun `init reuses continuous profiler of appStartMetrics, if exists`() {
val appStartContinuousProfiler = mock<IContinuousProfiler>()
AppStartMetrics.getInstance().appStartContinuousProfiler = appStartContinuousProfiler
fixture.initSut()
fixture.initSut(configureOptions = { isEnableLegacyProfiling = true })

assertEquals(fixture.sentryOptions.transactionProfiler, NoOpTransactionProfiler.getInstance())
assertEquals(appStartContinuousProfiler, fixture.sentryOptions.continuousProfiler)
Expand All @@ -564,6 +587,25 @@ class AndroidOptionsInitializerTest {
assertNull(AppStartMetrics.getInstance().appStartContinuousProfiler)
}

@Test
fun `init with transaction profiling and legacy disabled closes app start continuous profiler`() {
val appStartContinuousProfiler = mock<IContinuousProfiler>()
AppStartMetrics.getInstance().appStartContinuousProfiler = appStartContinuousProfiler
fixture.initSut(
configureOptions = {
profilesSampler = mock()
isEnableLegacyProfiling = false
},
)

assertEquals(NoOpTransactionProfiler.getInstance(), fixture.sentryOptions.transactionProfiler)
assertEquals(NoOpContinuousProfiler.getInstance(), fixture.sentryOptions.continuousProfiler)
verify(appStartContinuousProfiler).close(eq(true))

assertNull(AppStartMetrics.getInstance().appStartProfiler)
assertNull(AppStartMetrics.getInstance().appStartContinuousProfiler)
}

@Test
fun `init starts performance collector if continuous profiler of appStartMetrics is running`() {
val appStartContinuousProfiler = mock<IContinuousProfiler>()
Expand All @@ -573,7 +615,12 @@ class AndroidOptionsInitializerTest {
whenever(appStartContinuousProfiler.chunkId).thenReturn(chunkId)

AppStartMetrics.getInstance().appStartContinuousProfiler = appStartContinuousProfiler
fixture.initSut(configureOptions = { compositePerformanceCollector = mockPerformanceCollector })
fixture.initSut(
configureOptions = {
compositePerformanceCollector = mockPerformanceCollector
isEnableLegacyProfiling = true
},
)

verify(mockPerformanceCollector).start(eq(chunkId.toString()))
}
Expand All @@ -595,7 +642,12 @@ class AndroidOptionsInitializerTest {
fun `init with transaction profiling closes continuous profiler of appStartMetrics`() {
val appStartContinuousProfiler = mock<IContinuousProfiler>()
AppStartMetrics.getInstance().appStartContinuousProfiler = appStartContinuousProfiler
fixture.initSut(configureOptions = { profilesSampler = mock() })
fixture.initSut(
configureOptions = {
profilesSampler = mock()
isEnableLegacyProfiling = true
},
)

assertNotNull(fixture.sentryOptions.transactionProfiler)
assertNotEquals(
Expand All @@ -616,7 +668,7 @@ class AndroidOptionsInitializerTest {
fun `init with continuous profiling closes transaction profiler of appStartMetrics`() {
val appStartProfiler = mock<ITransactionProfiler>()
AppStartMetrics.getInstance().appStartProfiler = appStartProfiler
fixture.initSut()
fixture.initSut(configureOptions = { isEnableLegacyProfiling = true })

assertEquals(NoOpTransactionProfiler.getInstance(), fixture.sentryOptions.transactionProfiler)
assertNotNull(fixture.sentryOptions.continuousProfiler)
Expand Down
Loading
Loading