Skip to content

Commit c7164d7

Browse files
committed
merged main and updated tests
1 parent 1206307 commit c7164d7

File tree

14 files changed

+261
-66
lines changed

14 files changed

+261
-66
lines changed

CHANGELOG.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,16 @@
1010
Note: Both `options.profilesSampler` and `options.profilesSampleRate` must **not** be set to enable Continuous Profiling.
1111

1212
```java
13+
import io.sentry.ProfileLifecycle;
1314
import io.sentry.android.core.SentryAndroid;
1415

1516
SentryAndroid.init(context) { options ->
1617

1718
// Currently under experimental options:
1819
options.getExperimental().setProfileSessionSampleRate(1.0);
20+
// In manual mode, you need to start and stop the profiler manually using Sentry.startProfileSession and Sentry.stopProfileSession
21+
// In trace mode, the profiler will start and stop automatically whenever a sampled trace starts and finishes
22+
options.getExperimental().setProfileLifecycle(ProfileLifecycle.MANUAL);
1923
}
2024
// Start profiling
2125
Sentry.startProfileSession();
@@ -24,12 +28,16 @@
2428
Sentry.stopProfileSession();
2529
```
2630
```kotlin
31+
import io.sentry.ProfileLifecycle
2732
import io.sentry.android.core.SentryAndroid
2833

2934
SentryAndroid.init(context) { options ->
3035

3136
// Currently under experimental options:
3237
options.experimental.profileSessionSampleRate = 1.0
38+
// In manual mode, you need to start and stop the profiler manually using Sentry.startProfileSession and Sentry.stopProfileSession
39+
// In trace mode, the profiler will start and stop automatically whenever a sampled trace starts and finishes
40+
options.experimental.profileLifecycle = ProfileLifecycle.MANUAL
3341
}
3442
// Start profiling
3543
Sentry.startProfileSession()

gradle/wrapper/gradle-wrapper.properties

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1+
#Mon Mar 17 13:40:54 CET 2025
12
distributionBase=GRADLE_USER_HOME
23
distributionPath=wrapper/dists
3-
distributionUrl=https\://services.gradle.org/distributions/gradle-8.13-bin.zip
4+
distributionUrl=https\://services.gradle.org/distributions/gradle-8.7-bin.zip
45
networkTimeout=10000
56
validateDistributionUrl=true
67
zipStoreBase=GRADLE_USER_HOME

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
import io.sentry.android.core.internal.util.SentryFrameMetricsCollector;
2525
import io.sentry.protocol.SentryId;
2626
import io.sentry.transport.RateLimiter;
27+
import io.sentry.util.SentryRandom;
2728
import java.util.ArrayList;
2829
import java.util.List;
2930
import java.util.concurrent.Future;
@@ -109,7 +110,7 @@ public synchronized void startProfileSession(
109110
final @NotNull ProfileLifecycle profileLifecycle,
110111
final @NotNull TracesSampler tracesSampler) {
111112
if (shouldSample) {
112-
isSampled = tracesSampler.sampleSessionProfile();
113+
isSampled = tracesSampler.sampleSessionProfile(SentryRandom.current().nextDouble());
113114
shouldSample = false;
114115
}
115116
if (!isSampled) {

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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
import android.net.Uri;
1010
import android.os.Process;
1111
import android.os.SystemClock;
12-
import androidx.annotation.NonNull;
1312
import io.sentry.IContinuousProfiler;
1413
import io.sentry.ILogger;
1514
import io.sentry.ISentryLifecycleToken;

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

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@ import io.sentry.test.DeferredExecutorService
2727
import io.sentry.test.getProperty
2828
import io.sentry.transport.RateLimiter
2929
import org.junit.runner.RunWith
30+
import org.mockito.Mockito
31+
import org.mockito.Mockito.mockStatic
3032
import org.mockito.kotlin.any
3133
import org.mockito.kotlin.check
3234
import org.mockito.kotlin.eq
@@ -59,6 +61,7 @@ class AndroidContinuousProfilerTest {
5961
val buildInfo = mock<BuildInfoProvider> {
6062
whenever(it.sdkInfoVersion).thenReturn(Build.VERSION_CODES.LOLLIPOP_MR1)
6163
}
64+
val mockedSentry = mockStatic(Sentry::class.java)
6265
val mockLogger = mock<ILogger>()
6366
val mockTracesSampler = mock<TracesSampler>()
6467

@@ -77,7 +80,7 @@ class AndroidContinuousProfilerTest {
7780
}
7881

7982
init {
80-
whenever(mockTracesSampler.sampleSessionProfile()).thenReturn(true)
83+
whenever(mockTracesSampler.sampleSessionProfile(any())).thenReturn(true)
8184
}
8285

8386
fun getSut(buildInfoProvider: BuildInfoProvider = buildInfo, optionConfig: ((options: SentryAndroidOptions) -> Unit) = {}): AndroidContinuousProfiler {
@@ -133,11 +136,14 @@ class AndroidContinuousProfilerTest {
133136
File(fixture.options.profilingTracesDirPath!!).mkdirs()
134137

135138
Sentry.setCurrentScopes(fixture.scopes)
139+
140+
fixture.mockedSentry.`when`<Any> { Sentry.getCurrentScopes() }.thenReturn(fixture.scopes)
136141
}
137142

138143
@AfterTest
139144
fun clear() {
140145
context.cacheDir.deleteRecursively()
146+
fixture.mockedSentry.close()
141147
}
142148

143149
@Test
@@ -189,7 +195,7 @@ class AndroidContinuousProfilerTest {
189195
@Test
190196
fun `profiler logs a warning on start if not sampled`() {
191197
val profiler = fixture.getSut()
192-
whenever(fixture.mockTracesSampler.sampleSessionProfile()).thenReturn(false)
198+
whenever(fixture.mockTracesSampler.sampleSessionProfile(any())).thenReturn(false)
193199
profiler.startProfileSession(ProfileLifecycle.MANUAL, fixture.mockTracesSampler)
194200
assertFalse(profiler.isRunning)
195201
verify(fixture.mockLogger).log(eq(SentryLevel.DEBUG), eq("Profiler was not started due to sampling decision."))
@@ -198,28 +204,28 @@ class AndroidContinuousProfilerTest {
198204
@Test
199205
fun `profiler evaluates sessionSampleRate only the first time`() {
200206
val profiler = fixture.getSut()
201-
verify(fixture.mockTracesSampler, never()).sampleSessionProfile()
207+
verify(fixture.mockTracesSampler, never()).sampleSessionProfile(any())
202208
// The first time the profiler is started, the sessionSampleRate is evaluated
203209
profiler.startProfileSession(ProfileLifecycle.MANUAL, fixture.mockTracesSampler)
204-
verify(fixture.mockTracesSampler, times(1)).sampleSessionProfile()
210+
verify(fixture.mockTracesSampler, times(1)).sampleSessionProfile(any())
205211
// Then, the sessionSampleRate is not evaluated again
206212
profiler.startProfileSession(ProfileLifecycle.MANUAL, fixture.mockTracesSampler)
207-
verify(fixture.mockTracesSampler, times(1)).sampleSessionProfile()
213+
verify(fixture.mockTracesSampler, times(1)).sampleSessionProfile(any())
208214
}
209215

210216
@Test
211217
fun `when reevaluateSampling, profiler evaluates sessionSampleRate on next start`() {
212218
val profiler = fixture.getSut()
213-
verify(fixture.mockTracesSampler, never()).sampleSessionProfile()
219+
verify(fixture.mockTracesSampler, never()).sampleSessionProfile(any())
214220
// The first time the profiler is started, the sessionSampleRate is evaluated
215221
profiler.startProfileSession(ProfileLifecycle.MANUAL, fixture.mockTracesSampler)
216-
verify(fixture.mockTracesSampler, times(1)).sampleSessionProfile()
222+
verify(fixture.mockTracesSampler, times(1)).sampleSessionProfile(any())
217223
// When reevaluateSampling is called, the sessionSampleRate is not evaluated immediately
218224
profiler.reevaluateSampling()
219-
verify(fixture.mockTracesSampler, times(1)).sampleSessionProfile()
225+
verify(fixture.mockTracesSampler, times(1)).sampleSessionProfile(any())
220226
// Then, when the profiler starts again, the sessionSampleRate is reevaluated
221227
profiler.startProfileSession(ProfileLifecycle.MANUAL, fixture.mockTracesSampler)
222-
verify(fixture.mockTracesSampler, times(2)).sampleSessionProfile()
228+
verify(fixture.mockTracesSampler, times(2)).sampleSessionProfile(any())
223229
}
224230

225231
@Test
@@ -538,4 +544,9 @@ class AndroidContinuousProfilerTest {
538544
assertEquals(SentryId.EMPTY_ID, profiler.profilerId)
539545
verify(fixture.mockLogger).log(eq(SentryLevel.WARNING), eq("Device is offline. Stopping profiler."))
540546
}
547+
548+
fun withMockScopes(closure: () -> Unit) = Mockito.mockStatic(Sentry::class.java).use {
549+
it.`when`<Any> { Sentry.getCurrentScopes() }.thenReturn(fixture.scopes)
550+
closure.invoke()
551+
}
541552
}

sentry-android-core/src/test/java/io/sentry/android/core/performance/AppStartMetricsTest.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -269,7 +269,7 @@ class AppStartMetricsTest {
269269
whenever(profiler.isRunning).thenReturn(true)
270270
AppStartMetrics.getInstance().appStartContinuousProfiler = profiler
271271

272-
AppStartMetrics.getInstance().registerApplicationForegroundCheck(mock())
272+
AppStartMetrics.getInstance().registerLifecycleCallbacks(mock())
273273
// Job on main thread checks if activity was launched
274274
Shadows.shadowOf(Looper.getMainLooper()).idle()
275275

sentry-spring/src/test/kotlin/io/sentry/spring/webflux/SentryWebFluxTracingFilterTest.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -249,7 +249,7 @@ class SentryWebFluxTracingFilterTest {
249249
verify(fixture.chain).filter(fixture.exchange)
250250

251251
verify(fixture.scopes).isEnabled
252-
verify(fixture.scopes, times(3)).options
252+
verify(fixture.scopes, times(4)).options
253253
verify(fixture.scopes).continueTrace(anyOrNull(), anyOrNull())
254254
verify(fixture.scopes).addBreadcrumb(any<Breadcrumb>(), any<Hint>())
255255
verify(fixture.scopes).configureScope(any<ScopeCallback>())

0 commit comments

Comments
 (0)