Skip to content

Commit 5c2c846

Browse files
committed
Merge branch 'feat/continuous-profiling-part1' into feat/continuous-profiling-lifecycle
# Conflicts: # CHANGELOG.md # sentry-android-core/api/sentry-android-core.api # sentry-android-core/src/main/java/io/sentry/android/core/AndroidContinuousProfiler.java # sentry-android-core/src/main/java/io/sentry/android/core/ManifestMetadataReader.java # sentry-android-core/src/main/java/io/sentry/android/core/SentryPerformanceProvider.java # sentry-android-core/src/test/java/io/sentry/android/core/AndroidContinuousProfilerTest.kt # sentry-android-core/src/test/java/io/sentry/android/core/ManifestMetadataReaderTest.kt # sentry/api/sentry.api # sentry/src/main/java/io/sentry/ExperimentalOptions.java # sentry/src/main/java/io/sentry/IContinuousProfiler.java # sentry/src/main/java/io/sentry/NoOpContinuousProfiler.java # sentry/src/main/java/io/sentry/Scopes.java # sentry/src/main/java/io/sentry/SentryOptions.java # sentry/src/main/java/io/sentry/TracesSampler.java # sentry/src/test/java/io/sentry/NoOpContinuousProfilerTest.kt # sentry/src/test/java/io/sentry/ScopesTest.kt # sentry/src/test/java/io/sentry/SentryOptionsTest.kt # sentry/src/test/java/io/sentry/SentryTest.kt
2 parents fbe53eb + 1440de9 commit 5c2c846

10 files changed

Lines changed: 42 additions & 27 deletions

File tree

CHANGELOG.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
- Add Continuous Profiling Support ([#3710](https://github.com/getsentry/sentry-java/pull/3710))
88

9-
To enable Continuous Profiling use the `Sentry.startProfileSession` and `Sentry.stopProfileSession` experimental APIs. Sampling rate can be set through `options.profileSessionSampleRate` (defaults to 0.0).
9+
To enable Continuous Profiling use the `Sentry.startProfileSession` and `Sentry.stopProfileSession` experimental APIs. Sampling rate can be set through `options.profileSessionSampleRate`, which defaults to null (disabled).
1010
Note: Both `options.profilesSampler` and `options.profilesSampleRate` must **not** be set to enable Continuous Profiling.
1111

1212
```java

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -321,7 +321,7 @@ static void applyMetadata(
321321
}
322322
}
323323

324-
if (options.getProfileSessionSampleRate() == 0.0) {
324+
if (options.getProfileSessionSampleRate() == null) {
325325
final double profileSessionSampleRate =
326326
readDouble(metadata, logger, PROFILE_SESSION_SAMPLE_RATE);
327327
if (profileSessionSampleRate != -1) {

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -185,7 +185,7 @@ private void createAndStartContinuousProfiler(
185185
// Let's fake a sampler to accept the sampling decision that was calculated on last run
186186
sentryOptions
187187
.getExperimental()
188-
.setProfileSessionSampleRate(profilingOptions.isContinuousProfileSampled() ? 1 : 0);
188+
.setProfileSessionSampleRate(profilingOptions.isContinuousProfileSampled() ? 1.0 : 0.0);
189189
appStartContinuousProfiler.startProfileSession(
190190
profilingOptions.getProfileLifecycle(), new TracesSampler(sentryOptions));
191191
}

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

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -831,41 +831,41 @@ class ManifestMetadataReaderTest {
831831
}
832832

833833
@Test
834-
fun `applyMetadata without specifying profileLifecycle, stays MANUAL`() {
834+
fun `applyMetadata without specifying profileSessionSampleRate, stays null`() {
835835
// Arrange
836836
val context = fixture.getContext()
837837

838838
// Act
839839
ManifestMetadataReader.applyMetadata(context, fixture.options, fixture.buildInfoProvider)
840840

841841
// Assert
842-
assertEquals(ProfileLifecycle.MANUAL, fixture.options.profileLifecycle)
842+
assertNull(fixture.options.profileSessionSampleRate)
843843
}
844844

845845
@Test
846-
fun `applyMetadata reads profileLifecycle from metadata`() {
846+
fun `applyMetadata without specifying profileLifecycle, stays MANUAL`() {
847847
// Arrange
848-
val expectedLifecycle = "trace"
849-
val bundle = bundleOf(ManifestMetadataReader.PROFILE_LIFECYCLE to expectedLifecycle)
850-
val context = fixture.getContext(metaData = bundle)
848+
val context = fixture.getContext()
851849

852850
// Act
853851
ManifestMetadataReader.applyMetadata(context, fixture.options, fixture.buildInfoProvider)
854852

855853
// Assert
856-
assertEquals(ProfileLifecycle.TRACE, fixture.options.profileLifecycle)
854+
assertEquals(ProfileLifecycle.MANUAL, fixture.options.profileLifecycle)
857855
}
858856

859857
@Test
860-
fun `applyMetadata without specifying profileSessionSampleRate, stays 0`() {
858+
fun `applyMetadata reads profileLifecycle from metadata`() {
861859
// Arrange
862-
val context = fixture.getContext()
860+
val expectedLifecycle = "trace"
861+
val bundle = bundleOf(ManifestMetadataReader.PROFILE_LIFECYCLE to expectedLifecycle)
862+
val context = fixture.getContext(metaData = bundle)
863863

864864
// Act
865865
ManifestMetadataReader.applyMetadata(context, fixture.options, fixture.buildInfoProvider)
866866

867867
// Assert
868-
assertEquals(0.0, fixture.options.profileSessionSampleRate)
868+
assertEquals(ProfileLifecycle.TRACE, fixture.options.profileLifecycle)
869869
}
870870

871871
@Test

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

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import io.sentry.util.SampleRateUtils;
44
import org.jetbrains.annotations.ApiStatus;
55
import org.jetbrains.annotations.NotNull;
6+
import org.jetbrains.annotations.Nullable;
67

78
/**
89
* Experimental options for new features, these options are going to be promoted to SentryOptions
@@ -15,10 +16,10 @@ public final class ExperimentalOptions {
1516

1617
/**
1718
* Indicates the percentage in which the profiles for the session will be created. Specifying 0
18-
* means never, 1.0 means always. The value needs to be >= 0.0 and <= 1.0 The default is 0
19+
* means never, 1.0 means always. The value needs to be >= 0.0 and <= 1.0 The default is null
1920
* (disabled).
2021
*/
21-
private double profileSessionSampleRate = 0.0;
22+
private @Nullable Double profileSessionSampleRate;
2223

2324
/**
2425
* Whether the profiling lifecycle is controlled manually or based on the trace lifecycle.
@@ -60,12 +61,12 @@ public void setProfileLifecycle(final @NotNull ProfileLifecycle profileLifecycle
6061
}
6162

6263
@ApiStatus.Experimental
63-
public double getProfileSessionSampleRate() {
64+
public @Nullable Double getProfileSessionSampleRate() {
6465
return profileSessionSampleRate;
6566
}
6667

6768
@ApiStatus.Experimental
68-
public void setProfileSessionSampleRate(final double profileSessionSampleRate) {
69+
public void setProfileSessionSampleRate(final @Nullable Double profileSessionSampleRate) {
6970
if (!SampleRateUtils.isValidContinuousProfilesSampleRate(profileSessionSampleRate)) {
7071
throw new IllegalArgumentException(
7172
"The value "

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

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1742,6 +1742,7 @@ public boolean isProfilingEnabled() {
17421742
public boolean isContinuousProfilingEnabled() {
17431743
return profilesSampleRate == null
17441744
&& profilesSampler == null
1745+
&& experimental.getProfileSessionSampleRate() != null
17451746
&& experimental.getProfileSessionSampleRate() > 0;
17461747
}
17471748

@@ -1790,14 +1791,14 @@ public void setProfilesSampleRate(final @Nullable Double profilesSampleRate) {
17901791
}
17911792

17921793
/**
1793-
* Returns the session sample rate. Default is 0 (disabled). ProfilesSampleRate takes precedence
1794-
* over this. To enable continuous profiling, don't set profilesSampleRate or profilesSampler, or
1795-
* set them to null.
1794+
* Returns the session sample rate. Default is null (disabled). ProfilesSampleRate takes
1795+
* precedence over this. To enable continuous profiling, don't set profilesSampleRate or
1796+
* profilesSampler, or set them to null.
17961797
*
17971798
* @return the sample rate
17981799
*/
17991800
@ApiStatus.Experimental
1800-
public double getProfileSessionSampleRate() {
1801+
public @Nullable Double getProfileSessionSampleRate() {
18011802
return experimental.getProfileSessionSampleRate();
18021803
}
18031804

@@ -1808,7 +1809,7 @@ public double getProfileSessionSampleRate() {
18081809
* @return the profile lifecycle
18091810
*/
18101811
@ApiStatus.Experimental
1811-
public ProfileLifecycle getProfileLifecycle() {
1812+
public @NotNull ProfileLifecycle getProfileLifecycle() {
18121813
return experimental.getProfileLifecycle();
18131814
}
18141815

sentry/src/main/java/io/sentry/TracesSampler.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -86,8 +86,8 @@ public TracesSamplingDecision sample(final @NotNull SamplingContext samplingCont
8686
}
8787

8888
public boolean sampleSessionProfile() {
89-
final double sampling = options.getProfileSessionSampleRate();
90-
return sample(sampling);
89+
final @Nullable Double sampling = options.getProfileSessionSampleRate();
90+
return sampling != null && sample(sampling);
9191
}
9292

9393
private boolean sample(final @NotNull Double aDouble) {

sentry/src/main/java/io/sentry/util/SampleRateUtils.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,8 @@ public static boolean isValidProfilesSampleRate(@Nullable Double profilesSampleR
2323
return isValidRate(profilesSampleRate, true);
2424
}
2525

26-
public static boolean isValidContinuousProfilesSampleRate(double profilesSampleRate) {
27-
return isValidRate(profilesSampleRate, false);
26+
public static boolean isValidContinuousProfilesSampleRate(@Nullable Double profilesSampleRate) {
27+
return isValidRate(profilesSampleRate, true);
2828
}
2929

3030
private static boolean isValidRate(final @Nullable Double rate, final boolean allowNull) {

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

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -237,14 +237,22 @@ class SentryOptionsTest {
237237
}
238238

239239
@Test
240-
fun `when profileSessionSampleRate is set to a 0, isProfilingEnabled is false and isContinuousProfilingEnabled is false`() {
240+
fun `when profileSessionSampleRate is set to 0, isProfilingEnabled is false and isContinuousProfilingEnabled is false`() {
241241
val options = SentryOptions().apply {
242242
this.experimental.profileSessionSampleRate = 0.0
243243
}
244244
assertFalse(options.isProfilingEnabled)
245245
assertFalse(options.isContinuousProfilingEnabled)
246246
}
247247

248+
@Test
249+
fun `when profileSessionSampleRate is null, isProfilingEnabled is false and isContinuousProfilingEnabled is false`() {
250+
val options = SentryOptions()
251+
assertNull(options.experimental.profileSessionSampleRate)
252+
assertFalse(options.isProfilingEnabled)
253+
assertFalse(options.isContinuousProfilingEnabled)
254+
}
255+
248256
@Test
249257
fun `when setProfilesSampleRate is set to exactly 0, value is set`() {
250258
val options = SentryOptions().apply {

sentry/src/test/java/io/sentry/util/SampleRateUtilTest.kt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,11 @@ class SampleRateUtilTest {
141141
assertTrue(SampleRateUtils.isValidContinuousProfilesSampleRate(1.0))
142142
}
143143

144+
@Test
145+
fun `accepts null continuous profiles sample rate`() {
146+
assertTrue(SampleRateUtils.isValidProfilesSampleRate(null))
147+
}
148+
144149
@Test
145150
fun `rejects negative continuous profiles sample rate`() {
146151
assertFalse(SampleRateUtils.isValidContinuousProfilesSampleRate(-0.5))

0 commit comments

Comments
 (0)