Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bind external properties to a dedicated class. #1750

Merged
merged 4 commits into from
Oct 8, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,15 @@
# Changelog

## 6.x.x

* Ref: Bind external properties to a dedicated class. (#1750)

Breaking changes:

- `SentryOptions` can merge properties from `ExternalOptions` instead of another instance of `SentryOptions`
- Following boolean properties from `SentryOptions` that allowed `null` values are now not nullable - `debug`, `enableUncaughtExceptionHandler`, `enableDeduplication`
- `SentryOptions` cannot be created anymore using `PropertiesProvider` with `SentryOptions#from` method. Use `ExternalOptions#from` instead and merge created object with `SentryOptions#merge`

## Unreleased

* Feat: Attach Java vendor and version to events and transactions (#1703)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,9 @@ public void start() {
options -> {
options.setEnableExternalConfiguration(true);
options.setDsn(dsn);
options.setDebug(debug);
if (debug != null) {
options.setDebug(debug);
}
options.setSentryClientName(BuildConfig.SENTRY_LOG4J2_SDK_NAME);
options.setSdkVersion(createSdkVersion(options));
Optional.ofNullable(transportFactory).ifPresent(options::setTransportFactory);
Expand Down
47 changes: 42 additions & 5 deletions sentry/api/sentry.api
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,45 @@ public abstract interface class io/sentry/EventProcessor {
public fun process (Lio/sentry/protocol/SentryTransaction;Ljava/lang/Object;)Lio/sentry/protocol/SentryTransaction;
}

public final class io/sentry/ExternalOptions {
public fun <init> ()V
public fun addIgnoredExceptionForType (Ljava/lang/Class;)V
public fun addInAppExclude (Ljava/lang/String;)V
public fun addInAppInclude (Ljava/lang/String;)V
public fun addTracingOrigin (Ljava/lang/String;)V
public static fun from (Lio/sentry/config/PropertiesProvider;Lio/sentry/ILogger;)Lio/sentry/ExternalOptions;
public fun getDebug ()Ljava/lang/Boolean;
public fun getDist ()Ljava/lang/String;
public fun getDsn ()Ljava/lang/String;
public fun getEnableDeduplication ()Ljava/lang/Boolean;
public fun getEnableUncaughtExceptionHandler ()Ljava/lang/Boolean;
public fun getEnvironment ()Ljava/lang/String;
public fun getIgnoredExceptionsForType ()Ljava/util/Set;
public fun getInAppExcludes ()Ljava/util/List;
public fun getInAppIncludes ()Ljava/util/List;
public fun getMaxRequestBodySize ()Lio/sentry/SentryOptions$RequestSize;
public fun getProguardUuid ()Ljava/lang/String;
public fun getProxy ()Lio/sentry/SentryOptions$Proxy;
public fun getRelease ()Ljava/lang/String;
public fun getServerName ()Ljava/lang/String;
public fun getTags ()Ljava/util/Map;
public fun getTracesSampleRate ()Ljava/lang/Double;
public fun getTracingOrigins ()Ljava/util/List;
public fun setDebug (Ljava/lang/Boolean;)V
public fun setDist (Ljava/lang/String;)V
public fun setDsn (Ljava/lang/String;)V
public fun setEnableDeduplication (Ljava/lang/Boolean;)V
public fun setEnableUncaughtExceptionHandler (Ljava/lang/Boolean;)V
public fun setEnvironment (Ljava/lang/String;)V
public fun setMaxRequestBodySize (Lio/sentry/SentryOptions$RequestSize;)V
public fun setProguardUuid (Ljava/lang/String;)V
public fun setProxy (Lio/sentry/SentryOptions$Proxy;)V
public fun setRelease (Ljava/lang/String;)V
public fun setServerName (Ljava/lang/String;)V
public fun setTag (Ljava/lang/String;Ljava/lang/String;)V
public fun setTracesSampleRate (Ljava/lang/Double;)V
}

public final class io/sentry/GsonSerializer : io/sentry/ISerializer {
public fun <init> (Lio/sentry/SentryOptions;)V
public fun deserialize (Ljava/io/Reader;Ljava/lang/Class;)Ljava/lang/Object;
Expand Down Expand Up @@ -810,7 +849,6 @@ public class io/sentry/SentryOptions {
public fun addIntegration (Lio/sentry/Integration;)V
public fun addScopeObserver (Lio/sentry/IScopeObserver;)V
public fun addTracingOrigin (Ljava/lang/String;)V
public static fun from (Lio/sentry/config/PropertiesProvider;Lio/sentry/ILogger;)Lio/sentry/SentryOptions;
public fun getBeforeBreadcrumb ()Lio/sentry/SentryOptions$BeforeBreadcrumbCallback;
public fun getBeforeSend ()Lio/sentry/SentryOptions$BeforeSendCallback;
public fun getCacheDirPath ()Ljava/lang/String;
Expand All @@ -820,7 +858,6 @@ public class io/sentry/SentryOptions {
public fun getDist ()Ljava/lang/String;
public fun getDistinctId ()Ljava/lang/String;
public fun getDsn ()Ljava/lang/String;
public fun getEnableUncaughtExceptionHandler ()Ljava/lang/Boolean;
public fun getEnvelopeDiskCache ()Lio/sentry/cache/IEnvelopeCache;
public fun getEnvelopeReader ()Lio/sentry/IEnvelopeReader;
public fun getEnvironment ()Ljava/lang/String;
Expand Down Expand Up @@ -880,19 +917,19 @@ public class io/sentry/SentryOptions {
public fun setCacheDirPath (Ljava/lang/String;)V
public fun setCacheDirSize (I)V
public fun setConnectionTimeoutMillis (I)V
public fun setDebug (Ljava/lang/Boolean;)V
public fun setDebug (Z)V
public fun setDiagnosticLevel (Lio/sentry/SentryLevel;)V
public fun setDist (Ljava/lang/String;)V
public fun setDistinctId (Ljava/lang/String;)V
public fun setDsn (Ljava/lang/String;)V
public fun setEnableAutoSessionTracking (Z)V
public fun setEnableDeduplication (Ljava/lang/Boolean;)V
public fun setEnableDeduplication (Z)V
public fun setEnableExternalConfiguration (Z)V
public fun setEnableNdk (Z)V
public fun setEnableScopeSync (Z)V
public fun setEnableSessionTracking (Z)V
public fun setEnableShutdownHook (Z)V
public fun setEnableUncaughtExceptionHandler (Ljava/lang/Boolean;)V
public fun setEnableUncaughtExceptionHandler (Z)V
public fun setEnvelopeDiskCache (Lio/sentry/cache/IEnvelopeCache;)V
public fun setEnvelopeReader (Lio/sentry/IEnvelopeReader;)V
public fun setEnvironment (Ljava/lang/String;)V
Expand Down
243 changes: 243 additions & 0 deletions sentry/src/main/java/io/sentry/ExternalOptions.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,243 @@
package io.sentry;

import io.sentry.config.PropertiesProvider;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.concurrent.CopyOnWriteArraySet;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

/** Externally bindable properties set on {@link SentryOptions}. */
public final class ExternalOptions {

/** The default HTTP proxy port to use if an HTTP Proxy hostname is set but port is not. */
private static final String PROXY_PORT_DEFAULT = "80";

private @Nullable String dsn;
private @Nullable String environment;
private @Nullable String release;
private @Nullable String dist;
private @Nullable String serverName;
private @Nullable Boolean enableUncaughtExceptionHandler;
private @Nullable Boolean debug;
private @Nullable Boolean enableDeduplication;
private @Nullable Double tracesSampleRate;
private @Nullable SentryOptions.RequestSize maxRequestBodySize;
private final @NotNull Map<String, @NotNull String> tags = new ConcurrentHashMap<>();
private @Nullable SentryOptions.Proxy proxy;
private final @NotNull List<String> inAppExcludes = new CopyOnWriteArrayList<>();
private final @NotNull List<String> inAppIncludes = new CopyOnWriteArrayList<>();
private final @NotNull List<String> tracingOrigins = new CopyOnWriteArrayList<>();
private @Nullable String proguardUuid;
private final @NotNull Set<Class<? extends Throwable>> ignoredExceptionsForType =
new CopyOnWriteArraySet<>();

@SuppressWarnings("unchecked")
public static @NotNull ExternalOptions from(
final @NotNull PropertiesProvider propertiesProvider, final @NotNull ILogger logger) {
final ExternalOptions options = new ExternalOptions();
options.setDsn(propertiesProvider.getProperty("dsn"));
options.setEnvironment(propertiesProvider.getProperty("environment"));
options.setRelease(propertiesProvider.getProperty("release"));
options.setDist(propertiesProvider.getProperty("dist"));
options.setServerName(propertiesProvider.getProperty("servername"));
options.setEnableUncaughtExceptionHandler(
propertiesProvider.getBooleanProperty("uncaught.handler.enabled"));
options.setTracesSampleRate(propertiesProvider.getDoubleProperty("traces-sample-rate"));
options.setDebug(propertiesProvider.getBooleanProperty("debug"));
options.setEnableDeduplication(propertiesProvider.getBooleanProperty("enable-deduplication"));
final String maxRequestBodySize = propertiesProvider.getProperty("max-request-body-size");
if (maxRequestBodySize != null) {
options.setMaxRequestBodySize(
SentryOptions.RequestSize.valueOf(maxRequestBodySize.toUpperCase(Locale.ROOT)));
}
final Map<String, String> tags = propertiesProvider.getMap("tags");
for (final Map.Entry<String, String> tag : tags.entrySet()) {
options.setTag(tag.getKey(), tag.getValue());
}

final String proxyHost = propertiesProvider.getProperty("proxy.host");
final String proxyUser = propertiesProvider.getProperty("proxy.user");
final String proxyPass = propertiesProvider.getProperty("proxy.pass");
final String proxyPort = propertiesProvider.getProperty("proxy.port", PROXY_PORT_DEFAULT);

if (proxyHost != null) {
options.setProxy(new SentryOptions.Proxy(proxyHost, proxyPort, proxyUser, proxyPass));
}

for (final String inAppInclude : propertiesProvider.getList("in-app-includes")) {
options.addInAppInclude(inAppInclude);
}
for (final String inAppExclude : propertiesProvider.getList("in-app-excludes")) {
options.addInAppExclude(inAppExclude);
}
for (final String tracingOrigin : propertiesProvider.getList("tracing-origins")) {
options.addTracingOrigin(tracingOrigin);
}
options.setProguardUuid(propertiesProvider.getProperty("proguard-uuid"));

for (final String ignoredExceptionType :
propertiesProvider.getList("ignored-exceptions-for-type")) {
try {
Class<?> clazz = Class.forName(ignoredExceptionType);
if (Throwable.class.isAssignableFrom(clazz)) {
options.addIgnoredExceptionForType((Class<? extends Throwable>) clazz);
} else {
logger.log(
SentryLevel.WARNING,
"Skipping setting %s as ignored-exception-for-type. Reason: %s does not extend Throwable",
ignoredExceptionType,
ignoredExceptionType);
}
} catch (ClassNotFoundException e) {
logger.log(
SentryLevel.WARNING,
"Skipping setting %s as ignored-exception-for-type. Reason: %s class is not found",
ignoredExceptionType,
ignoredExceptionType);
}
}
return options;
}

public @Nullable String getDsn() {
return dsn;
}

public void setDsn(final @Nullable String dsn) {
this.dsn = dsn;
}

public @Nullable String getEnvironment() {
return environment;
}

public void setEnvironment(final @Nullable String environment) {
this.environment = environment;
}

public @Nullable String getRelease() {
return release;
}

public void setRelease(final @Nullable String release) {
this.release = release;
}

public @Nullable String getDist() {
return dist;
}

public void setDist(final @Nullable String dist) {
this.dist = dist;
}

public @Nullable String getServerName() {
return serverName;
}

public void setServerName(final @Nullable String serverName) {
this.serverName = serverName;
}

public @Nullable Boolean getEnableUncaughtExceptionHandler() {
return enableUncaughtExceptionHandler;
}

public void setEnableUncaughtExceptionHandler(
final @Nullable Boolean enableUncaughtExceptionHandler) {
this.enableUncaughtExceptionHandler = enableUncaughtExceptionHandler;
}

public @NotNull List<String> getTracingOrigins() {
return tracingOrigins;
}

public @Nullable Boolean getDebug() {
return debug;
}

public void setDebug(final @Nullable Boolean debug) {
this.debug = debug;
}

public @Nullable Boolean getEnableDeduplication() {
return enableDeduplication;
}

public void setEnableDeduplication(final @Nullable Boolean enableDeduplication) {
this.enableDeduplication = enableDeduplication;
}

public @Nullable Double getTracesSampleRate() {
return tracesSampleRate;
}

public void setTracesSampleRate(final @Nullable Double tracesSampleRate) {
this.tracesSampleRate = tracesSampleRate;
}

public @Nullable SentryOptions.RequestSize getMaxRequestBodySize() {
return maxRequestBodySize;
}

public void setMaxRequestBodySize(final @Nullable SentryOptions.RequestSize maxRequestBodySize) {
this.maxRequestBodySize = maxRequestBodySize;
}

public @NotNull Map<String, String> getTags() {
return tags;
}

public @Nullable SentryOptions.Proxy getProxy() {
return proxy;
}

public void setProxy(final @Nullable SentryOptions.Proxy proxy) {
this.proxy = proxy;
}

public @NotNull List<String> getInAppExcludes() {
return inAppExcludes;
}

public @NotNull List<String> getInAppIncludes() {
return inAppIncludes;
}

public @Nullable String getProguardUuid() {
return proguardUuid;
}

public void setProguardUuid(final @Nullable String proguardUuid) {
this.proguardUuid = proguardUuid;
}

public @NotNull Set<Class<? extends Throwable>> getIgnoredExceptionsForType() {
return ignoredExceptionsForType;
}

public void addInAppInclude(final @NotNull String include) {
inAppIncludes.add(include);
}

public void addInAppExclude(final @NotNull String exclude) {
inAppExcludes.add(exclude);
}

public void addTracingOrigin(final @NotNull String tracingOrigin) {
this.tracingOrigins.add(tracingOrigin);
}

public void addIgnoredExceptionForType(final @NotNull Class<? extends Throwable> exceptionType) {
this.ignoredExceptionsForType.add(exceptionType);
}

public void setTag(final @NotNull String key, final @NotNull String value) {
this.tags.put(key, value);
}
}
2 changes: 1 addition & 1 deletion sentry/src/main/java/io/sentry/Sentry.java
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@ private static synchronized void init(

private static boolean initConfigurations(final @NotNull SentryOptions options) {
if (options.isEnableExternalConfiguration()) {
options.merge(SentryOptions.from(PropertiesProviderFactory.create(), options.getLogger()));
options.merge(ExternalOptions.from(PropertiesProviderFactory.create(), options.getLogger()));
}

final String dsn = options.getDsn();
Expand Down
Loading