Skip to content
This repository was archived by the owner on Aug 30, 2023. It is now read-only.
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
6 changes: 3 additions & 3 deletions sentry-core/src/main/java/io/sentry/core/SentryOptions.java
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public class SentryOptions {
* background queue and this queue is given a certain amount to drain pending events Default is
* 2000 = 2s
*/
private long shutdownTimeoutMillis = 2000; // 2s
private long shutdownTimeout = 2000; // 2s

/**
* Controls how many seconds to wait before flushing down. Sentry SDKs cache events from a
Expand Down Expand Up @@ -369,7 +369,7 @@ public void setEnableNdk(boolean enableNdk) {
* @return the timeout in Millis
*/
public long getShutdownTimeout() {
return shutdownTimeoutMillis;
return shutdownTimeout;
}

/**
Expand All @@ -378,7 +378,7 @@ public long getShutdownTimeout() {
* @param shutdownTimeoutMillis the shutdown timeout in millis
*/
public void setShutdownTimeout(long shutdownTimeoutMillis) {
this.shutdownTimeoutMillis = shutdownTimeoutMillis;
this.shutdownTimeout = shutdownTimeoutMillis;
}

/**
Expand Down
91 changes: 14 additions & 77 deletions sentry-logback/src/main/java/io/sentry/logback/SentryAppender.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@
import io.sentry.core.SentryOptions;
import io.sentry.core.protocol.Message;
import io.sentry.core.protocol.SdkVersion;

import io.sentry.core.transport.ITransport;
import io.sentry.core.util.CollectionUtils;
import java.util.Arrays;
import java.util.Collections;
import java.util.Date;
Expand All @@ -20,48 +21,22 @@
import java.util.Objects;
import java.util.Optional;
import java.util.stream.Collectors;

import io.sentry.core.transport.ITransport;
import io.sentry.core.util.CollectionUtils;
import org.jetbrains.annotations.ApiStatus;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

/** Appender for logback in charge of sending the logged events to a Sentry server. */
public final class SentryAppender extends UnsynchronizedAppenderBase<ILoggingEvent> {
private @Nullable String dsn;
private @Nullable String environment;
private @Nullable Integer maxBreadcrumbs;
private @Nullable Integer shutdownTimeoutMillis;
private @Nullable Integer flushTimeoutMillis;
private @Nullable Integer readTimeoutMillis;
private @Nullable Double sampleRate;
private @Nullable Boolean bypassSecurity;
private @Nullable Boolean debug;
private @Nullable Boolean attachThreads;
private @Nullable Boolean attachStacktrace;
private @Nullable SentryOptions options;
private @Nullable ITransport transport;

@Override
public void start() {
if (dsn != null) {
Sentry.init(
options -> {
options.setDsn(dsn);
Optional.ofNullable(maxBreadcrumbs).ifPresent(options::setMaxBreadcrumbs);
Optional.ofNullable(environment).ifPresent(options::setEnvironment);
Optional.ofNullable(shutdownTimeoutMillis).ifPresent(options::setShutdownTimeout);
Optional.ofNullable(flushTimeoutMillis).ifPresent(options::setFlushTimeoutMillis);
Optional.ofNullable(readTimeoutMillis).ifPresent(options::setReadTimeoutMillis);
Optional.ofNullable(sampleRate).ifPresent(options::setSampleRate);
Optional.ofNullable(bypassSecurity).ifPresent(options::setBypassSecurity);
Optional.ofNullable(debug).ifPresent(options::setDebug);
Optional.ofNullable(attachThreads).ifPresent(options::setAttachThreads);
Optional.ofNullable(attachStacktrace).ifPresent(options::setAttachStacktrace);
options.setSentryClientName(BuildConfig.SENTRY_LOGBACK_SDK_NAME);
options.setSdkVersion(createSdkVersion(options));
Optional.ofNullable(transport).ifPresent(options::setTransport);
});
if (options != null && options.getDsn() != null) {
options.setSentryClientName(BuildConfig.SENTRY_LOGBACK_SDK_NAME);
options.setSdkVersion(createSdkVersion(options));
Optional.ofNullable(transport).ifPresent(options::setTransport);
Sentry.init(options);
}
super.start();
}
Expand All @@ -79,7 +54,8 @@ protected void append(@NotNull ILoggingEvent eventObject) {
*/
@SuppressWarnings("JdkObsolete")
final @NotNull SentryEvent createEvent(@NotNull ILoggingEvent loggingEvent) {
final SentryEvent event = new SentryEvent(DateUtils.getDateTime(new Date(loggingEvent.getTimeStamp())));
final SentryEvent event =
new SentryEvent(DateUtils.getDateTime(new Date(loggingEvent.getTimeStamp())));
final Message message = new Message();
message.setMessage(loggingEvent.getMessage());
message.setFormatted(loggingEvent.getFormattedMessage());
Expand All @@ -97,7 +73,8 @@ protected void append(@NotNull ILoggingEvent eventObject) {
event.setExtra("thread_name", loggingEvent.getThreadName());
}

final Map<String, String> mdcProperties = CollectionUtils.shallowCopy(loggingEvent.getMDCPropertyMap());
final Map<String, String> mdcProperties =
CollectionUtils.shallowCopy(loggingEvent.getMDCPropertyMap());
if (!mdcProperties.isEmpty()) {
event.getContexts().put("MDC", mdcProperties);
}
Expand Down Expand Up @@ -149,48 +126,8 @@ protected void append(@NotNull ILoggingEvent eventObject) {
return sdkVersion;
}

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

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

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

public void setShutdownTimeoutMillis(@Nullable Integer shutdownTimeoutMillis) {
this.shutdownTimeoutMillis = shutdownTimeoutMillis;
}

public void setFlushTimeoutMillis(@Nullable Integer flushTimeoutMillis) {
this.flushTimeoutMillis = flushTimeoutMillis;
}

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

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

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

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

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

public void setAttachStacktrace(@Nullable Boolean attachStacktrace) {
this.attachStacktrace = attachStacktrace;
public void setOptions(SentryOptions options) {
this.options = options;
}

@ApiStatus.Internal
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,22 +9,23 @@ import com.nhaarman.mockitokotlin2.verify
import com.nhaarman.mockitokotlin2.whenever
import io.sentry.core.SentryEvent
import io.sentry.core.SentryLevel
import io.sentry.core.SentryOptions
import io.sentry.core.transport.ITransport
import io.sentry.core.transport.TransportResult
import org.awaitility.kotlin.await
import kotlin.test.Test
import kotlin.test.assertEquals
import kotlin.test.assertNotNull
import org.slf4j.Logger
import org.slf4j.LoggerFactory
import org.slf4j.MDC
import java.time.Instant
import java.time.LocalDateTime
import java.time.ZoneId
import kotlin.test.AfterTest
import kotlin.test.BeforeTest
import kotlin.test.Test
import kotlin.test.assertEquals
import kotlin.test.assertFalse
import kotlin.test.assertNotNull
import kotlin.test.assertTrue
import org.awaitility.kotlin.await
import org.slf4j.Logger
import org.slf4j.LoggerFactory
import org.slf4j.MDC

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

val appender = SentryAppender()
appender.setDsn("http://key@localhost/proj")
val options = SentryOptions()
options.dsn = "http://key@localhost/proj"
appender.setOptions(options)
appender.context = loggerContext
appender.setTransport(transport)

Expand Down Expand Up @@ -204,8 +207,8 @@ class SentryAppenderTest {
assertEquals(BuildConfig.VERSION_NAME, it.sdk.version)
assertNotNull(it.sdk.packages)
assertTrue(it.sdk.packages!!.any { pkg ->
"maven:sentry-logback" == pkg.name
&& BuildConfig.VERSION_NAME == pkg.version
"maven:sentry-logback" == pkg.name &&
BuildConfig.VERSION_NAME == pkg.version
})
})
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,10 @@
</appender>

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

<!-- Optionally you can filter what statements are sent to Sentry independently from loggers configuration -->
<!-- <filter class="ch.qos.logback.classic.filter.ThresholdFilter">-->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,12 @@
import org.springframework.core.Ordered;

@Configuration
@ConditionalOnProperty(name = "sentry.enabled", havingValue = "true", matchIfMissing = true)
@ConditionalOnProperty(name = "sentry.dsn")
@Open
public class SentryAutoConfiguration {

/** Registers general purpose Sentry related beans. */
@Configuration
@ConditionalOnProperty("sentry.dsn")
@EnableConfigurationProperties(SentryProperties.class)
@Open
static class HubConfiguration {
Expand All @@ -55,34 +54,27 @@ static class HubConfiguration {
}

@Bean
public @NotNull SentryOptions sentryOptions(
public @NotNull IHub sentryHub(
final @NotNull Sentry.OptionsConfiguration<SentryOptions> optionsConfiguration,
final @NotNull SentryProperties properties,
final @NotNull SentryProperties options,
final @NotNull ObjectProvider<GitProperties> gitProperties) {
final SentryOptions options = new SentryOptions();
optionsConfiguration.configure(options);
gitProperties.ifAvailable(
git -> {
if (properties.isUseGitCommitIdAsRelease()) {
if (options.getRelease() == null && options.isUseGitCommitIdAsRelease()) {
options.setRelease(git.getCommitId());
}
});
properties.applyTo(options);

options.setSentryClientName(BuildConfig.SENTRY_SPRING_BOOT_SDK_NAME);
options.setSdkVersion(createSdkVersion(options));
return options;
}

@Bean
public @NotNull IHub sentryHub(final @NotNull SentryOptions sentryOptions) {
Sentry.init(sentryOptions);
Sentry.init(options);
return HubAdapter.getInstance();
}

/** Registers beans specific to Spring MVC. */
@Configuration
@ConditionalOnWebApplication(type = ConditionalOnWebApplication.Type.SERVLET)
@ConditionalOnProperty("sentry.dsn")
@Open
static class SentryWebMvcConfiguration {

Expand Down
Loading