Skip to content

Commit 7682cd0

Browse files
authored
Merge 58e605d into adf8fe3
2 parents adf8fe3 + 58e605d commit 7682cd0

File tree

10 files changed

+86
-4
lines changed

10 files changed

+86
-4
lines changed

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
# Changelog
22

3+
## Unreleased
4+
5+
### Features
6+
7+
- Disable Sentry with SentryOptions ([#2840](https://github.com/getsentry/sentry-java/pull/2840))
8+
39
## 6.25.1
410

511
### Fixes

sentry-spring-boot-starter-jakarta/src/test/kotlin/io/sentry/spring/boot/jakarta/SentryAutoConfigurationTest.kt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,8 @@ class SentryAutoConfigurationTest {
155155
"sentry.tags.tag1=tag1-value",
156156
"sentry.tags.tag2=tag2-value",
157157
"sentry.ignored-exceptions-for-type=java.lang.RuntimeException,java.lang.IllegalStateException,io.sentry.Sentry",
158-
"sentry.trace-propagation-targets=localhost,^(http|https)://api\\..*\$"
158+
"sentry.trace-propagation-targets=localhost,^(http|https)://api\\..*\$",
159+
"sentry.enabled=false"
159160
).run {
160161
val options = it.getBean(SentryProperties::class.java)
161162
assertThat(options.readTimeoutMillis).isEqualTo(10)
@@ -184,6 +185,7 @@ class SentryAutoConfigurationTest {
184185
assertThat(options.tags).containsEntry("tag1", "tag1-value").containsEntry("tag2", "tag2-value")
185186
assertThat(options.ignoredExceptionsForType).containsOnly(RuntimeException::class.java, IllegalStateException::class.java)
186187
assertThat(options.tracePropagationTargets).containsOnly("localhost", "^(http|https)://api\\..*\$")
188+
assertThat(options.isEnabled).isEqualTo(false)
187189
}
188190
}
189191

sentry-spring-boot-starter/src/test/kotlin/io/sentry/spring/boot/SentryAutoConfigurationTest.kt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,8 @@ class SentryAutoConfigurationTest {
155155
"sentry.tags.tag1=tag1-value",
156156
"sentry.tags.tag2=tag2-value",
157157
"sentry.ignored-exceptions-for-type=java.lang.RuntimeException,java.lang.IllegalStateException,io.sentry.Sentry",
158-
"sentry.trace-propagation-targets=localhost,^(http|https)://api\\..*\$"
158+
"sentry.trace-propagation-targets=localhost,^(http|https)://api\\..*\$",
159+
"sentry.enabled=false"
159160
).run {
160161
val options = it.getBean(SentryProperties::class.java)
161162
assertThat(options.readTimeoutMillis).isEqualTo(10)
@@ -184,6 +185,7 @@ class SentryAutoConfigurationTest {
184185
assertThat(options.tags).containsEntry("tag1", "tag1-value").containsEntry("tag2", "tag2-value")
185186
assertThat(options.ignoredExceptionsForType).containsOnly(RuntimeException::class.java, IllegalStateException::class.java)
186187
assertThat(options.tracePropagationTargets).containsOnly("localhost", "^(http|https)://api\\..*\$")
188+
assertThat(options.isEnabled).isEqualTo(false)
187189
}
188190
}
189191

sentry/api/sentry.api

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -272,12 +272,14 @@ public final class io/sentry/ExternalOptions {
272272
public fun getTracePropagationTargets ()Ljava/util/List;
273273
public fun getTracesSampleRate ()Ljava/lang/Double;
274274
public fun getTracingOrigins ()Ljava/util/List;
275+
public fun isEnabled ()Ljava/lang/Boolean;
275276
public fun setDebug (Ljava/lang/Boolean;)V
276277
public fun setDist (Ljava/lang/String;)V
277278
public fun setDsn (Ljava/lang/String;)V
278279
public fun setEnableDeduplication (Ljava/lang/Boolean;)V
279280
public fun setEnableTracing (Ljava/lang/Boolean;)V
280281
public fun setEnableUncaughtExceptionHandler (Ljava/lang/Boolean;)V
282+
public fun setEnabled (Ljava/lang/Boolean;)V
281283
public fun setEnvironment (Ljava/lang/String;)V
282284
public fun setIdleTimeout (Ljava/lang/Long;)V
283285
public fun setMaxRequestBodySize (Lio/sentry/SentryOptions$RequestSize;)V
@@ -1772,6 +1774,7 @@ public class io/sentry/SentryOptions {
17721774
public fun isEnableUncaughtExceptionHandler ()Z
17731775
public fun isEnableUserInteractionBreadcrumbs ()Z
17741776
public fun isEnableUserInteractionTracing ()Z
1777+
public fun isEnabled ()Z
17751778
public fun isPrintUncaughtStackTrace ()Z
17761779
public fun isProfilingEnabled ()Z
17771780
public fun isSendClientReports ()Z
@@ -1806,6 +1809,7 @@ public class io/sentry/SentryOptions {
18061809
public fun setEnableUncaughtExceptionHandler (Z)V
18071810
public fun setEnableUserInteractionBreadcrumbs (Z)V
18081811
public fun setEnableUserInteractionTracing (Z)V
1812+
public fun setEnabled (Z)V
18091813
public fun setEnvelopeDiskCache (Lio/sentry/cache/IEnvelopeCache;)V
18101814
public fun setEnvelopeReader (Lio/sentry/IEnvelopeReader;)V
18111815
public fun setEnvironment (Ljava/lang/String;)V

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

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ public final class ExternalOptions {
4242
private @Nullable Boolean printUncaughtStackTrace;
4343
private @Nullable Boolean sendClientReports;
4444
private @NotNull Set<String> bundleIds = new CopyOnWriteArraySet<>();
45+
private @Nullable Boolean enabled;
4546

4647
@SuppressWarnings("unchecked")
4748
public static @NotNull ExternalOptions from(
@@ -115,6 +116,8 @@ public final class ExternalOptions {
115116
}
116117
options.setIdleTimeout(propertiesProvider.getLongProperty("idle-timeout"));
117118

119+
options.setEnabled(propertiesProvider.getBooleanProperty("enabled"));
120+
118121
for (final String ignoredExceptionType :
119122
propertiesProvider.getList("ignored-exceptions-for-type")) {
120123
try {
@@ -347,4 +350,12 @@ public void setSendClientReports(final @Nullable Boolean sendClientReports) {
347350
public void addBundleId(final @NotNull String bundleId) {
348351
bundleIds.add(bundleId);
349352
}
353+
354+
public @Nullable Boolean isEnabled() {
355+
return enabled;
356+
}
357+
358+
public void setEnabled(final @Nullable Boolean enabled) {
359+
this.enabled = enabled;
360+
}
350361
}

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -288,8 +288,9 @@ private static boolean initConfigurations(final @NotNull SentryOptions options)
288288

289289
final String dsn = options.getDsn();
290290
if (dsn == null) {
291-
throw new IllegalArgumentException("DSN is required. Use empty string to disable SDK.");
292-
} else if (dsn.isEmpty()) {
291+
throw new IllegalArgumentException(
292+
"DSN is required. Use empty string or set enabled to false in SentryOptions to disable SDK.");
293+
} else if (dsn.isEmpty() || !options.isEnabled()) {
293294
close();
294295
return false;
295296
}

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

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -434,6 +434,9 @@ public class SentryOptions {
434434
private final @NotNull FullyDisplayedReporter fullyDisplayedReporter =
435435
FullyDisplayedReporter.getInstance();
436436

437+
/** Whether Sentry should be enabled */
438+
private boolean enabled = true;
439+
437440
/**
438441
* Adds an event processor
439442
*
@@ -2096,6 +2099,24 @@ public void setTraceOptionsRequests(boolean traceOptionsRequests) {
20962099
this.traceOptionsRequests = traceOptionsRequests;
20972100
}
20982101

2102+
/**
2103+
* Whether Sentry is enabled.
2104+
*
2105+
* @return true if Sentry should be enabled
2106+
*/
2107+
public boolean isEnabled() {
2108+
return enabled;
2109+
}
2110+
2111+
/**
2112+
* Whether Sentry should be enabled.
2113+
*
2114+
* @param enabled true if Sentry should be enabled
2115+
*/
2116+
public void setEnabled(boolean enabled) {
2117+
this.enabled = enabled;
2118+
}
2119+
20992120
/** Returns the current {@link SentryDateProvider} that is used to retrieve the current date. */
21002121
@ApiStatus.Internal
21012122
public @NotNull SentryDateProvider getDateProvider() {
@@ -2333,6 +2354,10 @@ public void merge(final @NotNull ExternalOptions options) {
23332354
for (String bundleId : options.getBundleIds()) {
23342355
addBundleId(bundleId);
23352356
}
2357+
2358+
if (options.isEnabled() != null) {
2359+
setEnabled(options.isEnabled());
2360+
}
23362361
}
23372362

23382363
private @NotNull SdkVersion createSdkVersion() {

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -247,6 +247,13 @@ class ExternalOptionsTest {
247247
}
248248
}
249249

250+
@Test
251+
fun `creates options with enabled set to true`() {
252+
withPropertiesFile("enabled=true") { options ->
253+
assertTrue(options.isEnabled() == true)
254+
}
255+
}
256+
250257
private fun withPropertiesFile(textLines: List<String> = emptyList(), logger: ILogger = mock(), fn: (ExternalOptions) -> Unit) {
251258
// create a sentry.properties file in temporary folder
252259
val temporaryFolder = TemporaryFolder()

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -368,6 +368,7 @@ class SentryOptionsTest {
368368
externalOptions.proguardUuid = "1234"
369369
externalOptions.idleTimeout = 1500L
370370
externalOptions.bundleIds.addAll(listOf("12ea7a02-46ac-44c0-a5bb-6d1fd9586411 ", " faa3ab42-b1bd-4659-af8e-1682324aa744"))
371+
externalOptions.isEnabled = false
371372
val options = SentryOptions()
372373

373374
options.merge(externalOptions)
@@ -392,6 +393,7 @@ class SentryOptionsTest {
392393
assertEquals("1234", options.proguardUuid)
393394
assertEquals(1500L, options.idleTimeout)
394395
assertEquals(setOf("12ea7a02-46ac-44c0-a5bb-6d1fd9586411", "faa3ab42-b1bd-4659-af8e-1682324aa744"), options.bundleIds)
396+
assertFalse(options.isEnabled)
395397
}
396398

397399
@Test
@@ -483,4 +485,9 @@ class SentryOptionsTest {
483485
fun `when options are initialized, FullyDrawnReporter is set`() {
484486
assertEquals(FullyDisplayedReporter.getInstance(), SentryOptions().fullyDisplayedReporter)
485487
}
488+
489+
@Test
490+
fun `when options are initialized, enabled is set to true by default`() {
491+
assertTrue(SentryOptions().isEnabled)
492+
}
486493
}

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

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,23 @@ class SentryTest {
167167
}
168168
}
169169

170+
@Test
171+
fun `initializes Sentry with enabled=false, thus disabling Sentry even if dsn is set`() {
172+
Sentry.init {
173+
it.isEnabled = false
174+
it.dsn = "http://key@localhost/proj"
175+
}
176+
177+
Sentry.setTag("none", "shouldNotExist")
178+
179+
var value: String? = null
180+
Sentry.getCurrentHub().configureScope {
181+
value = it.tags[value]
182+
}
183+
assertTrue(Sentry.getCurrentHub() is NoOpHub)
184+
assertNull(value)
185+
}
186+
170187
@Test
171188
fun `captureUserFeedback gets forwarded to client`() {
172189
Sentry.init { it.dsn = dsn }

0 commit comments

Comments
 (0)