Skip to content
This repository was archived by the owner on Aug 30, 2023. It is now read-only.

Commit 2d6e215

Browse files
Refactor binding options (#530)
1 parent 68c7ceb commit 2d6e215

File tree

7 files changed

+44
-422
lines changed

7 files changed

+44
-422
lines changed

sentry-core/src/main/java/io/sentry/core/SentryOptions.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ public class SentryOptions {
4848
* background queue and this queue is given a certain amount to drain pending events Default is
4949
* 2000 = 2s
5050
*/
51-
private long shutdownTimeoutMillis = 2000; // 2s
51+
private long shutdownTimeout = 2000; // 2s
5252

5353
/**
5454
* Controls how many seconds to wait before flushing down. Sentry SDKs cache events from a
@@ -369,7 +369,7 @@ public void setEnableNdk(boolean enableNdk) {
369369
* @return the timeout in Millis
370370
*/
371371
public long getShutdownTimeout() {
372-
return shutdownTimeoutMillis;
372+
return shutdownTimeout;
373373
}
374374

375375
/**
@@ -378,7 +378,7 @@ public long getShutdownTimeout() {
378378
* @param shutdownTimeoutMillis the shutdown timeout in millis
379379
*/
380380
public void setShutdownTimeout(long shutdownTimeoutMillis) {
381-
this.shutdownTimeoutMillis = shutdownTimeoutMillis;
381+
this.shutdownTimeout = shutdownTimeoutMillis;
382382
}
383383

384384
/**

sentry-logback/src/main/java/io/sentry/logback/SentryAppender.java

Lines changed: 14 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@
1111
import io.sentry.core.SentryOptions;
1212
import io.sentry.core.protocol.Message;
1313
import io.sentry.core.protocol.SdkVersion;
14-
14+
import io.sentry.core.transport.ITransport;
15+
import io.sentry.core.util.CollectionUtils;
1516
import java.util.Arrays;
1617
import java.util.Collections;
1718
import java.util.Date;
@@ -20,48 +21,22 @@
2021
import java.util.Objects;
2122
import java.util.Optional;
2223
import java.util.stream.Collectors;
23-
24-
import io.sentry.core.transport.ITransport;
25-
import io.sentry.core.util.CollectionUtils;
2624
import org.jetbrains.annotations.ApiStatus;
2725
import org.jetbrains.annotations.NotNull;
2826
import org.jetbrains.annotations.Nullable;
2927

3028
/** Appender for logback in charge of sending the logged events to a Sentry server. */
3129
public final class SentryAppender extends UnsynchronizedAppenderBase<ILoggingEvent> {
32-
private @Nullable String dsn;
33-
private @Nullable String environment;
34-
private @Nullable Integer maxBreadcrumbs;
35-
private @Nullable Integer shutdownTimeoutMillis;
36-
private @Nullable Integer flushTimeoutMillis;
37-
private @Nullable Integer readTimeoutMillis;
38-
private @Nullable Double sampleRate;
39-
private @Nullable Boolean bypassSecurity;
40-
private @Nullable Boolean debug;
41-
private @Nullable Boolean attachThreads;
42-
private @Nullable Boolean attachStacktrace;
30+
private @Nullable SentryOptions options;
4331
private @Nullable ITransport transport;
4432

4533
@Override
4634
public void start() {
47-
if (dsn != null) {
48-
Sentry.init(
49-
options -> {
50-
options.setDsn(dsn);
51-
Optional.ofNullable(maxBreadcrumbs).ifPresent(options::setMaxBreadcrumbs);
52-
Optional.ofNullable(environment).ifPresent(options::setEnvironment);
53-
Optional.ofNullable(shutdownTimeoutMillis).ifPresent(options::setShutdownTimeout);
54-
Optional.ofNullable(flushTimeoutMillis).ifPresent(options::setFlushTimeoutMillis);
55-
Optional.ofNullable(readTimeoutMillis).ifPresent(options::setReadTimeoutMillis);
56-
Optional.ofNullable(sampleRate).ifPresent(options::setSampleRate);
57-
Optional.ofNullable(bypassSecurity).ifPresent(options::setBypassSecurity);
58-
Optional.ofNullable(debug).ifPresent(options::setDebug);
59-
Optional.ofNullable(attachThreads).ifPresent(options::setAttachThreads);
60-
Optional.ofNullable(attachStacktrace).ifPresent(options::setAttachStacktrace);
61-
options.setSentryClientName(BuildConfig.SENTRY_LOGBACK_SDK_NAME);
62-
options.setSdkVersion(createSdkVersion(options));
63-
Optional.ofNullable(transport).ifPresent(options::setTransport);
64-
});
35+
if (options != null && options.getDsn() != null) {
36+
options.setSentryClientName(BuildConfig.SENTRY_LOGBACK_SDK_NAME);
37+
options.setSdkVersion(createSdkVersion(options));
38+
Optional.ofNullable(transport).ifPresent(options::setTransport);
39+
Sentry.init(options);
6540
}
6641
super.start();
6742
}
@@ -79,7 +54,8 @@ protected void append(@NotNull ILoggingEvent eventObject) {
7954
*/
8055
@SuppressWarnings("JdkObsolete")
8156
final @NotNull SentryEvent createEvent(@NotNull ILoggingEvent loggingEvent) {
82-
final SentryEvent event = new SentryEvent(DateUtils.getDateTime(new Date(loggingEvent.getTimeStamp())));
57+
final SentryEvent event =
58+
new SentryEvent(DateUtils.getDateTime(new Date(loggingEvent.getTimeStamp())));
8359
final Message message = new Message();
8460
message.setMessage(loggingEvent.getMessage());
8561
message.setFormatted(loggingEvent.getFormattedMessage());
@@ -97,7 +73,8 @@ protected void append(@NotNull ILoggingEvent eventObject) {
9773
event.setExtra("thread_name", loggingEvent.getThreadName());
9874
}
9975

100-
final Map<String, String> mdcProperties = CollectionUtils.shallowCopy(loggingEvent.getMDCPropertyMap());
76+
final Map<String, String> mdcProperties =
77+
CollectionUtils.shallowCopy(loggingEvent.getMDCPropertyMap());
10178
if (!mdcProperties.isEmpty()) {
10279
event.getContexts().put("MDC", mdcProperties);
10380
}
@@ -149,48 +126,8 @@ protected void append(@NotNull ILoggingEvent eventObject) {
149126
return sdkVersion;
150127
}
151128

152-
public void setDsn(@Nullable String dsn) {
153-
this.dsn = dsn;
154-
}
155-
156-
public void setEnvironment(@Nullable String environment) {
157-
this.environment = environment;
158-
}
159-
160-
public void setMaxBreadcrumbs(@Nullable Integer maxBreadcrumbs) {
161-
this.maxBreadcrumbs = maxBreadcrumbs;
162-
}
163-
164-
public void setShutdownTimeoutMillis(@Nullable Integer shutdownTimeoutMillis) {
165-
this.shutdownTimeoutMillis = shutdownTimeoutMillis;
166-
}
167-
168-
public void setFlushTimeoutMillis(@Nullable Integer flushTimeoutMillis) {
169-
this.flushTimeoutMillis = flushTimeoutMillis;
170-
}
171-
172-
public void setReadTimeoutMillis(@Nullable Integer readTimeoutMillis) {
173-
this.readTimeoutMillis = readTimeoutMillis;
174-
}
175-
176-
public void setSampleRate(@Nullable Double sampleRate) {
177-
this.sampleRate = sampleRate;
178-
}
179-
180-
public void setBypassSecurity(@Nullable Boolean bypassSecurity) {
181-
this.bypassSecurity = bypassSecurity;
182-
}
183-
184-
public void setDebug(@Nullable Boolean debug) {
185-
this.debug = debug;
186-
}
187-
188-
public void setAttachThreads(@Nullable Boolean attachThreads) {
189-
this.attachThreads = attachThreads;
190-
}
191-
192-
public void setAttachStacktrace(@Nullable Boolean attachStacktrace) {
193-
this.attachStacktrace = attachStacktrace;
129+
public void setOptions(SentryOptions options) {
130+
this.options = options;
194131
}
195132

196133
@ApiStatus.Internal

sentry-logback/src/test/kotlin/io/sentry/logback/SentryAppenderTest.kt

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -9,22 +9,23 @@ import com.nhaarman.mockitokotlin2.verify
99
import com.nhaarman.mockitokotlin2.whenever
1010
import io.sentry.core.SentryEvent
1111
import io.sentry.core.SentryLevel
12+
import io.sentry.core.SentryOptions
1213
import io.sentry.core.transport.ITransport
1314
import io.sentry.core.transport.TransportResult
14-
import org.awaitility.kotlin.await
15-
import kotlin.test.Test
16-
import kotlin.test.assertEquals
17-
import kotlin.test.assertNotNull
18-
import org.slf4j.Logger
19-
import org.slf4j.LoggerFactory
20-
import org.slf4j.MDC
2115
import java.time.Instant
2216
import java.time.LocalDateTime
2317
import java.time.ZoneId
2418
import kotlin.test.AfterTest
2519
import kotlin.test.BeforeTest
20+
import kotlin.test.Test
21+
import kotlin.test.assertEquals
2622
import kotlin.test.assertFalse
23+
import kotlin.test.assertNotNull
2724
import kotlin.test.assertTrue
25+
import org.awaitility.kotlin.await
26+
import org.slf4j.Logger
27+
import org.slf4j.LoggerFactory
28+
import org.slf4j.MDC
2829

2930
class SentryAppenderTest {
3031
private class Fixture {
@@ -36,7 +37,9 @@ class SentryAppenderTest {
3637
whenever(transport.send(any<SentryEvent>())).thenReturn(TransportResult.success())
3738

3839
val appender = SentryAppender()
39-
appender.setDsn("http://key@localhost/proj")
40+
val options = SentryOptions()
41+
options.dsn = "http://key@localhost/proj"
42+
appender.setOptions(options)
4043
appender.context = loggerContext
4144
appender.setTransport(transport)
4245

@@ -204,8 +207,8 @@ class SentryAppenderTest {
204207
assertEquals(BuildConfig.VERSION_NAME, it.sdk.version)
205208
assertNotNull(it.sdk.packages)
206209
assertTrue(it.sdk.packages!!.any { pkg ->
207-
"maven:sentry-logback" == pkg.name
208-
&& BuildConfig.VERSION_NAME == pkg.version
210+
"maven:sentry-logback" == pkg.name &&
211+
BuildConfig.VERSION_NAME == pkg.version
209212
})
210213
})
211214
}

sentry-samples/sentry-samples-logback/src/main/resources/logback.xml

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,10 @@
77
</appender>
88

99
<appender name="sentry" class="io.sentry.logback.SentryAppender">
10-
<!-- NOTE: Replace the test DSN below with YOUR OWN DSN to see the events from this app in your Sentry project/dashboard -->
11-
<dsn>https://[email protected]/1808954</dsn>
10+
<options>
11+
<!-- NOTE: Replace the test DSN below with YOUR OWN DSN to see the events from this app in your Sentry project/dashboard -->
12+
<dsn>https://[email protected]/1808954</dsn>
13+
</options>
1214

1315
<!-- Optionally you can filter what statements are sent to Sentry independently from loggers configuration -->
1416
<!-- <filter class="ch.qos.logback.classic.filter.ThresholdFilter">-->

sentry-spring-boot-starter/src/main/java/io/sentry/spring/boot/SentryAutoConfiguration.java

Lines changed: 6 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,12 @@
2323
import org.springframework.core.Ordered;
2424

2525
@Configuration
26-
@ConditionalOnProperty(name = "sentry.enabled", havingValue = "true", matchIfMissing = true)
26+
@ConditionalOnProperty(name = "sentry.dsn")
2727
@Open
2828
public class SentryAutoConfiguration {
2929

3030
/** Registers general purpose Sentry related beans. */
3131
@Configuration
32-
@ConditionalOnProperty("sentry.dsn")
3332
@EnableConfigurationProperties(SentryProperties.class)
3433
@Open
3534
static class HubConfiguration {
@@ -55,34 +54,27 @@ static class HubConfiguration {
5554
}
5655

5756
@Bean
58-
public @NotNull SentryOptions sentryOptions(
57+
public @NotNull IHub sentryHub(
5958
final @NotNull Sentry.OptionsConfiguration<SentryOptions> optionsConfiguration,
60-
final @NotNull SentryProperties properties,
59+
final @NotNull SentryProperties options,
6160
final @NotNull ObjectProvider<GitProperties> gitProperties) {
62-
final SentryOptions options = new SentryOptions();
6361
optionsConfiguration.configure(options);
6462
gitProperties.ifAvailable(
6563
git -> {
66-
if (properties.isUseGitCommitIdAsRelease()) {
64+
if (options.getRelease() == null && options.isUseGitCommitIdAsRelease()) {
6765
options.setRelease(git.getCommitId());
6866
}
6967
});
70-
properties.applyTo(options);
68+
7169
options.setSentryClientName(BuildConfig.SENTRY_SPRING_BOOT_SDK_NAME);
7270
options.setSdkVersion(createSdkVersion(options));
73-
return options;
74-
}
75-
76-
@Bean
77-
public @NotNull IHub sentryHub(final @NotNull SentryOptions sentryOptions) {
78-
Sentry.init(sentryOptions);
71+
Sentry.init(options);
7972
return HubAdapter.getInstance();
8073
}
8174

8275
/** Registers beans specific to Spring MVC. */
8376
@Configuration
8477
@ConditionalOnWebApplication(type = ConditionalOnWebApplication.Type.SERVLET)
85-
@ConditionalOnProperty("sentry.dsn")
8678
@Open
8779
static class SentryWebMvcConfiguration {
8880

0 commit comments

Comments
 (0)