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

Fix: Avoid NPE when MDC contains null values #1385

Merged
merged 9 commits into from
Apr 9, 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
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
# Unreleased

* Feat: Add option to ignore exceptions by type (#1352)
* Fix: Fix NPE when MDC contains null values (#1364)
* Fix: Fix NPE when MDC contains null values (sentry-logback) (#1364)
* Fix: Avoid NPE when MDC contains null values (sentry-jul) (#1385)
* Feat: Sentry closes Android NDK and ShutdownHook integrations (#1358)
* Enhancement: Allow inheritance of SentryHandler class in sentry-jul package(#1367)
* Fix: Accept only non null value maps (#1368)
Expand Down
15 changes: 10 additions & 5 deletions sentry-jul/src/main/java/io/sentry/jul/SentryHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
import io.sentry.SentryOptions;
import io.sentry.protocol.Message;
import io.sentry.protocol.SdkVersion;
import io.sentry.util.CollectionUtils;
import java.text.MessageFormat;
import java.util.ArrayList;
import java.util.Date;
Expand All @@ -20,6 +19,7 @@
import java.util.logging.Level;
import java.util.logging.LogManager;
import java.util.logging.LogRecord;
import java.util.stream.Collectors;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.annotations.TestOnly;
Expand Down Expand Up @@ -185,10 +185,15 @@ SentryEvent createEvent(final @NotNull LogRecord record) {
if (throwable != null) {
event.setThrowable(throwable);
}
final Map<String, String> mdcProperties =
CollectionUtils.shallowCopy(MDC.getMDCAdapter().getCopyOfContextMap());
if (mdcProperties != null && !mdcProperties.isEmpty()) {
event.getContexts().put("MDC", mdcProperties);
Map<String, String> mdcProperties = MDC.getMDCAdapter().getCopyOfContextMap();
if (mdcProperties != null) {
mdcProperties =
mdcProperties.entrySet().stream()
.filter(it -> it.getValue() != null)
.collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue));
if (!mdcProperties.isEmpty()) {
event.getContexts().put("MDC", mdcProperties);
}
}
event.setExtra(THREAD_ID, record.getThreadID());
return event;
Expand Down
13 changes: 13 additions & 0 deletions sentry-jul/src/test/kotlin/io/sentry/jul/SentryHandlerTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -287,6 +287,19 @@ class SentryHandlerTest {
}
}

@Test
fun `ignore set tags with null values from MDC`() {
fixture = Fixture(minimumEventLevel = Level.WARNING)
MDC.put("key", null)
fixture.logger.warning("testing MDC tags")

await.untilAsserted {
verify(fixture.transport).send(checkEvent { event ->
assertFalse(event.contexts.containsKey("MDC"))
}, anyOrNull())
}
}

@Test
fun `does not create MDC context when no MDC tags are set`() {
fixture = Fixture(minimumEventLevel = Level.WARNING)
Expand Down