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

Simplify configuring Logback integration when environment variable with the DSN is not set. #1271

Merged
merged 6 commits into from
Feb 22, 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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
* Fix: Make the ANR Atomic flags immutable
* Enchancement: Integration interface better compatibility with Kotlin null-safety
* Enchancement: Simplify Sentry configuration in Spring integration (#1259)
* Enchancement: Simplify configuring Logback integration when environment variable with the DSN is not set (#1271)
* Fix: Prevent NoOpHub from creating heavy SentryOptions objects (#1272)
* Enchancement: Add Request to the Scope. #1270
* Fix: Fix SentryTransaction#getStatus NPE (#1273)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,14 +35,20 @@ public final class SentryAppender extends UnsynchronizedAppenderBase<ILoggingEve
@Override
public void start() {
if (!Sentry.isEnabled()) {
options.setEnableExternalConfiguration(true);
options.setSentryClientName(BuildConfig.SENTRY_LOGBACK_SDK_NAME);
options.setSdkVersion(createSdkVersion(options));
Optional.ofNullable(transportFactory).ifPresent(options::setTransportFactory);
try {
Sentry.init(options);
} catch (IllegalArgumentException e) {
addWarn("Failed to init Sentry during appender initialization: " + e.getMessage());
if (options.getDsn() != null && !options.getDsn().endsWith("_IS_UNDEFINED")) {
options.setEnableExternalConfiguration(true);
options.setSentryClientName(BuildConfig.SENTRY_LOGBACK_SDK_NAME);
options.setSdkVersion(createSdkVersion(options));
Optional.ofNullable(transportFactory).ifPresent(options::setTransportFactory);
try {
Sentry.init(options);
} catch (IllegalArgumentException e) {
addWarn("Failed to init Sentry during appender initialization: " + e.getMessage());
}
} else {
options
.getLogger()
.log(SentryLevel.WARNING, "DSN is null. SentryAppender is not being initialized");
}
}
super.start();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package io.sentry.logback

import ch.qos.logback.classic.Level
import ch.qos.logback.classic.LoggerContext
import ch.qos.logback.core.status.Status
import com.nhaarman.mockitokotlin2.any
import com.nhaarman.mockitokotlin2.anyOrNull
import com.nhaarman.mockitokotlin2.mock
Expand Down Expand Up @@ -29,7 +30,7 @@ import org.slf4j.LoggerFactory
import org.slf4j.MDC

class SentryAppenderTest {
private class Fixture(minimumBreadcrumbLevel: Level? = null, minimumEventLevel: Level? = null) {
private class Fixture(dsn: String? = "http://key@localhost/proj", minimumBreadcrumbLevel: Level? = null, minimumEventLevel: Level? = null) {
val logger: Logger = LoggerFactory.getLogger(SentryAppenderTest::class.java)
val loggerContext = LoggerFactory.getILoggerFactory() as LoggerContext
val transportFactory = mock<ITransportFactory>()
Expand All @@ -40,7 +41,7 @@ class SentryAppenderTest {
whenever(this.transportFactory.create(any(), any())).thenReturn(transport)
val appender = SentryAppender()
val options = SentryOptions()
options.dsn = "http://key@localhost/proj"
options.dsn = dsn
appender.setOptions(options)
appender.setMinimumBreadcrumbLevel(minimumBreadcrumbLevel)
appender.setMinimumEventLevel(minimumEventLevel)
Expand Down Expand Up @@ -305,4 +306,12 @@ class SentryAppenderTest {
}, anyOrNull())
}
}

@Test
fun `does not initialize Sentry when environment variable with DSN is not set`() {
// environment variables referenced in the logback.xml that are not set in the OS, have value "ENV_NAME_IS_UNDEFINED"
fixture = Fixture(dsn = "DSN_IS_UNDEFINED", minimumEventLevel = Level.DEBUG)

assertTrue(fixture.loggerContext.statusManager.copyOfStatusList.none { it.level == Status.WARN })
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

<appender name="sentry" class="io.sentry.logback.SentryAppender">
<options>
<debug>true</debug>
<!-- 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]/5428563</dsn>
</options>
Expand Down