Skip to content

Commit

Permalink
Merge 1169294 into 5583fa5
Browse files Browse the repository at this point in the history
  • Loading branch information
adinauer authored Sep 2, 2024
2 parents 5583fa5 + 1169294 commit ee4e8d3
Show file tree
Hide file tree
Showing 12 changed files with 269 additions and 39 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,7 @@ class SentryAutoConfigurationTest {
"sentry.send-modules=false",
"sentry.ignored-checkins=slug1,slugB",
"sentry.enable-backpressure-handling=false",
"sentry.force-init=true",
"sentry.cron.default-checkin-margin=10",
"sentry.cron.default-max-runtime=30",
"sentry.cron.default-timezone=America/New_York",
Expand Down Expand Up @@ -209,6 +210,7 @@ class SentryAutoConfigurationTest {
assertThat(options.isSendModules).isEqualTo(false)
assertThat(options.ignoredCheckIns).containsOnly("slug1", "slugB")
assertThat(options.isEnableBackpressureHandling).isEqualTo(false)
assertThat(options.isForceInit).isEqualTo(true)
assertThat(options.cron).isNotNull
assertThat(options.cron!!.defaultCheckinMargin).isEqualTo(10L)
assertThat(options.cron!!.defaultMaxRuntime).isEqualTo(30L)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,7 @@ class SentryAutoConfigurationTest {
"sentry.send-modules=false",
"sentry.ignored-checkins=slug1,slugB",
"sentry.enable-backpressure-handling=false",
"sentry.force-init=true",
"sentry.cron.default-checkin-margin=10",
"sentry.cron.default-max-runtime=30",
"sentry.cron.default-timezone=America/New_York",
Expand Down Expand Up @@ -208,6 +209,7 @@ class SentryAutoConfigurationTest {
assertThat(options.isSendModules).isEqualTo(false)
assertThat(options.ignoredCheckIns).containsOnly("slug1", "slugB")
assertThat(options.isEnableBackpressureHandling).isEqualTo(false)
assertThat(options.isForceInit).isEqualTo(true)
assertThat(options.cron).isNotNull
assertThat(options.cron!!.defaultCheckinMargin).isEqualTo(10L)
assertThat(options.cron!!.defaultMaxRuntime).isEqualTo(30L)
Expand Down
21 changes: 21 additions & 0 deletions sentry/api/sentry.api
Original file line number Diff line number Diff line change
Expand Up @@ -459,6 +459,7 @@ public final class io/sentry/ExternalOptions {
public fun isEnableBackpressureHandling ()Ljava/lang/Boolean;
public fun isEnablePrettySerializationOutput ()Ljava/lang/Boolean;
public fun isEnabled ()Ljava/lang/Boolean;
public fun isForceInit ()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 @@ -472,6 +473,7 @@ public final class io/sentry/ExternalOptions {
public fun setEnableUncaughtExceptionHandler (Ljava/lang/Boolean;)V
public fun setEnabled (Ljava/lang/Boolean;)V
public fun setEnvironment (Ljava/lang/String;)V
public fun setForceInit (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 @@ -1028,6 +1030,16 @@ public abstract interface class io/sentry/ITransportFactory {
public abstract fun create (Lio/sentry/SentryOptions;Lio/sentry/RequestDetails;)Lio/sentry/transport/ITransport;
}

public final class io/sentry/InitPriority : java/lang/Enum {
public static final field HIGH Lio/sentry/InitPriority;
public static final field HIGHEST Lio/sentry/InitPriority;
public static final field LOW Lio/sentry/InitPriority;
public static final field LOWEST Lio/sentry/InitPriority;
public static final field MEDIUM Lio/sentry/InitPriority;
public static fun valueOf (Ljava/lang/String;)Lio/sentry/InitPriority;
public static fun values ()[Lio/sentry/InitPriority;
}

public final class io/sentry/Instrumenter : java/lang/Enum {
public static final field OTEL Lio/sentry/Instrumenter;
public static final field SENTRY Lio/sentry/Instrumenter;
Expand Down Expand Up @@ -2706,6 +2718,7 @@ public class io/sentry/SentryOptions {
public fun getIgnoredSpanOrigins ()Ljava/util/List;
public fun getInAppExcludes ()Ljava/util/List;
public fun getInAppIncludes ()Ljava/util/List;
public fun getInitPriority ()Lio/sentry/InitPriority;
public fun getInstrumenter ()Lio/sentry/Instrumenter;
public fun getIntegrations ()Ljava/util/List;
public fun getInternalTracesSampler ()Lio/sentry/TracesSampler;
Expand Down Expand Up @@ -2775,6 +2788,7 @@ public class io/sentry/SentryOptions {
public fun isEnableUserInteractionBreadcrumbs ()Z
public fun isEnableUserInteractionTracing ()Z
public fun isEnabled ()Z
public fun isForceInit ()Z
public fun isPrintUncaughtStackTrace ()Z
public fun isProfilingEnabled ()Z
public fun isSendClientReports ()Z
Expand Down Expand Up @@ -2828,10 +2842,12 @@ public class io/sentry/SentryOptions {
public fun setEnvironment (Ljava/lang/String;)V
public fun setExecutorService (Lio/sentry/ISentryExecutorService;)V
public fun setFlushTimeoutMillis (J)V
public fun setForceInit (Z)V
public fun setGestureTargetLocators (Ljava/util/List;)V
public fun setIdleTimeout (Ljava/lang/Long;)V
public fun setIgnoredCheckIns (Ljava/util/List;)V
public fun setIgnoredSpanOrigins (Ljava/util/List;)V
public fun setInitPriority (Lio/sentry/InitPriority;)V
public fun setInstrumenter (Lio/sentry/Instrumenter;)V
public fun setLogger (Lio/sentry/ILogger;)V
public fun setMainThreadChecker (Lio/sentry/util/thread/IMainThreadChecker;)V
Expand Down Expand Up @@ -5548,6 +5564,11 @@ public final class io/sentry/util/HttpUtils {
public static fun isSecurityCookie (Ljava/lang/String;Ljava/util/List;)Z
}

public final class io/sentry/util/InitUtil {
public fun <init> ()V
public static fun shouldInit (Lio/sentry/SentryOptions;Lio/sentry/SentryOptions;Z)Z
}

public final class io/sentry/util/IntegrationUtils {
public fun <init> ()V
public static fun addIntegrationToSdkVersion (Ljava/lang/Class;)V
Expand Down
10 changes: 10 additions & 0 deletions sentry/src/main/java/io/sentry/ExternalOptions.java
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ public final class ExternalOptions {
private @Nullable Boolean sendModules;
private @Nullable Boolean sendDefaultPii;
private @Nullable Boolean enableBackpressureHandling;
private @Nullable Boolean forceInit;

private @Nullable SentryOptions.Cron cron;

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

public void setForceInit(final @Nullable Boolean forceInit) {
this.forceInit = forceInit;
}

public @Nullable Boolean isForceInit() {
return forceInit;
}

@ApiStatus.Experimental
public @Nullable SentryOptions.Cron getCron() {
return cron;
Expand Down
12 changes: 12 additions & 0 deletions sentry/src/main/java/io/sentry/InitPriority.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package io.sentry;

import org.jetbrains.annotations.ApiStatus;

@ApiStatus.Internal
public enum InitPriority {
LOWEST,
LOW,
MEDIUM,
HIGH,
HIGHEST;
}
20 changes: 9 additions & 11 deletions sentry/src/main/java/io/sentry/Scope.java
Original file line number Diff line number Diff line change
Expand Up @@ -1055,17 +1055,15 @@ public void setSpanContext(
@ApiStatus.Internal
@Override
public void replaceOptions(final @NotNull SentryOptions options) {
if (!getClient().isEnabled()) {
this.options = options;
final Queue<Breadcrumb> oldBreadcrumbs = breadcrumbs;
breadcrumbs = createBreadcrumbsList(options.getMaxBreadcrumbs());
for (Breadcrumb breadcrumb : oldBreadcrumbs) {
/*
this should trigger beforeBreadcrumb
and notify observers for breadcrumbs added before options where customized in Sentry.init
*/
addBreadcrumb(breadcrumb);
}
this.options = options;
final Queue<Breadcrumb> oldBreadcrumbs = breadcrumbs;
breadcrumbs = createBreadcrumbsList(options.getMaxBreadcrumbs());
for (Breadcrumb breadcrumb : oldBreadcrumbs) {
/*
this should trigger beforeBreadcrumb
and notify observers for breadcrumbs added before options where customized in Sentry.init
*/
addBreadcrumb(breadcrumb);
}
}

Expand Down
71 changes: 43 additions & 28 deletions sentry/src/main/java/io/sentry/Sentry.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import io.sentry.transport.NoOpEnvelopeCache;
import io.sentry.util.DebugMetaPropertiesApplier;
import io.sentry.util.FileUtils;
import io.sentry.util.InitUtil;
import io.sentry.util.LoadClass;
import io.sentry.util.Platform;
import io.sentry.util.thread.IMainThreadChecker;
Expand Down Expand Up @@ -279,43 +280,55 @@ private static synchronized void init(
"Sentry has been already initialized. Previous configuration will be overwritten.");
}

if (!initConfigurations(options)) {
if (!preInitConfigurations(options)) {
return;
}

options.getLogger().log(SentryLevel.INFO, "GlobalHubMode: '%s'", String.valueOf(globalHubMode));
Sentry.globalHubMode = globalHubMode;
globalScope.replaceOptions(options);
final boolean shouldInit = InitUtil.shouldInit(globalScope.getOptions(), options, isEnabled());
if (shouldInit) {
globalScope.replaceOptions(options);

final IScopes scopes = getCurrentScopes();
final IScope rootScope = new Scope(options);
final IScope rootIsolationScope = new Scope(options);
rootScopes = new Scopes(rootScope, rootIsolationScope, globalScope, "Sentry.init");
final IScopes scopes = getCurrentScopes();
final IScope rootScope = new Scope(options);
final IScope rootIsolationScope = new Scope(options);
rootScopes = new Scopes(rootScope, rootIsolationScope, globalScope, "Sentry.init");

getScopesStorage().set(rootScopes);
getScopesStorage().set(rootScopes);

scopes.close(true);
globalScope.bindClient(new SentryClient(rootScopes.getOptions()));
scopes.close(true);

// If the executorService passed in the init is the same that was previously closed, we have to
// set a new one
if (options.getExecutorService().isClosed()) {
options.setExecutorService(new SentryExecutorService());
}
initConfigurations(options);

// when integrations are registered on Scopes ctor and async integrations are fired,
// it might and actually happened that integrations called captureSomething
// and Scopes was still NoOp.
// Registering integrations here make sure that Scopes is already created.
for (final Integration integration : options.getIntegrations()) {
integration.register(ScopesAdapter.getInstance(), options);
}
globalScope.bindClient(new SentryClient(options));

// If the executorService passed in the init is the same that was previously closed, we have
// to
// set a new one
if (options.getExecutorService().isClosed()) {
options.setExecutorService(new SentryExecutorService());
}
// when integrations are registered on Scopes ctor and async integrations are fired,
// it might and actually happened that integrations called captureSomething
// and Scopes was still NoOp.
// Registering integrations here make sure that Scopes is already created.
for (final Integration integration : options.getIntegrations()) {
integration.register(ScopesAdapter.getInstance(), options);
}

notifyOptionsObservers(options);
notifyOptionsObservers(options);

finalizePreviousSession(options, ScopesAdapter.getInstance());
finalizePreviousSession(options, ScopesAdapter.getInstance());

handleAppStartProfilingConfig(options, options.getExecutorService());
handleAppStartProfilingConfig(options, options.getExecutorService());
} else {
options
.getLogger()
.log(
SentryLevel.WARNING,
"This init call has been ignored due to priority being too low.");
}
}

@SuppressWarnings("FutureReturnValueIgnored")
Expand Down Expand Up @@ -419,8 +432,7 @@ private static void notifyOptionsObservers(final @NotNull SentryOptions options)
}
}

@SuppressWarnings("FutureReturnValueIgnored")
private static boolean initConfigurations(final @NotNull SentryOptions options) {
private static boolean preInitConfigurations(final @NotNull SentryOptions options) {
if (options.isEnableExternalConfiguration()) {
options.merge(ExternalOptions.from(PropertiesProviderFactory.create(), options.getLogger()));
}
Expand All @@ -438,6 +450,11 @@ private static boolean initConfigurations(final @NotNull SentryOptions options)
@SuppressWarnings("unused")
final Dsn parsedDsn = new Dsn(dsn);

return true;
}

@SuppressWarnings("FutureReturnValueIgnored")
private static void initConfigurations(final @NotNull SentryOptions options) {
ILogger logger = options.getLogger();

if (options.isDebug() && logger instanceof NoOpLogger) {
Expand Down Expand Up @@ -534,8 +551,6 @@ private static boolean initConfigurations(final @NotNull SentryOptions options)
options.setBackpressureMonitor(new BackpressureMonitor(options, ScopesAdapter.getInstance()));
options.getBackpressureMonitor().start();
}

return true;
}

/** Close the SDK */
Expand Down
34 changes: 34 additions & 0 deletions sentry/src/main/java/io/sentry/SentryOptions.java
Original file line number Diff line number Diff line change
Expand Up @@ -489,6 +489,9 @@ public class SentryOptions {

private @NotNull ScopeType defaultScopeType = ScopeType.ISOLATION;

private @NotNull InitPriority initPriority = InitPriority.MEDIUM;
private boolean forceInit = false;

/**
* Adds an event processor
*
Expand Down Expand Up @@ -2440,6 +2443,33 @@ public void setDefaultScopeType(final @NotNull ScopeType scopeType) {
return defaultScopeType;
}

@ApiStatus.Internal
public void setInitPriority(final @NotNull InitPriority initPriority) {
this.initPriority = initPriority;
}

@ApiStatus.Internal
public @NotNull InitPriority getInitPriority() {
return initPriority;
}

/**
* If set to true a call to Sentry.init (or SentryAndroid.init) will go through and replace
* previous options if there are any.
*
* <p>By default the SDK will check whether a previous call to Sentry.init has higher priority
* than the current one and decide whether to actually perform the init and replace options.
*
* @param forceInit true = replace previous init and options
*/
public void setForceInit(final boolean forceInit) {
this.forceInit = forceInit;
}

public boolean isForceInit() {
return forceInit;
}

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

Expand Down Expand Up @@ -2562,6 +2592,7 @@ public SentryOptions() {
*/
private SentryOptions(final boolean empty) {
if (!empty) {
setInitPriority(InitPriority.LOWEST);
setSpanFactory(new DefaultSpanFactory());
// SentryExecutorService should be initialized before any
// SendCachedEventFireAndForgetIntegration
Expand Down Expand Up @@ -2636,6 +2667,9 @@ public void merge(final @NotNull ExternalOptions options) {
if (options.getSendClientReports() != null) {
setSendClientReports(options.getSendClientReports());
}
if (options.isForceInit() != null) {
setForceInit(options.isForceInit());
}
final Map<String, String> tags = new HashMap<>(options.getTags());
for (final Map.Entry<String, String> tag : tags.entrySet()) {
this.tags.put(tag.getKey(), tag.getValue());
Expand Down
28 changes: 28 additions & 0 deletions sentry/src/main/java/io/sentry/util/InitUtil.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package io.sentry.util;

import io.sentry.SentryOptions;
import org.jetbrains.annotations.ApiStatus;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

@ApiStatus.Internal
public final class InitUtil {
public static boolean shouldInit(
final @Nullable SentryOptions previousOptions,
final @NotNull SentryOptions newOptions,
final boolean isEnabled) {
if (!isEnabled) {
return true;
}

if (previousOptions == null) {
return true;
}

if (newOptions.isForceInit()) {
return true;
}

return previousOptions.getInitPriority().ordinal() <= newOptions.getInitPriority().ordinal();
}
}
Loading

0 comments on commit ee4e8d3

Please sign in to comment.