Skip to content

Commit dd735c2

Browse files
committed
added isStartProfilerOnAppStart experimental option
1 parent 79742ca commit dd735c2

6 files changed

Lines changed: 72 additions & 3 deletions

File tree

sentry-android-core/src/main/java/io/sentry/android/core/ManifestMetadataReader.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,8 @@ final class ManifestMetadataReader {
7070

7171
static final String PROFILE_LIFECYCLE = "io.sentry.traces.profiling.lifecycle";
7272

73+
static final String PROFILER_START_ON_APP_START = "io.sentry.traces.profiling.start-on-app-start";
74+
7375
@ApiStatus.Experimental static final String TRACE_SAMPLING = "io.sentry.traces.trace-sampling";
7476
static final String TRACE_PROPAGATION_TARGETS = "io.sentry.traces.trace-propagation-targets";
7577

@@ -342,6 +344,10 @@ static void applyMetadata(
342344
ProfileLifecycle.valueOf(profileLifecycle.toUpperCase(Locale.ROOT)));
343345
}
344346

347+
options.getExperimental().setStartProfilerOnAppStart(
348+
readBool(
349+
metadata, logger, PROFILER_START_ON_APP_START, options.isStartProfilerOnAppStart()));
350+
345351
options.setEnableUserInteractionTracing(
346352
readBool(metadata, logger, TRACES_UI_ENABLE, options.isEnableUserInteractionTracing()));
347353

sentry-android-core/src/test/java/io/sentry/android/core/ManifestMetadataReaderTest.kt

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -868,6 +868,31 @@ class ManifestMetadataReaderTest {
868868
assertEquals(ProfileLifecycle.TRACE, fixture.options.profileLifecycle)
869869
}
870870

871+
@Test
872+
fun `applyMetadata without specifying isStartProfilerOnAppStart, stays false`() {
873+
// Arrange
874+
val context = fixture.getContext()
875+
876+
// Act
877+
ManifestMetadataReader.applyMetadata(context, fixture.options, fixture.buildInfoProvider)
878+
879+
// Assert
880+
assertFalse(fixture.options.isStartProfilerOnAppStart)
881+
}
882+
883+
@Test
884+
fun `applyMetadata reads isStartProfilerOnAppStart from metadata`() {
885+
// Arrange
886+
val bundle = bundleOf(ManifestMetadataReader.PROFILER_START_ON_APP_START to true)
887+
val context = fixture.getContext(metaData = bundle)
888+
889+
// Act
890+
ManifestMetadataReader.applyMetadata(context, fixture.options, fixture.buildInfoProvider)
891+
892+
// Assert
893+
assertTrue(fixture.options.isStartProfilerOnAppStart)
894+
}
895+
871896
@Test
872897
fun `applyMetadata reads tracePropagationTargets to options`() {
873898
// Arrange

sentry/src/main/java/io/sentry/ExperimentalOptions.java

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,13 @@ public final class ExperimentalOptions {
2727
*/
2828
private @NotNull ProfileLifecycle profileLifecycle = ProfileLifecycle.MANUAL;
2929

30+
/**
31+
* Whether profiling can automatically be started as early as possible during the app lifecycle, to capture more of app startup.
32+
* If {@link ExperimentalOptions#profileLifecycle} is {@link ProfileLifecycle#MANUAL} Profiling is started automatically on startup and stopProfileSession must be called manually whenever the app startup is completed
33+
* If {@link ExperimentalOptions#profileLifecycle} is {@link ProfileLifecycle#TRACE} Profiling is started automatically on startup, and will automatically be stopped when the root span that is associated with app startup ends
34+
*/
35+
private boolean startProfilerOnAppStart = false;
36+
3037
public ExperimentalOptions(final boolean empty) {
3138
this.sessionReplay = new SentryReplayOptions(empty);
3239
}
@@ -74,4 +81,14 @@ public void setProfileSessionSampleRate(final @Nullable Double profileSessionSam
7481
}
7582
this.profileSessionSampleRate = profileSessionSampleRate;
7683
}
84+
85+
@ApiStatus.Experimental
86+
public boolean isStartProfilerOnAppStart() {
87+
return startProfilerOnAppStart;
88+
}
89+
90+
@ApiStatus.Experimental
91+
public void setStartProfilerOnAppStart(boolean startProfilerOnAppStart) {
92+
this.startProfilerOnAppStart = startProfilerOnAppStart;
93+
}
7794
}

sentry/src/main/java/io/sentry/Sentry.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -370,9 +370,10 @@ private static void handleAppStartProfilingConfig(
370370
try {
371371
// We always delete the config file for app start profiling
372372
FileUtils.deleteRecursively(appStartProfilingConfigFile);
373-
if (!options.isEnableAppStartProfiling()) {
373+
if (!options.isEnableAppStartProfiling() && !options.isStartProfilerOnAppStart()) {
374374
return;
375375
}
376+
todo isStartProfilerOnAppStart doesn't need tracing! - SentryTest takes hours to run!
376377
if (!options.isTracingEnabled()) {
377378
options
378379
.getLogger()
@@ -382,8 +383,8 @@ private static void handleAppStartProfilingConfig(
382383
return;
383384
}
384385
if (appStartProfilingConfigFile.createNewFile()) {
385-
final @NotNull TracesSamplingDecision appStartSamplingDecision =
386-
sampleAppStartProfiling(options);
386+
// If old app start profiling is false, it means the transaction will not be sampled, but we create the file anyway to allow continuous profiling on app start
387+
final @NotNull TracesSamplingDecision appStartSamplingDecision = options.isEnableAppStartProfiling() ? sampleAppStartProfiling(options) : new TracesSamplingDecision(false);
387388
final @NotNull SentryAppStartProfilingOptions appStartProfilingOptions =
388389
new SentryAppStartProfilingOptions(options, appStartSamplingDecision);
389390
try (final OutputStream outputStream =

sentry/src/main/java/io/sentry/SentryOptions.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1813,6 +1813,12 @@ public void setProfilesSampleRate(final @Nullable Double profilesSampleRate) {
18131813
return experimental.getProfileLifecycle();
18141814
}
18151815

1816+
/** Whether profiling can automatically be started as early as possible during the app lifecycle. */
1817+
@ApiStatus.Experimental
1818+
public boolean isStartProfilerOnAppStart() {
1819+
return experimental.isStartProfilerOnAppStart();
1820+
}
1821+
18161822
/**
18171823
* Returns the profiling traces dir. path if set
18181824
*

sentry/src/test/java/io/sentry/SentryOptionsTest.kt

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -303,6 +303,20 @@ class SentryOptionsTest {
303303
assertEquals(ProfileLifecycle.MANUAL, options.profileLifecycle)
304304
}
305305

306+
@Test
307+
fun `when isStartProfilerOnAppStart is set to a value, value is set`() {
308+
val options = SentryOptions().apply {
309+
this.experimental.isStartProfilerOnAppStart = true
310+
}
311+
assertTrue(options.isStartProfilerOnAppStart)
312+
}
313+
314+
@Test
315+
fun `isStartProfilerOnAppStart defaults to false`() {
316+
val options = SentryOptions()
317+
assertFalse(options.isStartProfilerOnAppStart)
318+
}
319+
306320
@Test
307321
fun `when options is initialized, compositePerformanceCollector is set`() {
308322
assertIs<CompositePerformanceCollector>(SentryOptions().compositePerformanceCollector)

0 commit comments

Comments
 (0)