Skip to content

Commit 1169294

Browse files
committed
Add settings for controlling init priority
1 parent 1787942 commit 1169294

File tree

15 files changed

+252
-37
lines changed

15 files changed

+252
-37
lines changed

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
@@ -172,6 +172,7 @@ class SentryAutoConfigurationTest {
172172
"sentry.send-modules=false",
173173
"sentry.ignored-checkins=slug1,slugB",
174174
"sentry.enable-backpressure-handling=false",
175+
"sentry.force-init=true",
175176
"sentry.cron.default-checkin-margin=10",
176177
"sentry.cron.default-max-runtime=30",
177178
"sentry.cron.default-timezone=America/New_York",
@@ -209,6 +210,7 @@ class SentryAutoConfigurationTest {
209210
assertThat(options.isSendModules).isEqualTo(false)
210211
assertThat(options.ignoredCheckIns).containsOnly("slug1", "slugB")
211212
assertThat(options.isEnableBackpressureHandling).isEqualTo(false)
213+
assertThat(options.isForceInit).isEqualTo(true)
212214
assertThat(options.cron).isNotNull
213215
assertThat(options.cron!!.defaultCheckinMargin).isEqualTo(10L)
214216
assertThat(options.cron!!.defaultMaxRuntime).isEqualTo(30L)

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
@@ -171,6 +171,7 @@ class SentryAutoConfigurationTest {
171171
"sentry.send-modules=false",
172172
"sentry.ignored-checkins=slug1,slugB",
173173
"sentry.enable-backpressure-handling=false",
174+
"sentry.force-init=true",
174175
"sentry.cron.default-checkin-margin=10",
175176
"sentry.cron.default-max-runtime=30",
176177
"sentry.cron.default-timezone=America/New_York",
@@ -208,6 +209,7 @@ class SentryAutoConfigurationTest {
208209
assertThat(options.isSendModules).isEqualTo(false)
209210
assertThat(options.ignoredCheckIns).containsOnly("slug1", "slugB")
210211
assertThat(options.isEnableBackpressureHandling).isEqualTo(false)
212+
assertThat(options.isForceInit).isEqualTo(true)
211213
assertThat(options.cron).isNotNull
212214
assertThat(options.cron!!.defaultCheckinMargin).isEqualTo(10L)
213215
assertThat(options.cron!!.defaultMaxRuntime).isEqualTo(30L)

sentry/api/sentry.api

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -275,7 +275,7 @@ public final class io/sentry/CombinedScopeView : io/sentry/IScope {
275275
public fun removeContexts (Ljava/lang/String;)V
276276
public fun removeExtra (Ljava/lang/String;)V
277277
public fun removeTag (Ljava/lang/String;)V
278-
public fun replaceOptions (Lio/sentry/SentryOptions;)Z
278+
public fun replaceOptions (Lio/sentry/SentryOptions;)V
279279
public fun setActiveSpan (Lio/sentry/ISpan;)V
280280
public fun setContexts (Ljava/lang/String;Ljava/lang/Boolean;)V
281281
public fun setContexts (Ljava/lang/String;Ljava/lang/Character;)V
@@ -459,6 +459,7 @@ public final class io/sentry/ExternalOptions {
459459
public fun isEnableBackpressureHandling ()Ljava/lang/Boolean;
460460
public fun isEnablePrettySerializationOutput ()Ljava/lang/Boolean;
461461
public fun isEnabled ()Ljava/lang/Boolean;
462+
public fun isForceInit ()Ljava/lang/Boolean;
462463
public fun isSendDefaultPii ()Ljava/lang/Boolean;
463464
public fun isSendModules ()Ljava/lang/Boolean;
464465
public fun setCron (Lio/sentry/SentryOptions$Cron;)V
@@ -472,6 +473,7 @@ public final class io/sentry/ExternalOptions {
472473
public fun setEnableUncaughtExceptionHandler (Ljava/lang/Boolean;)V
473474
public fun setEnabled (Ljava/lang/Boolean;)V
474475
public fun setEnvironment (Ljava/lang/String;)V
476+
public fun setForceInit (Ljava/lang/Boolean;)V
475477
public fun setIdleTimeout (Ljava/lang/Long;)V
476478
public fun setIgnoredCheckIns (Ljava/util/List;)V
477479
public fun setMaxRequestBodySize (Lio/sentry/SentryOptions$RequestSize;)V
@@ -769,7 +771,7 @@ public abstract interface class io/sentry/IScope {
769771
public abstract fun removeContexts (Ljava/lang/String;)V
770772
public abstract fun removeExtra (Ljava/lang/String;)V
771773
public abstract fun removeTag (Ljava/lang/String;)V
772-
public abstract fun replaceOptions (Lio/sentry/SentryOptions;)Z
774+
public abstract fun replaceOptions (Lio/sentry/SentryOptions;)V
773775
public abstract fun setActiveSpan (Lio/sentry/ISpan;)V
774776
public abstract fun setContexts (Ljava/lang/String;Ljava/lang/Boolean;)V
775777
public abstract fun setContexts (Ljava/lang/String;Ljava/lang/Character;)V
@@ -1028,6 +1030,16 @@ public abstract interface class io/sentry/ITransportFactory {
10281030
public abstract fun create (Lio/sentry/SentryOptions;Lio/sentry/RequestDetails;)Lio/sentry/transport/ITransport;
10291031
}
10301032

1033+
public final class io/sentry/InitPriority : java/lang/Enum {
1034+
public static final field HIGH Lio/sentry/InitPriority;
1035+
public static final field HIGHEST Lio/sentry/InitPriority;
1036+
public static final field LOW Lio/sentry/InitPriority;
1037+
public static final field LOWEST Lio/sentry/InitPriority;
1038+
public static final field MEDIUM Lio/sentry/InitPriority;
1039+
public static fun valueOf (Ljava/lang/String;)Lio/sentry/InitPriority;
1040+
public static fun values ()[Lio/sentry/InitPriority;
1041+
}
1042+
10311043
public final class io/sentry/Instrumenter : java/lang/Enum {
10321044
public static final field OTEL Lio/sentry/Instrumenter;
10331045
public static final field SENTRY Lio/sentry/Instrumenter;
@@ -1449,7 +1461,7 @@ public final class io/sentry/NoOpScope : io/sentry/IScope {
14491461
public fun removeContexts (Ljava/lang/String;)V
14501462
public fun removeExtra (Ljava/lang/String;)V
14511463
public fun removeTag (Ljava/lang/String;)V
1452-
public fun replaceOptions (Lio/sentry/SentryOptions;)Z
1464+
public fun replaceOptions (Lio/sentry/SentryOptions;)V
14531465
public fun setActiveSpan (Lio/sentry/ISpan;)V
14541466
public fun setContexts (Ljava/lang/String;Ljava/lang/Boolean;)V
14551467
public fun setContexts (Ljava/lang/String;Ljava/lang/Character;)V
@@ -1923,7 +1935,7 @@ public final class io/sentry/Scope : io/sentry/IScope {
19231935
public fun removeContexts (Ljava/lang/String;)V
19241936
public fun removeExtra (Ljava/lang/String;)V
19251937
public fun removeTag (Ljava/lang/String;)V
1926-
public fun replaceOptions (Lio/sentry/SentryOptions;)Z
1938+
public fun replaceOptions (Lio/sentry/SentryOptions;)V
19271939
public fun setActiveSpan (Lio/sentry/ISpan;)V
19281940
public fun setContexts (Ljava/lang/String;Ljava/lang/Boolean;)V
19291941
public fun setContexts (Ljava/lang/String;Ljava/lang/Character;)V
@@ -2706,6 +2718,7 @@ public class io/sentry/SentryOptions {
27062718
public fun getIgnoredSpanOrigins ()Ljava/util/List;
27072719
public fun getInAppExcludes ()Ljava/util/List;
27082720
public fun getInAppIncludes ()Ljava/util/List;
2721+
public fun getInitPriority ()Lio/sentry/InitPriority;
27092722
public fun getInstrumenter ()Lio/sentry/Instrumenter;
27102723
public fun getIntegrations ()Ljava/util/List;
27112724
public fun getInternalTracesSampler ()Lio/sentry/TracesSampler;
@@ -2775,6 +2788,7 @@ public class io/sentry/SentryOptions {
27752788
public fun isEnableUserInteractionBreadcrumbs ()Z
27762789
public fun isEnableUserInteractionTracing ()Z
27772790
public fun isEnabled ()Z
2791+
public fun isForceInit ()Z
27782792
public fun isPrintUncaughtStackTrace ()Z
27792793
public fun isProfilingEnabled ()Z
27802794
public fun isSendClientReports ()Z
@@ -2828,10 +2842,12 @@ public class io/sentry/SentryOptions {
28282842
public fun setEnvironment (Ljava/lang/String;)V
28292843
public fun setExecutorService (Lio/sentry/ISentryExecutorService;)V
28302844
public fun setFlushTimeoutMillis (J)V
2845+
public fun setForceInit (Z)V
28312846
public fun setGestureTargetLocators (Ljava/util/List;)V
28322847
public fun setIdleTimeout (Ljava/lang/Long;)V
28332848
public fun setIgnoredCheckIns (Ljava/util/List;)V
28342849
public fun setIgnoredSpanOrigins (Ljava/util/List;)V
2850+
public fun setInitPriority (Lio/sentry/InitPriority;)V
28352851
public fun setInstrumenter (Lio/sentry/Instrumenter;)V
28362852
public fun setLogger (Lio/sentry/ILogger;)V
28372853
public fun setMainThreadChecker (Lio/sentry/util/thread/IMainThreadChecker;)V
@@ -5548,6 +5564,11 @@ public final class io/sentry/util/HttpUtils {
55485564
public static fun isSecurityCookie (Ljava/lang/String;Ljava/util/List;)Z
55495565
}
55505566

5567+
public final class io/sentry/util/InitUtil {
5568+
public fun <init> ()V
5569+
public static fun shouldInit (Lio/sentry/SentryOptions;Lio/sentry/SentryOptions;Z)Z
5570+
}
5571+
55515572
public final class io/sentry/util/IntegrationUtils {
55525573
public fun <init> ()V
55535574
public static fun addIntegrationToSdkVersion (Ljava/lang/Class;)V

sentry/src/main/java/io/sentry/CombinedScopeView.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -486,7 +486,7 @@ public void setSpanContext(
486486

487487
@ApiStatus.Internal
488488
@Override
489-
public boolean replaceOptions(@NotNull SentryOptions options) {
490-
return globalScope.replaceOptions(options);
489+
public void replaceOptions(@NotNull SentryOptions options) {
490+
globalScope.replaceOptions(options);
491491
}
492492
}

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ public final class ExternalOptions {
5151
private @Nullable Boolean sendModules;
5252
private @Nullable Boolean sendDefaultPii;
5353
private @Nullable Boolean enableBackpressureHandling;
54+
private @Nullable Boolean forceInit;
5455

5556
private @Nullable SentryOptions.Cron cron;
5657

@@ -73,6 +74,7 @@ public final class ExternalOptions {
7374
options.setDebug(propertiesProvider.getBooleanProperty("debug"));
7475
options.setEnableDeduplication(propertiesProvider.getBooleanProperty("enable-deduplication"));
7576
options.setSendClientReports(propertiesProvider.getBooleanProperty("send-client-reports"));
77+
options.setForceInit(propertiesProvider.getBooleanProperty("force-init"));
7678
final String maxRequestBodySize = propertiesProvider.getProperty("max-request-body-size");
7779
if (maxRequestBodySize != null) {
7880
options.setMaxRequestBodySize(
@@ -451,6 +453,14 @@ public void setEnableBackpressureHandling(final @Nullable Boolean enableBackpres
451453
return enableBackpressureHandling;
452454
}
453455

456+
public void setForceInit(final @Nullable Boolean forceInit) {
457+
this.forceInit = forceInit;
458+
}
459+
460+
public @Nullable Boolean isForceInit() {
461+
return forceInit;
462+
}
463+
454464
@ApiStatus.Experimental
455465
public @Nullable SentryOptions.Cron getCron() {
456466
return cron;

sentry/src/main/java/io/sentry/IScope.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -404,5 +404,5 @@ void setSpanContext(
404404
final @NotNull String transactionName);
405405

406406
@ApiStatus.Internal
407-
boolean replaceOptions(final @NotNull SentryOptions options);
407+
void replaceOptions(final @NotNull SentryOptions options);
408408
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package io.sentry;
2+
3+
import org.jetbrains.annotations.ApiStatus;
4+
5+
@ApiStatus.Internal
6+
public enum InitPriority {
7+
LOWEST,
8+
LOW,
9+
MEDIUM,
10+
HIGH,
11+
HIGHEST;
12+
}

sentry/src/main/java/io/sentry/NoOpScope.java

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -285,7 +285,5 @@ public void setSpanContext(
285285
@NotNull Throwable throwable, @NotNull ISpan span, @NotNull String transactionName) {}
286286

287287
@Override
288-
public boolean replaceOptions(@NotNull SentryOptions options) {
289-
return false;
290-
}
288+
public void replaceOptions(@NotNull SentryOptions options) {}
291289
}

sentry/src/main/java/io/sentry/Scope.java

Lines changed: 10 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1054,22 +1054,17 @@ public void setSpanContext(
10541054

10551055
@ApiStatus.Internal
10561056
@Override
1057-
public boolean replaceOptions(final @NotNull SentryOptions options) {
1058-
// TODO [POTEL] implement some override mechanism
1059-
if (!getClient().isEnabled()) {
1060-
this.options = options;
1061-
final Queue<Breadcrumb> oldBreadcrumbs = breadcrumbs;
1062-
breadcrumbs = createBreadcrumbsList(options.getMaxBreadcrumbs());
1063-
for (Breadcrumb breadcrumb : oldBreadcrumbs) {
1064-
/*
1065-
this should trigger beforeBreadcrumb
1066-
and notify observers for breadcrumbs added before options where customized in Sentry.init
1067-
*/
1068-
addBreadcrumb(breadcrumb);
1069-
}
1070-
return true;
1057+
public void replaceOptions(final @NotNull SentryOptions options) {
1058+
this.options = options;
1059+
final Queue<Breadcrumb> oldBreadcrumbs = breadcrumbs;
1060+
breadcrumbs = createBreadcrumbsList(options.getMaxBreadcrumbs());
1061+
for (Breadcrumb breadcrumb : oldBreadcrumbs) {
1062+
/*
1063+
this should trigger beforeBreadcrumb
1064+
and notify observers for breadcrumbs added before options where customized in Sentry.init
1065+
*/
1066+
addBreadcrumb(breadcrumb);
10711067
}
1072-
return false;
10731068
}
10741069

10751070
/** The IWithTransaction callback */

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

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
import io.sentry.transport.NoOpEnvelopeCache;
1818
import io.sentry.util.DebugMetaPropertiesApplier;
1919
import io.sentry.util.FileUtils;
20+
import io.sentry.util.InitUtil;
2021
import io.sentry.util.LoadClass;
2122
import io.sentry.util.Platform;
2223
import io.sentry.util.thread.IMainThreadChecker;
@@ -278,24 +279,22 @@ private static synchronized void init(
278279

279280
options.getLogger().log(SentryLevel.INFO, "GlobalHubMode: '%s'", String.valueOf(globalHubMode));
280281
Sentry.globalHubMode = globalHubMode;
281-
final boolean didReplaceOptions = globalScope.replaceOptions(options);
282+
final boolean shouldInit = InitUtil.shouldInit(globalScope.getOptions(), options, isEnabled());
283+
if (shouldInit) {
284+
globalScope.replaceOptions(options);
282285

283-
// since replaceOptions can noop, we retrieve the options to use from global scope
284-
final @NotNull SentryOptions optionsToUse = globalScope.getOptions();
286+
final IScopes scopes = getCurrentScopes();
287+
final IScope rootScope = new Scope(options);
288+
final IScope rootIsolationScope = new Scope(options);
289+
rootScopes = new Scopes(rootScope, rootIsolationScope, globalScope, "Sentry.init");
285290

286-
final IScopes scopes = getCurrentScopes();
287-
final IScope rootScope = new Scope(optionsToUse);
288-
final IScope rootIsolationScope = new Scope(optionsToUse);
289-
rootScopes = new Scopes(rootScope, rootIsolationScope, globalScope, "Sentry.init");
290-
291-
getScopesStorage().set(rootScopes);
291+
getScopesStorage().set(rootScopes);
292292

293-
if (didReplaceOptions) {
294293
scopes.close(true);
295294

296-
initConfigurations(optionsToUse);
295+
initConfigurations(options);
297296

298-
globalScope.bindClient(new SentryClient(optionsToUse));
297+
globalScope.bindClient(new SentryClient(options));
299298

300299
// If the executorService passed in the init is the same that was previously closed, we have
301300
// to
@@ -316,6 +315,12 @@ private static synchronized void init(
316315
finalizePreviousSession(options, ScopesAdapter.getInstance());
317316

318317
handleAppStartProfilingConfig(options, options.getExecutorService());
318+
} else {
319+
options
320+
.getLogger()
321+
.log(
322+
SentryLevel.WARNING,
323+
"This init call has been ignored due to priority being too low.");
319324
}
320325
}
321326

0 commit comments

Comments
 (0)