Skip to content
This repository was archived by the owner on Aug 30, 2023. It is now read-only.
Closed
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
263 changes: 263 additions & 0 deletions sentry-core/src/main/java/io/sentry/core/SentryCommonOptions.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,263 @@
package io.sentry.core;

import com.jakewharton.nopen.annotation.Open;
import java.util.ArrayList;
import java.util.List;

/** Subset of {@link SentryOptions} properties used in 3rd party framework integrations. */
@Open
public class SentryCommonOptions {

/** @see SentryOptions#dsn */
private String dsn = "";

/** @see SentryOptions#shutdownTimeoutMillis */
private Long shutdownTimeoutMillis;

/** @see SentryOptions#flushTimeoutMillis */
private Long flushTimeoutMillis;

/** @see SentryOptions#readTimeoutMillis */
private Integer readTimeoutMillis;

/** @see SentryOptions#bypassSecurity */
private Boolean bypassSecurity;

/** @see SentryOptions#debug */
private Boolean debug;
Comment on lines +14 to +27
Copy link
Contributor

@marandaneto marandaneto Aug 28, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should we use primitive types? otherwise, we'd need to do NPE checks in the setters too, also for all the other fields that are primitive types in SentryOptions

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The advantage is being able to know if the value was set explicitly by the user and being able to flip the default after invoking a user callback.

But for this field I agree probably doesn't make sense

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As @bruno-garcia said, we need to know which values were set explicitly by user. This way we don't need to keep in sync default values on SentryOptions and SentryCommonOptions. This also includes data type for fields - they have to be nullable otherwise we loose this information.


/** @see SentryOptions#diagnosticLevel */
private SentryLevel diagnosticLevel;

/** @see SentryOptions#maxBreadcrumbs */
private Integer maxBreadcrumbs;

/** @see SentryOptions#release */
private String release;

/** @see SentryOptions#environment */
private String environment;

/** @see SentryOptions#sampleRate */
private Double sampleRate;

/** @see SentryOptions#inAppExcludes */
private List<String> inAppExcludes = new ArrayList<>();

/** @see SentryOptions#inAppIncludes */
private List<String> inAppIncludes = new ArrayList<>();

/** @see SentryOptions#dist */
private String dist;

/** @see SentryOptions#attachThreads */
private Boolean attachThreads;

/** @see SentryOptions#attachStacktrace */
private Boolean attachStacktrace;

/** @see SentryOptions#serverName */
private String serverName;

/**
* Applies configuration from this instance to the {@link SentryOptions} instance.
*
* @param options the instance of {@link SentryOptions} to apply the configuration to
*/
public void applyTo(SentryOptions options) {
if (dsn != null) {
options.setDsn(dsn);
}
if (maxBreadcrumbs != null) {
options.setMaxBreadcrumbs(maxBreadcrumbs);
}
if (environment != null) {
options.setEnvironment(environment);
}
if (shutdownTimeoutMillis != null) {
options.setShutdownTimeout(shutdownTimeoutMillis);
}
if (flushTimeoutMillis != null) {
options.setFlushTimeoutMillis(flushTimeoutMillis);
}
if (readTimeoutMillis != null) {
options.setReadTimeoutMillis(readTimeoutMillis);
}
if (sampleRate != null) {
options.setSampleRate(sampleRate);
}
if (bypassSecurity != null) {
options.setBypassSecurity(bypassSecurity);
}
if (debug != null) {
options.setDebug(debug);
}
if (attachThreads != null) {
options.setAttachThreads(attachThreads);
}
if (attachStacktrace != null) {
options.setAttachStacktrace(attachStacktrace);
}
if (diagnosticLevel != null) {
options.setDiagnosticLevel(diagnosticLevel);
}
if (dist != null) {
options.setDist(dist);
}
if (release != null) {
options.setRelease(release);
}
if (sampleRate != null) {
options.setSampleRate(sampleRate);
}
if (serverName != null) {
options.setServerName(serverName);
}
if (inAppExcludes != null) {
for (String inAppExclude : inAppExcludes) {
options.addInAppExclude(inAppExclude);
}
}
if (inAppIncludes != null) {
for (String inAppInclude : inAppIncludes) {
options.addInAppInclude(inAppInclude);
}
}
}

public String getDsn() {
return dsn;
}

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

public Long getShutdownTimeoutMillis() {
return shutdownTimeoutMillis;
}

public void setShutdownTimeoutMillis(Long shutdownTimeoutMillis) {
this.shutdownTimeoutMillis = shutdownTimeoutMillis;
}

public Long getFlushTimeoutMillis() {
return flushTimeoutMillis;
}

public void setFlushTimeoutMillis(Long flushTimeoutMillis) {
this.flushTimeoutMillis = flushTimeoutMillis;
}

public Integer getReadTimeoutMillis() {
return readTimeoutMillis;
}

public void setReadTimeoutMillis(Integer readTimeoutMillis) {
this.readTimeoutMillis = readTimeoutMillis;
}

public Boolean getBypassSecurity() {
return bypassSecurity;
}

public void setBypassSecurity(Boolean bypassSecurity) {
this.bypassSecurity = bypassSecurity;
}

public Boolean getDebug() {
return debug;
}

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

public SentryLevel getDiagnosticLevel() {
return diagnosticLevel;
}

public void setDiagnosticLevel(SentryLevel diagnosticLevel) {
this.diagnosticLevel = diagnosticLevel;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if you look at SentryOptions.setDiagnosticLevel you see that we don't allow null values, and here it is possible, if one sets it to null, it'll crash the app.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In applyTo there is a check for null. The idea is that we apply only properties explicitly set by user.

}

public Integer getMaxBreadcrumbs() {
return maxBreadcrumbs;
}

public void setMaxBreadcrumbs(Integer maxBreadcrumbs) {
this.maxBreadcrumbs = maxBreadcrumbs;
}

public String getRelease() {
return release;
}

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

public String getEnvironment() {
return environment;
}

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

public Double getSampleRate() {
return sampleRate;
}

public void setSampleRate(Double sampleRate) {
this.sampleRate = sampleRate;
}

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

public void setInAppExcludes(List<String> inAppExcludes) {
this.inAppExcludes = inAppExcludes;
}

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

public void setInAppIncludes(List<String> inAppIncludes) {
this.inAppIncludes = inAppIncludes;
}

public String getDist() {
return dist;
}

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

public Boolean getAttachThreads() {
return attachThreads;
}

public void setAttachThreads(Boolean attachThreads) {
this.attachThreads = attachThreads;
}

public Boolean getAttachStacktrace() {
return attachStacktrace;
}

public void setAttachStacktrace(Boolean attachStacktrace) {
this.attachStacktrace = attachStacktrace;
}

public String getServerName() {
return serverName;
}

public void setServerName(String serverName) {
this.serverName = serverName;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package io.sentry.core

import kotlin.test.Test
import kotlin.test.assertEquals
import kotlin.test.assertTrue

class SentryCommonOptionsTest {

@Test
fun `applies properties to SentryOptions`() {
val commonOptions = with(SentryCommonOptions()) {
dsn = "http://key@localhost/proj"
readTimeoutMillis = 10
shutdownTimeoutMillis = 20L
flushTimeoutMillis = 30
bypassSecurity = true
debug = true
diagnosticLevel = SentryLevel.INFO
maxBreadcrumbs = 100
release = "1.0.3"
environment = "production"
sampleRate = 0.2
inAppExcludes = listOf("org.springframework")
inAppIncludes = listOf("com.myapp")
dist = "my-dist"
attachStacktrace = true
attachThreads = true
serverName = "host-001"
this
}

val options = SentryOptions()
commonOptions.applyTo(options)

assertEquals(10, options.readTimeoutMillis)
assertEquals(20, options.shutdownTimeout)
assertEquals(30, options.flushTimeoutMillis)
assertEquals(true, options.isBypassSecurity)
assertEquals(true, options.isDebug)
assertEquals(SentryLevel.INFO, options.diagnosticLevel)
assertEquals(100, options.maxBreadcrumbs)
assertEquals("1.0.3", options.release)
assertEquals("production", options.environment)
assertEquals(0.2, options.sampleRate)
assertTrue(options.inAppExcludes.contains("org.springframework"))
assertTrue(options.inAppIncludes.contains("com.myapp"))
assertEquals("my-dist", options.dist)
assertEquals(true, options.isAttachThreads)
assertEquals(true, options.isAttachStacktrace)
assertEquals("host-001", options.serverName)
}
}
Loading