Skip to content

Commit 9c8b5aa

Browse files
lbloderadinauergetsentry-bot
authored
Disable Sentry with SentryOptions (#2840)
* add enabled flag to SentryOptions * add enabled flag to external options, add tests * add changelog * Update CHANGELOG.md Co-authored-by: Alexander Dinauer <[email protected]> * only check for dsn null after checking whether sentry is enabled * format * add test for disabling sentry by option flag with dsn = null * add test to verify that exception is thrown when Senty is initialized with dsn = null and enabled = true * fix changelog after merge * add enabled flag to AndroidManifest.xml * Format code --------- Co-authored-by: Alexander Dinauer <[email protected]> Co-authored-by: Sentry Github Bot <[email protected]>
1 parent 7cdf121 commit 9c8b5aa

File tree

13 files changed

+151
-8
lines changed

13 files changed

+151
-8
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
- Add autoconfigure modules for Spring Boot called `sentry-spring-boot` and `sentry-spring-boot-jakarta` ([#2880](https://github.com/getsentry/sentry-java/pull/2880))
1616
- The autoconfigure modules `sentry-spring-boot` and `sentry-spring-boot-jakarta` have a `compileOnly` dependency on `spring-boot-starter` which is needed for our auto installation in [sentry-android-gradle-plugin](https://github.com/getsentry/sentry-android-gradle-plugin)
1717
- The starter modules `sentry-spring-boot-starter` and `sentry-spring-boot-starter-jakarta` now bring `spring-boot-starter` as a dependency
18+
- You can now disable Sentry by setting the `enabled` option to `false` ([#2840](https://github.com/getsentry/sentry-java/pull/2840))
1819

1920
### Fixes
2021

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

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,8 @@ final class ManifestMetadataReader {
9292

9393
static final String ENABLE_ROOT_CHECK = "io.sentry.enable-root-check";
9494

95+
static final String ENABLE_SENTRY = "io.sentry.enabled";
96+
9597
/** ManifestMetadataReader ctor */
9698
private ManifestMetadataReader() {}
9799

@@ -157,13 +159,21 @@ static void applyMetadata(
157159
options.getAnrTimeoutIntervalMillis()));
158160

159161
final String dsn = readString(metadata, logger, DSN, options.getDsn());
160-
if (dsn == null) {
162+
final boolean enabled = readBool(metadata, logger, ENABLE_SENTRY, options.isEnabled());
163+
164+
if (!enabled || (dsn != null && dsn.isEmpty())) {
165+
options
166+
.getLogger()
167+
.log(
168+
SentryLevel.DEBUG,
169+
"Sentry enabled flag set to false or DSN is empty: disabling sentry-android");
170+
} else if (dsn == null) {
161171
options
162172
.getLogger()
163173
.log(SentryLevel.FATAL, "DSN is required. Use empty string to disable SDK.");
164-
} else if (dsn.isEmpty()) {
165-
options.getLogger().log(SentryLevel.DEBUG, "DSN is empty, disabling sentry-android");
166174
}
175+
176+
options.setEnabled(enabled);
167177
options.setDsn(dsn);
168178

169179
options.setEnableNdk(readBool(metadata, logger, NDK_ENABLE, options.isEnableNdk()));

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

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1268,4 +1268,29 @@ class ManifestMetadataReaderTest {
12681268
// Assert
12691269
assertTrue(fixture.options.isEnableRootCheck)
12701270
}
1271+
1272+
@Test
1273+
fun `applyMetadata reads enabled flag to options`() {
1274+
// Arrange
1275+
val bundle = bundleOf(ManifestMetadataReader.ENABLE_SENTRY to false)
1276+
val context = fixture.getContext(metaData = bundle)
1277+
1278+
// Act
1279+
ManifestMetadataReader.applyMetadata(context, fixture.options, fixture.buildInfoProvider)
1280+
1281+
// Assert
1282+
assertFalse(fixture.options.isEnabled)
1283+
}
1284+
1285+
@Test
1286+
fun `applyMetadata reads enabled flag to options and keeps default if not found`() {
1287+
// Arrange
1288+
val context = fixture.getContext()
1289+
1290+
// Act
1291+
ManifestMetadataReader.applyMetadata(context, fixture.options, fixture.buildInfoProvider)
1292+
1293+
// Assert
1294+
assertTrue(fixture.options.isEnabled)
1295+
}
12711296
}

sentry-samples/sentry-samples-android/src/main/AndroidManifest.xml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,5 +148,8 @@
148148
<!-- how to enable the send default pii-->
149149
<meta-data android:name="io.sentry.send-default-pii" android:value="true" />
150150

151+
<!-- how to disable sentry -->
152+
<!-- <meta-data android:name="io.sentry.enabled" android:value="false" /> -->
153+
151154
</application>
152155
</manifest>

sentry-spring-boot-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/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
@@ -284,12 +284,14 @@ public final class io/sentry/ExternalOptions {
284284
public fun getTracePropagationTargets ()Ljava/util/List;
285285
public fun getTracesSampleRate ()Ljava/lang/Double;
286286
public fun getTracingOrigins ()Ljava/util/List;
287+
public fun isEnabled ()Ljava/lang/Boolean;
287288
public fun setDebug (Ljava/lang/Boolean;)V
288289
public fun setDist (Ljava/lang/String;)V
289290
public fun setDsn (Ljava/lang/String;)V
290291
public fun setEnableDeduplication (Ljava/lang/Boolean;)V
291292
public fun setEnableTracing (Ljava/lang/Boolean;)V
292293
public fun setEnableUncaughtExceptionHandler (Ljava/lang/Boolean;)V
294+
public fun setEnabled (Ljava/lang/Boolean;)V
293295
public fun setEnvironment (Ljava/lang/String;)V
294296
public fun setIdleTimeout (Ljava/lang/Long;)V
295297
public fun setMaxRequestBodySize (Lio/sentry/SentryOptions$RequestSize;)V
@@ -1829,6 +1831,7 @@ public class io/sentry/SentryOptions {
18291831
public fun isEnableUncaughtExceptionHandler ()Z
18301832
public fun isEnableUserInteractionBreadcrumbs ()Z
18311833
public fun isEnableUserInteractionTracing ()Z
1834+
public fun isEnabled ()Z
18321835
public fun isPrintUncaughtStackTrace ()Z
18331836
public fun isProfilingEnabled ()Z
18341837
public fun isSendClientReports ()Z
@@ -1863,6 +1866,7 @@ public class io/sentry/SentryOptions {
18631866
public fun setEnableUncaughtExceptionHandler (Z)V
18641867
public fun setEnableUserInteractionBreadcrumbs (Z)V
18651868
public fun setEnableUserInteractionTracing (Z)V
1869+
public fun setEnabled (Z)V
18661870
public fun setEnvelopeDiskCache (Lio/sentry/cache/IEnvelopeCache;)V
18671871
public fun setEnvelopeReader (Lio/sentry/IEnvelopeReader;)V
18681872
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: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -287,11 +287,13 @@ private static boolean initConfigurations(final @NotNull SentryOptions options)
287287
}
288288

289289
final String dsn = options.getDsn();
290-
if (dsn == null) {
291-
throw new IllegalArgumentException("DSN is required. Use empty string to disable SDK.");
292-
} else if (dsn.isEmpty()) {
290+
291+
if (!options.isEnabled() || (dsn != null && dsn.isEmpty())) {
293292
close();
294293
return false;
294+
} else if (dsn == null) {
295+
throw new IllegalArgumentException(
296+
"DSN is required. Use empty string or set enabled to false in SentryOptions to disable SDK.");
295297
}
296298

297299
@SuppressWarnings("unused")

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() {

0 commit comments

Comments
 (0)