Skip to content

Commit 5b8c172

Browse files
authored
Add globalHubMode to options (#3805)
* Add globalHubMode option * CR changes * changelog
1 parent 6b638b0 commit 5b8c172

File tree

10 files changed

+116
-3
lines changed

10 files changed

+116
-3
lines changed

CHANGELOG.md

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

3+
## Unreleased
4+
5+
### Features
6+
7+
- Add `globalHubMode` to options ([#3805](https://github.com/getsentry/sentry-java/pull/3805))
8+
- `globalHubMode` used to only be a param on `Sentry.init`. To make it easier to be used in e.g. Desktop environments, we now additionally added it as an option on SentryOptions that can also be set via `sentry.properties`.
9+
- If both the param on `Sentry.init` and the option are set, the option will win. By default the option is set to `null` meaning whatever is passed to `Sentry.init` takes effect.
10+
311
## 8.0.0-beta.1
412

513
### Breaking Changes

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,7 @@ class SentryAutoConfigurationTest {
174174
"sentry.enable-spotlight=true",
175175
"sentry.spotlight-connection-url=http://local.sentry.io:1234",
176176
"sentry.force-init=true",
177+
"sentry.global-hub-mode=true",
177178
"sentry.cron.default-checkin-margin=10",
178179
"sentry.cron.default-max-runtime=30",
179180
"sentry.cron.default-timezone=America/New_York",
@@ -211,6 +212,7 @@ class SentryAutoConfigurationTest {
211212
assertThat(options.ignoredCheckIns).containsOnly("slug1", "slugB")
212213
assertThat(options.isEnableBackpressureHandling).isEqualTo(false)
213214
assertThat(options.isForceInit).isEqualTo(true)
215+
assertThat(options.isGlobalHubMode).isEqualTo(true)
214216
assertThat(options.isEnableSpotlight).isEqualTo(true)
215217
assertThat(options.spotlightConnectionUrl).isEqualTo("http://local.sentry.io:1234")
216218
assertThat(options.cron).isNotNull

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,7 @@ class SentryAutoConfigurationTest {
173173
"sentry.enable-spotlight=true",
174174
"sentry.spotlight-connection-url=http://local.sentry.io:1234",
175175
"sentry.force-init=true",
176+
"sentry.global-hub-mode=true",
176177
"sentry.cron.default-checkin-margin=10",
177178
"sentry.cron.default-max-runtime=30",
178179
"sentry.cron.default-timezone=America/New_York",
@@ -210,6 +211,7 @@ class SentryAutoConfigurationTest {
210211
assertThat(options.ignoredCheckIns).containsOnly("slug1", "slugB")
211212
assertThat(options.isEnableBackpressureHandling).isEqualTo(false)
212213
assertThat(options.isForceInit).isEqualTo(true)
214+
assertThat(options.isGlobalHubMode).isEqualTo(true)
213215
assertThat(options.isEnableSpotlight).isEqualTo(true)
214216
assertThat(options.spotlightConnectionUrl).isEqualTo("http://local.sentry.io:1234")
215217
assertThat(options.cron).isNotNull

sentry/api/sentry.api

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -473,6 +473,7 @@ public final class io/sentry/ExternalOptions {
473473
public fun isEnableSpotlight ()Ljava/lang/Boolean;
474474
public fun isEnabled ()Ljava/lang/Boolean;
475475
public fun isForceInit ()Ljava/lang/Boolean;
476+
public fun isGlobalHubMode ()Ljava/lang/Boolean;
476477
public fun isSendDefaultPii ()Ljava/lang/Boolean;
477478
public fun isSendModules ()Ljava/lang/Boolean;
478479
public fun setCron (Lio/sentry/SentryOptions$Cron;)V
@@ -487,6 +488,7 @@ public final class io/sentry/ExternalOptions {
487488
public fun setEnabled (Ljava/lang/Boolean;)V
488489
public fun setEnvironment (Ljava/lang/String;)V
489490
public fun setForceInit (Ljava/lang/Boolean;)V
491+
public fun setGlobalHubMode (Ljava/lang/Boolean;)V
490492
public fun setIdleTimeout (Ljava/lang/Long;)V
491493
public fun setIgnoredCheckIns (Ljava/util/List;)V
492494
public fun setMaxRequestBodySize (Lio/sentry/SentryOptions$RequestSize;)V
@@ -2891,6 +2893,7 @@ public class io/sentry/SentryOptions {
28912893
public fun isEnableUserInteractionTracing ()Z
28922894
public fun isEnabled ()Z
28932895
public fun isForceInit ()Z
2896+
public fun isGlobalHubMode ()Ljava/lang/Boolean;
28942897
public fun isPrintUncaughtStackTrace ()Z
28952898
public fun isProfilingEnabled ()Z
28962899
public fun isSendClientReports ()Z
@@ -2943,6 +2946,7 @@ public class io/sentry/SentryOptions {
29432946
public fun setForceInit (Z)V
29442947
public fun setFullyDisplayedReporter (Lio/sentry/FullyDisplayedReporter;)V
29452948
public fun setGestureTargetLocators (Ljava/util/List;)V
2949+
public fun setGlobalHubMode (Ljava/lang/Boolean;)V
29462950
public fun setIdleTimeout (Ljava/lang/Long;)V
29472951
public fun setIgnoredCheckIns (Ljava/util/List;)V
29482952
public fun setIgnoredSpanOrigins (Ljava/util/List;)V

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

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ public final class ExternalOptions {
5252
private @Nullable Boolean sendModules;
5353
private @Nullable Boolean sendDefaultPii;
5454
private @Nullable Boolean enableBackpressureHandling;
55+
private @Nullable Boolean globalHubMode;
5556
private @Nullable Boolean forceInit;
5657

5758
private @Nullable SentryOptions.Cron cron;
@@ -141,6 +142,8 @@ public final class ExternalOptions {
141142
options.setEnableBackpressureHandling(
142143
propertiesProvider.getBooleanProperty("enable-backpressure-handling"));
143144

145+
options.setGlobalHubMode(propertiesProvider.getBooleanProperty("global-hub-mode"));
146+
144147
for (final String ignoredExceptionType :
145148
propertiesProvider.getList("ignored-exceptions-for-type")) {
146149
try {
@@ -437,6 +440,15 @@ public void setEnableBackpressureHandling(final @Nullable Boolean enableBackpres
437440
return enableBackpressureHandling;
438441
}
439442

443+
public void setGlobalHubMode(final @Nullable Boolean globalHubMode) {
444+
this.globalHubMode = globalHubMode;
445+
}
446+
447+
@ApiStatus.Experimental
448+
public @Nullable Boolean isGlobalHubMode() {
449+
return globalHubMode;
450+
}
451+
440452
public void setForceInit(final @Nullable Boolean forceInit) {
441453
this.forceInit = forceInit;
442454
}

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

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -267,7 +267,6 @@ public static void init(final @NotNull SentryOptions options) {
267267
@SuppressWarnings("deprecation")
268268
private static void init(final @NotNull SentryOptions options, final boolean globalHubMode) {
269269
try (final @NotNull ISentryLifecycleToken ignored = lock.acquire()) {
270-
271270
if (!options.getClass().getName().equals("io.sentry.android.core.SentryAndroidOptions")
272271
&& Platform.isAndroid()) {
273272
throw new IllegalArgumentException(
@@ -279,10 +278,13 @@ private static void init(final @NotNull SentryOptions options, final boolean glo
279278
return;
280279
}
281280

281+
final @Nullable Boolean globalHubModeFromOptions = options.isGlobalHubMode();
282+
final boolean globalHubModeToUse =
283+
globalHubModeFromOptions != null ? globalHubModeFromOptions : globalHubMode;
282284
options
283285
.getLogger()
284-
.log(SentryLevel.INFO, "GlobalHubMode: '%s'", String.valueOf(globalHubMode));
285-
Sentry.globalHubMode = globalHubMode;
286+
.log(SentryLevel.INFO, "GlobalHubMode: '%s'", String.valueOf(globalHubModeToUse));
287+
Sentry.globalHubMode = globalHubModeToUse;
286288
final boolean shouldInit =
287289
InitUtil.shouldInit(globalScope.getOptions(), options, isEnabled());
288290
if (shouldInit) {

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

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -496,6 +496,9 @@ public class SentryOptions {
496496

497497
private boolean forceInit = false;
498498

499+
// TODO replace hub in name
500+
private @Nullable Boolean globalHubMode = null;
501+
499502
protected final @NotNull AutoClosableReentrantLock lock = new AutoClosableReentrantLock();
500503

501504
/**
@@ -2370,6 +2373,27 @@ public boolean isForceInit() {
23702373
return forceInit;
23712374
}
23722375

2376+
/**
2377+
* If set to true, automatic scope forking will be disabled. If set to false, scopes will be
2378+
* forked automatically, e.g. when scopes are accessed on a thread for the first time, pushScope
2379+
* is invoked, in some cases when we explicitly want to fork the root scopes, etc.
2380+
*
2381+
* <p>If this is set to something other than `null`, it will take precedence over what is passed
2382+
* to Sentry.init.
2383+
*
2384+
* <p>Enabling this is intended for mobile and desktop apps, not backends. For Android the default
2385+
* value passed to Sentry.init is true (globalHubMode enabled), for backends it defaults to false.
2386+
*
2387+
* @param globalHubMode true = automatic scope forking is disabled
2388+
*/
2389+
public void setGlobalHubMode(final @Nullable Boolean globalHubMode) {
2390+
this.globalHubMode = globalHubMode;
2391+
}
2392+
2393+
public @Nullable Boolean isGlobalHubMode() {
2394+
return globalHubMode;
2395+
}
2396+
23732397
/** The BeforeSend callback */
23742398
public interface BeforeSendCallback {
23752399

@@ -2634,6 +2658,10 @@ public void merge(final @NotNull ExternalOptions options) {
26342658
setSpotlightConnectionUrl(options.getSpotlightConnectionUrl());
26352659
}
26362660

2661+
if (options.isGlobalHubMode() != null) {
2662+
setGlobalHubMode(options.isGlobalHubMode());
2663+
}
2664+
26372665
if (options.getCron() != null) {
26382666
if (getCron() == null) {
26392667
setCron(options.getCron());

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

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -307,6 +307,20 @@ class ExternalOptionsTest {
307307
}
308308
}
309309

310+
@Test
311+
fun `creates options with globalHubMode set to true`() {
312+
withPropertiesFile("global-hub-mode=true") { options ->
313+
assertTrue(options.isGlobalHubMode == true)
314+
}
315+
}
316+
317+
@Test
318+
fun `creates options with globalHubMode set to false`() {
319+
withPropertiesFile("global-hub-mode=false") { options ->
320+
assertTrue(options.isGlobalHubMode == false)
321+
}
322+
}
323+
310324
private fun withPropertiesFile(textLines: List<String> = emptyList(), logger: ILogger = mock(), fn: (ExternalOptions) -> Unit) {
311325
// create a sentry.properties file in temporary folder
312326
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
@@ -338,6 +338,7 @@ class SentryOptionsTest {
338338
}
339339
externalOptions.isEnableSpotlight = true
340340
externalOptions.spotlightConnectionUrl = "http://local.sentry.io:1234"
341+
externalOptions.isGlobalHubMode = true
341342

342343
val options = SentryOptions()
343344

@@ -379,6 +380,7 @@ class SentryOptionsTest {
379380
assertEquals(RequestSize.MEDIUM, options.maxRequestBodySize)
380381
assertTrue(options.isEnableSpotlight)
381382
assertEquals("http://local.sentry.io:1234", options.spotlightConnectionUrl)
383+
assertTrue(options.isGlobalHubMode!!)
382384
}
383385

384386
@Test
@@ -547,6 +549,11 @@ class SentryOptionsTest {
547549
assertFalse(SentryOptions().isEnableAppStartProfiling)
548550
}
549551

552+
@Test
553+
fun `when options are initialized, isGlobalHubMode is set to null by default`() {
554+
assertNull(SentryOptions().isGlobalHubMode)
555+
}
556+
550557
@Test
551558
fun `when setEnableAppStartProfiling is called, overrides default`() {
552559
val options = SentryOptions()

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

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,9 @@ import kotlin.test.assertFalse
4949
import kotlin.test.assertIs
5050
import kotlin.test.assertNotEquals
5151
import kotlin.test.assertNotNull
52+
import kotlin.test.assertNotSame
5253
import kotlin.test.assertNull
54+
import kotlin.test.assertSame
5355
import kotlin.test.assertTrue
5456

5557
class SentryTest {
@@ -1243,6 +1245,38 @@ class SentryTest {
12431245
}
12441246
}
12451247

1248+
@Test
1249+
fun `if globalHubMode on options is not set, uses false from init param`() {
1250+
Sentry.init({ o -> o.dsn = dsn }, false)
1251+
val s1 = Sentry.forkedRootScopes("s1")
1252+
val s2 = Sentry.forkedRootScopes("s2")
1253+
assertNotSame(s1, s2)
1254+
}
1255+
1256+
@Test
1257+
fun `if globalHubMode on options is not set, uses true from init param`() {
1258+
Sentry.init({ o -> o.dsn = dsn }, true)
1259+
val s1 = Sentry.forkedRootScopes("s1")
1260+
val s2 = Sentry.forkedRootScopes("s2")
1261+
assertSame(s1, s2)
1262+
}
1263+
1264+
@Test
1265+
fun `if globalHubMode on options is set, ignores false from init param`() {
1266+
Sentry.init({ o -> o.dsn = dsn; o.isGlobalHubMode = true }, false)
1267+
val s1 = Sentry.forkedRootScopes("s1")
1268+
val s2 = Sentry.forkedRootScopes("s2")
1269+
assertSame(s1, s2)
1270+
}
1271+
1272+
@Test
1273+
fun `if globalHubMode on options is set, ignores true from init param`() {
1274+
Sentry.init({ o -> o.dsn = dsn; o.isGlobalHubMode = false }, true)
1275+
val s1 = Sentry.forkedRootScopes("s1")
1276+
val s2 = Sentry.forkedRootScopes("s2")
1277+
assertNotSame(s1, s2)
1278+
}
1279+
12461280
private class InMemoryOptionsObserver : IOptionsObserver {
12471281
var release: String? = null
12481282
private set

0 commit comments

Comments
 (0)