Skip to content

Commit

Permalink
Add globalHubMode option
Browse files Browse the repository at this point in the history
  • Loading branch information
adinauer committed Oct 18, 2024
1 parent 6b638b0 commit efb1564
Show file tree
Hide file tree
Showing 9 changed files with 106 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,7 @@ class SentryAutoConfigurationTest {
"sentry.enable-spotlight=true",
"sentry.spotlight-connection-url=http://local.sentry.io:1234",
"sentry.force-init=true",
"sentry.global-hub-mode=true",
"sentry.cron.default-checkin-margin=10",
"sentry.cron.default-max-runtime=30",
"sentry.cron.default-timezone=America/New_York",
Expand Down Expand Up @@ -211,6 +212,7 @@ class SentryAutoConfigurationTest {
assertThat(options.ignoredCheckIns).containsOnly("slug1", "slugB")
assertThat(options.isEnableBackpressureHandling).isEqualTo(false)
assertThat(options.isForceInit).isEqualTo(true)
assertThat(options.isGlobalHubMode).isEqualTo(true)
assertThat(options.isEnableSpotlight).isEqualTo(true)
assertThat(options.spotlightConnectionUrl).isEqualTo("http://local.sentry.io:1234")
assertThat(options.cron).isNotNull
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,7 @@ class SentryAutoConfigurationTest {
"sentry.enable-spotlight=true",
"sentry.spotlight-connection-url=http://local.sentry.io:1234",
"sentry.force-init=true",
"sentry.global-hub-mode=true",
"sentry.cron.default-checkin-margin=10",
"sentry.cron.default-max-runtime=30",
"sentry.cron.default-timezone=America/New_York",
Expand Down Expand Up @@ -210,6 +211,7 @@ class SentryAutoConfigurationTest {
assertThat(options.ignoredCheckIns).containsOnly("slug1", "slugB")
assertThat(options.isEnableBackpressureHandling).isEqualTo(false)
assertThat(options.isForceInit).isEqualTo(true)
assertThat(options.isGlobalHubMode).isEqualTo(true)
assertThat(options.isEnableSpotlight).isEqualTo(true)
assertThat(options.spotlightConnectionUrl).isEqualTo("http://local.sentry.io:1234")
assertThat(options.cron).isNotNull
Expand Down
4 changes: 4 additions & 0 deletions sentry/api/sentry.api
Original file line number Diff line number Diff line change
Expand Up @@ -473,6 +473,7 @@ public final class io/sentry/ExternalOptions {
public fun isEnableSpotlight ()Ljava/lang/Boolean;
public fun isEnabled ()Ljava/lang/Boolean;
public fun isForceInit ()Ljava/lang/Boolean;
public fun isGlobalHubMode ()Ljava/lang/Boolean;
public fun isSendDefaultPii ()Ljava/lang/Boolean;
public fun isSendModules ()Ljava/lang/Boolean;
public fun setCron (Lio/sentry/SentryOptions$Cron;)V
Expand All @@ -487,6 +488,7 @@ public final class io/sentry/ExternalOptions {
public fun setEnabled (Ljava/lang/Boolean;)V
public fun setEnvironment (Ljava/lang/String;)V
public fun setForceInit (Ljava/lang/Boolean;)V
public fun setGlobalHubMode (Ljava/lang/Boolean;)V
public fun setIdleTimeout (Ljava/lang/Long;)V
public fun setIgnoredCheckIns (Ljava/util/List;)V
public fun setMaxRequestBodySize (Lio/sentry/SentryOptions$RequestSize;)V
Expand Down Expand Up @@ -2891,6 +2893,7 @@ public class io/sentry/SentryOptions {
public fun isEnableUserInteractionTracing ()Z
public fun isEnabled ()Z
public fun isForceInit ()Z
public fun isGlobalHubMode ()Ljava/lang/Boolean;
public fun isPrintUncaughtStackTrace ()Z
public fun isProfilingEnabled ()Z
public fun isSendClientReports ()Z
Expand Down Expand Up @@ -2943,6 +2946,7 @@ public class io/sentry/SentryOptions {
public fun setForceInit (Z)V
public fun setFullyDisplayedReporter (Lio/sentry/FullyDisplayedReporter;)V
public fun setGestureTargetLocators (Ljava/util/List;)V
public fun setGlobalHubMode (Ljava/lang/Boolean;)V
public fun setIdleTimeout (Ljava/lang/Long;)V
public fun setIgnoredCheckIns (Ljava/util/List;)V
public fun setIgnoredSpanOrigins (Ljava/util/List;)V
Expand Down
12 changes: 12 additions & 0 deletions sentry/src/main/java/io/sentry/ExternalOptions.java
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ public final class ExternalOptions {
private @Nullable Boolean sendModules;
private @Nullable Boolean sendDefaultPii;
private @Nullable Boolean enableBackpressureHandling;
private @Nullable Boolean globalHubMode;
private @Nullable Boolean forceInit;

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

options.setGlobalHubMode(propertiesProvider.getBooleanProperty("global-hub-mode"));

for (final String ignoredExceptionType :
propertiesProvider.getList("ignored-exceptions-for-type")) {
try {
Expand Down Expand Up @@ -437,6 +440,15 @@ public void setEnableBackpressureHandling(final @Nullable Boolean enableBackpres
return enableBackpressureHandling;
}

public void setGlobalHubMode(final @Nullable Boolean globalHubMode) {
this.globalHubMode = globalHubMode;
}

@ApiStatus.Experimental
public @Nullable Boolean isGlobalHubMode() {
return globalHubMode;
}

public void setForceInit(final @Nullable Boolean forceInit) {
this.forceInit = forceInit;
}
Expand Down
7 changes: 4 additions & 3 deletions sentry/src/main/java/io/sentry/Sentry.java
Original file line number Diff line number Diff line change
Expand Up @@ -267,7 +267,6 @@ public static void init(final @NotNull SentryOptions options) {
@SuppressWarnings("deprecation")
private static void init(final @NotNull SentryOptions options, final boolean globalHubMode) {
try (final @NotNull ISentryLifecycleToken ignored = lock.acquire()) {

if (!options.getClass().getName().equals("io.sentry.android.core.SentryAndroidOptions")
&& Platform.isAndroid()) {
throw new IllegalArgumentException(
Expand All @@ -279,10 +278,12 @@ private static void init(final @NotNull SentryOptions options, final boolean glo
return;
}

final boolean globalHubModeToUse =
options.isGlobalHubMode() != null ? options.isGlobalHubMode() : globalHubMode;
options
.getLogger()
.log(SentryLevel.INFO, "GlobalHubMode: '%s'", String.valueOf(globalHubMode));
Sentry.globalHubMode = globalHubMode;
.log(SentryLevel.INFO, "GlobalHubMode: '%s'", String.valueOf(globalHubModeToUse));
Sentry.globalHubMode = globalHubModeToUse;
final boolean shouldInit =
InitUtil.shouldInit(globalScope.getOptions(), options, isEnabled());
if (shouldInit) {
Expand Down
27 changes: 27 additions & 0 deletions sentry/src/main/java/io/sentry/SentryOptions.java
Original file line number Diff line number Diff line change
Expand Up @@ -496,6 +496,9 @@ public class SentryOptions {

private boolean forceInit = false;

// TODO replace hub in name
private @Nullable Boolean globalHubMode = null;

protected final @NotNull AutoClosableReentrantLock lock = new AutoClosableReentrantLock();

/**
Expand Down Expand Up @@ -2370,6 +2373,26 @@ public boolean isForceInit() {
return forceInit;
}

/**
* If set to true, automatic scope forking will be disabled. If set to false, scopes will be
* forked automatically, e.g. when scopes are accessed on a thread for the first time, pushScope
* is invoked, in some cases when we explicitly want to fork the root scopes, etc.
*
* <p>If this is set to something other than `null`, it will take precedence over what is passed
* to Sentry.init.
*
* <p>Enabling this is intended for mobile and desktop apps, not backends.
*
* @param globalHubMode true = automatic scope forking is disabled
*/
public void setGlobalHubMode(final @Nullable Boolean globalHubMode) {
this.globalHubMode = globalHubMode;
}

public @Nullable Boolean isGlobalHubMode() {
return globalHubMode;
}

/** The BeforeSend callback */
public interface BeforeSendCallback {

Expand Down Expand Up @@ -2634,6 +2657,10 @@ public void merge(final @NotNull ExternalOptions options) {
setSpotlightConnectionUrl(options.getSpotlightConnectionUrl());
}

if (options.isGlobalHubMode() != null) {
setGlobalHubMode(options.isGlobalHubMode());
}

if (options.getCron() != null) {
if (getCron() == null) {
setCron(options.getCron());
Expand Down
14 changes: 14 additions & 0 deletions sentry/src/test/java/io/sentry/ExternalOptionsTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -307,6 +307,20 @@ class ExternalOptionsTest {
}
}

@Test
fun `creates options with globalHubMode set to true`() {
withPropertiesFile("global-hub-mode=true") { options ->
assertTrue(options.isGlobalHubMode == true)
}
}

@Test
fun `creates options with globalHubMode set to false`() {
withPropertiesFile("global-hub-mode=false") { options ->
assertTrue(options.isGlobalHubMode == false)
}
}

private fun withPropertiesFile(textLines: List<String> = emptyList(), logger: ILogger = mock(), fn: (ExternalOptions) -> Unit) {
// create a sentry.properties file in temporary folder
val temporaryFolder = TemporaryFolder()
Expand Down
7 changes: 7 additions & 0 deletions sentry/src/test/java/io/sentry/SentryOptionsTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -338,6 +338,7 @@ class SentryOptionsTest {
}
externalOptions.isEnableSpotlight = true
externalOptions.spotlightConnectionUrl = "http://local.sentry.io:1234"
externalOptions.isGlobalHubMode = true

val options = SentryOptions()

Expand Down Expand Up @@ -379,6 +380,7 @@ class SentryOptionsTest {
assertEquals(RequestSize.MEDIUM, options.maxRequestBodySize)
assertTrue(options.isEnableSpotlight)
assertEquals("http://local.sentry.io:1234", options.spotlightConnectionUrl)
assertTrue(options.isGlobalHubMode!!)
}

@Test
Expand Down Expand Up @@ -547,6 +549,11 @@ class SentryOptionsTest {
assertFalse(SentryOptions().isEnableAppStartProfiling)
}

@Test
fun `when options are initialized, isGlobalHubMode is set to null by default`() {
assertNull(SentryOptions().isGlobalHubMode)
}

@Test
fun `when setEnableAppStartProfiling is called, overrides default`() {
val options = SentryOptions()
Expand Down
34 changes: 34 additions & 0 deletions sentry/src/test/java/io/sentry/SentryTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,9 @@ import kotlin.test.assertFalse
import kotlin.test.assertIs
import kotlin.test.assertNotEquals
import kotlin.test.assertNotNull
import kotlin.test.assertNotSame
import kotlin.test.assertNull
import kotlin.test.assertSame
import kotlin.test.assertTrue

class SentryTest {
Expand Down Expand Up @@ -1243,6 +1245,38 @@ class SentryTest {
}
}

@Test
fun `if globalHubMode on options is not set, uses false from init param`() {
Sentry.init({ o -> o.dsn = dsn }, false)
val s1 = Sentry.forkedRootScopes("s1")
val s2 = Sentry.forkedRootScopes("s2")
assertNotSame(s1, s2)
}

@Test
fun `if globalHubMode on options is not set, uses true from init param`() {
Sentry.init({ o -> o.dsn = dsn }, true)
val s1 = Sentry.forkedRootScopes("s1")
val s2 = Sentry.forkedRootScopes("s2")
assertSame(s1, s2)
}

@Test
fun `if globalHubMode on options is set, ignores false from init param`() {
Sentry.init({ o -> o.dsn = dsn; o.isGlobalHubMode = true }, false)
val s1 = Sentry.forkedRootScopes("s1")
val s2 = Sentry.forkedRootScopes("s2")
assertSame(s1, s2)
}

@Test
fun `if globalHubMode on options is set, ignores true from init param`() {
Sentry.init({ o -> o.dsn = dsn; o.isGlobalHubMode = false }, true)
val s1 = Sentry.forkedRootScopes("s1")
val s2 = Sentry.forkedRootScopes("s2")
assertNotSame(s1, s2)
}

private class InMemoryOptionsObserver : IOptionsObserver {
var release: String? = null
private set
Expand Down

0 comments on commit efb1564

Please sign in to comment.