Skip to content

Commit 815e034

Browse files
adinauerclaude
andauthored
feat(options): Add support for SENTRY_SAMPLE_RATE environment variable / sample-rate property (#5112)
* feat: Add support for SENTRY_SAMPLE_RATE environment variable Add `sampleRate` to `ExternalOptions` so it can be configured via the `SENTRY_SAMPLE_RATE` environment variable or `sample-rate` property, matching the behavior of other sample rate options. Fixes GH-5091 Co-Authored-By: Claude <noreply@anthropic.com> * docs: Add changelog entry for SENTRY_SAMPLE_RATE support Co-Authored-By: Claude <noreply@anthropic.com> --------- Co-authored-by: Claude <noreply@anthropic.com>
1 parent b8bd880 commit 815e034

File tree

6 files changed

+23
-0
lines changed

6 files changed

+23
-0
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
### Features
66

7+
- Add support for `SENTRY_SAMPLE_RATE` environment variable / `sample-rate` property ([#5112](https://github.com/getsentry/sentry-java/pull/5112))
78
- Create `sentry-opentelemetry-otlp` and `sentry-opentelemetry-otlp-spring` modules for combining OpenTelemetry SDK OTLP export with Sentry SDK ([#5100](https://github.com/getsentry/sentry-java/pull/5100))
89
- OpenTelemetry is configured to send spans to Sentry directly using an OTLP endpoint.
910
- Sentry only uses trace and span ID from OpenTelemetry (via `OpenTelemetryOtlpEventProcessor`) but will not send spans through OpenTelemetry nor use OpenTelemetry `Context` for `Scopes` propagation.

sentry/api/sentry.api

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -504,6 +504,7 @@ public final class io/sentry/ExternalOptions {
504504
public fun getProguardUuid ()Ljava/lang/String;
505505
public fun getProxy ()Lio/sentry/SentryOptions$Proxy;
506506
public fun getRelease ()Ljava/lang/String;
507+
public fun getSampleRate ()Ljava/lang/Double;
507508
public fun getSendClientReports ()Ljava/lang/Boolean;
508509
public fun getServerName ()Ljava/lang/String;
509510
public fun getSpotlightConnectionUrl ()Ljava/lang/String;
@@ -552,6 +553,7 @@ public final class io/sentry/ExternalOptions {
552553
public fun setProguardUuid (Ljava/lang/String;)V
553554
public fun setProxy (Lio/sentry/SentryOptions$Proxy;)V
554555
public fun setRelease (Ljava/lang/String;)V
556+
public fun setSampleRate (Ljava/lang/Double;)V
555557
public fun setSendClientReports (Ljava/lang/Boolean;)V
556558
public fun setSendDefaultPii (Ljava/lang/Boolean;)V
557559
public fun setSendModules (Ljava/lang/Boolean;)V

sentry/src/main/java/io/sentry/ExternalOptions.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ public final class ExternalOptions {
2323
private @Nullable Boolean enableUncaughtExceptionHandler;
2424
private @Nullable Boolean debug;
2525
private @Nullable Boolean enableDeduplication;
26+
private @Nullable Double sampleRate;
2627
private @Nullable Double tracesSampleRate;
2728
private @Nullable Double profilesSampleRate;
2829
private @Nullable SentryOptions.RequestSize maxRequestBodySize;
@@ -77,6 +78,7 @@ public final class ExternalOptions {
7778
propertiesProvider.getBooleanProperty("uncaught.handler.enabled"));
7879
options.setPrintUncaughtStackTrace(
7980
propertiesProvider.getBooleanProperty("uncaught.handler.print-stacktrace"));
81+
options.setSampleRate(propertiesProvider.getDoubleProperty("sample-rate"));
8082
options.setTracesSampleRate(propertiesProvider.getDoubleProperty("traces-sample-rate"));
8183
options.setProfilesSampleRate(propertiesProvider.getDoubleProperty("profiles-sample-rate"));
8284
options.setDebug(propertiesProvider.getBooleanProperty("debug"));
@@ -295,6 +297,14 @@ public void setEnableDeduplication(final @Nullable Boolean enableDeduplication)
295297
this.enableDeduplication = enableDeduplication;
296298
}
297299

300+
public @Nullable Double getSampleRate() {
301+
return sampleRate;
302+
}
303+
304+
public void setSampleRate(final @Nullable Double sampleRate) {
305+
this.sampleRate = sampleRate;
306+
}
307+
298308
public @Nullable Double getTracesSampleRate() {
299309
return tracesSampleRate;
300310
}

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3378,6 +3378,9 @@ public void merge(final @NotNull ExternalOptions options) {
33783378
if (options.getPrintUncaughtStackTrace() != null) {
33793379
setPrintUncaughtStackTrace(options.getPrintUncaughtStackTrace());
33803380
}
3381+
if (options.getSampleRate() != null) {
3382+
setSampleRate(options.getSampleRate());
3383+
}
33813384
if (options.getTracesSampleRate() != null) {
33823385
setTracesSampleRate(options.getTracesSampleRate());
33833386
}

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,11 @@ class ExternalOptionsTest {
101101
}
102102
}
103103

104+
@Test
105+
fun `creates options with sampleRate using external properties`() {
106+
withPropertiesFile("sample-rate=0.2") { assertEquals(0.2, it.sampleRate) }
107+
}
108+
104109
@Test
105110
fun `creates options with tracesSampleRate using external properties`() {
106111
withPropertiesFile("traces-sample-rate=0.2") { assertEquals(0.2, it.tracesSampleRate) }

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -376,6 +376,7 @@ class SentryOptionsTest {
376376
externalOptions.setTag("tag1", "value1")
377377
externalOptions.setTag("tag2", "value2")
378378
externalOptions.enableUncaughtExceptionHandler = false
379+
externalOptions.sampleRate = 0.3
379380
externalOptions.tracesSampleRate = 0.5
380381
externalOptions.profilesSampleRate = 0.5
381382
externalOptions.addInAppInclude("com.app")
@@ -433,6 +434,7 @@ class SentryOptionsTest {
433434
assertEquals(java.net.Proxy.Type.SOCKS, options.proxy!!.type)
434435
assertEquals(mapOf("tag1" to "value1", "tag2" to "value2"), options.tags)
435436
assertFalse(options.isEnableUncaughtExceptionHandler)
437+
assertEquals(0.3, options.sampleRate)
436438
assertEquals(0.5, options.tracesSampleRate)
437439
assertEquals(0.5, options.profilesSampleRate)
438440
assertEquals(listOf("com.app"), options.inAppIncludes)

0 commit comments

Comments
 (0)