Skip to content

Commit dacdab4

Browse files
committed
app going in the background now stops the continuous profiler
1 parent 2d1ee74 commit dacdab4

File tree

9 files changed

+44
-0
lines changed

9 files changed

+44
-0
lines changed

sentry-android-core/api/sentry-android-core.api

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ public class io/sentry/android/core/AndroidContinuousProfiler : io/sentry/IConti
4949
public fun onRateLimitChanged (Lio/sentry/transport/RateLimiter;)V
5050
public fun reevaluateSampling ()V
5151
public fun startProfiler (Lio/sentry/ProfileLifecycle;Lio/sentry/TracesSampler;)V
52+
public fun stopAllProfiles ()V
5253
public fun stopProfiler (Lio/sentry/ProfileLifecycle;)V
5354
}
5455

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -322,6 +322,15 @@ public void reevaluateSampling() {
322322
shouldSample = true;
323323
}
324324

325+
@Override
326+
public void stopAllProfiles() {
327+
try (final @NotNull ISentryLifecycleToken ignored = lock.acquire()) {
328+
rootSpanCounter = 0;
329+
shouldStop = true;
330+
}
331+
}
332+
333+
@Override
325334
public void close() {
326335
try (final @NotNull ISentryLifecycleToken ignored = lock.acquire()) {
327336
rootSpanCounter = 0;

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,7 @@ public void run() {
122122
scopes.endSession();
123123
}
124124
scopes.getOptions().getReplayController().stop();
125+
scopes.getOptions().getContinuousProfiler().stopAllProfiles();
125126
}
126127
};
127128

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

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -470,6 +470,23 @@ class AndroidContinuousProfilerTest {
470470
verify(fixture.scopes).captureProfileChunk(any())
471471
}
472472

473+
@Test
474+
fun `stopAllProfiles stops all profiles after chunk is finished`() {
475+
val profiler = fixture.getSut()
476+
profiler.startProfiler(ProfileLifecycle.MANUAL, fixture.mockTracesSampler)
477+
profiler.startProfiler(ProfileLifecycle.TRACE, fixture.mockTracesSampler)
478+
assertTrue(profiler.isRunning)
479+
// We are scheduling the profiler to stop at the end of the chunk, so it should still be running
480+
profiler.stopAllProfiles()
481+
assertTrue(profiler.isRunning)
482+
// However, stopAllProfiles already resets the rootSpanCounter
483+
assertEquals(0, profiler.rootSpanCounter)
484+
485+
// We run the executor service to trigger the chunk finish, and the profiler shouldn't restart
486+
fixture.executor.runAll()
487+
assertFalse(profiler.isRunning)
488+
}
489+
473490
@Test
474491
fun `profiler does not send chunks after close`() {
475492
val profiler = fixture.getSut()

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package io.sentry.android.core
33
import androidx.lifecycle.LifecycleOwner
44
import io.sentry.Breadcrumb
55
import io.sentry.DateUtils
6+
import io.sentry.IContinuousProfiler
67
import io.sentry.IScope
78
import io.sentry.IScopes
89
import io.sentry.ReplayController
@@ -38,6 +39,7 @@ class LifecycleWatcherTest {
3839
val dateProvider = mock<ICurrentDateProvider>()
3940
val options = SentryOptions()
4041
val replayController = mock<ReplayController>()
42+
val continuousProfiler = mock<IContinuousProfiler>()
4143

4244
fun getSUT(
4345
sessionIntervalMillis: Long = 0L,
@@ -52,6 +54,7 @@ class LifecycleWatcherTest {
5254
argumentCaptor.value.run(scope)
5355
}
5456
options.setReplayController(replayController)
57+
options.setContinuousProfiler(continuousProfiler)
5558
whenever(scopes.options).thenReturn(options)
5659

5760
return LifecycleWatcher(
@@ -106,6 +109,7 @@ class LifecycleWatcherTest {
106109
watcher.onStop(fixture.ownerMock)
107110
verify(fixture.scopes, timeout(10000)).endSession()
108111
verify(fixture.replayController, timeout(10000)).stop()
112+
verify(fixture.continuousProfiler, timeout(10000)).stopAllProfiles()
109113
}
110114

111115
@Test

sentry/api/sentry.api

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -753,6 +753,7 @@ public abstract interface class io/sentry/IContinuousProfiler {
753753
public abstract fun isRunning ()Z
754754
public abstract fun reevaluateSampling ()V
755755
public abstract fun startProfiler (Lio/sentry/ProfileLifecycle;Lio/sentry/TracesSampler;)V
756+
public abstract fun stopAllProfiles ()V
756757
public abstract fun stopProfiler (Lio/sentry/ProfileLifecycle;)V
757758
}
758759

@@ -1437,6 +1438,7 @@ public final class io/sentry/NoOpContinuousProfiler : io/sentry/IContinuousProfi
14371438
public fun isRunning ()Z
14381439
public fun reevaluateSampling ()V
14391440
public fun startProfiler (Lio/sentry/ProfileLifecycle;Lio/sentry/TracesSampler;)V
1441+
public fun stopAllProfiles ()V
14401442
public fun stopProfiler (Lio/sentry/ProfileLifecycle;)V
14411443
}
14421444

sentry/src/main/java/io/sentry/IContinuousProfiler.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,9 @@ void startProfiler(
1919

2020
void reevaluateSampling();
2121

22+
/** Stops all profiles currently running, regardless of the profileLifecycle that started them. */
23+
void stopAllProfiles();
24+
2225
@NotNull
2326
SentryId getProfilerId();
2427
}

sentry/src/main/java/io/sentry/NoOpContinuousProfiler.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,9 @@ public void startProfiler(
2626
final @NotNull ProfileLifecycle profileLifecycle,
2727
final @NotNull TracesSampler tracesSampler) {}
2828

29+
@Override
30+
public void stopAllProfiles() {}
31+
2932
@Override
3033
public void close() {}
3134

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,10 @@ class NoOpContinuousProfilerTest {
1717
fun `stop does not throw`() =
1818
profiler.stopProfiler(mock())
1919

20+
@Test
21+
fun `stopAllProfiles does not throw`() =
22+
profiler.stopAllProfiles()
23+
2024
@Test
2125
fun `isRunning returns false`() {
2226
assertFalse(profiler.isRunning)

0 commit comments

Comments
 (0)