Skip to content

Commit 9d8b774

Browse files
authored
Fix: Avoid NPE when MDC contains null values (getsentry#1385)
1 parent a7ab809 commit 9d8b774

3 files changed

Lines changed: 25 additions & 6 deletions

File tree

CHANGELOG.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
# Unreleased
22

33
* Feat: Add option to ignore exceptions by type (#1352)
4-
* Fix: Fix NPE when MDC contains null values (#1364)
4+
* Fix: Fix NPE when MDC contains null values (sentry-logback) (#1364)
5+
* Fix: Avoid NPE when MDC contains null values (sentry-jul) (#1385)
56
* Feat: Sentry closes Android NDK and ShutdownHook integrations (#1358)
67
* Enhancement: Allow inheritance of SentryHandler class in sentry-jul package(#1367)
78
* Fix: Accept only non null value maps (#1368)

sentry-jul/src/main/java/io/sentry/jul/SentryHandler.java

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
import io.sentry.SentryOptions;
99
import io.sentry.protocol.Message;
1010
import io.sentry.protocol.SdkVersion;
11-
import io.sentry.util.CollectionUtils;
1211
import java.text.MessageFormat;
1312
import java.util.ArrayList;
1413
import java.util.Date;
@@ -20,6 +19,7 @@
2019
import java.util.logging.Level;
2120
import java.util.logging.LogManager;
2221
import java.util.logging.LogRecord;
22+
import java.util.stream.Collectors;
2323
import org.jetbrains.annotations.NotNull;
2424
import org.jetbrains.annotations.Nullable;
2525
import org.jetbrains.annotations.TestOnly;
@@ -185,10 +185,15 @@ SentryEvent createEvent(final @NotNull LogRecord record) {
185185
if (throwable != null) {
186186
event.setThrowable(throwable);
187187
}
188-
final Map<String, String> mdcProperties =
189-
CollectionUtils.shallowCopy(MDC.getMDCAdapter().getCopyOfContextMap());
190-
if (mdcProperties != null && !mdcProperties.isEmpty()) {
191-
event.getContexts().put("MDC", mdcProperties);
188+
Map<String, String> mdcProperties = MDC.getMDCAdapter().getCopyOfContextMap();
189+
if (mdcProperties != null) {
190+
mdcProperties =
191+
mdcProperties.entrySet().stream()
192+
.filter(it -> it.getValue() != null)
193+
.collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue));
194+
if (!mdcProperties.isEmpty()) {
195+
event.getContexts().put("MDC", mdcProperties);
196+
}
192197
}
193198
event.setExtra(THREAD_ID, record.getThreadID());
194199
return event;

sentry-jul/src/test/kotlin/io/sentry/jul/SentryHandlerTest.kt

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -287,6 +287,19 @@ class SentryHandlerTest {
287287
}
288288
}
289289

290+
@Test
291+
fun `ignore set tags with null values from MDC`() {
292+
fixture = Fixture(minimumEventLevel = Level.WARNING)
293+
MDC.put("key", null)
294+
fixture.logger.warning("testing MDC tags")
295+
296+
await.untilAsserted {
297+
verify(fixture.transport).send(checkEvent { event ->
298+
assertFalse(event.contexts.containsKey("MDC"))
299+
}, anyOrNull())
300+
}
301+
}
302+
290303
@Test
291304
fun `does not create MDC context when no MDC tags are set`() {
292305
fixture = Fixture(minimumEventLevel = Level.WARNING)

0 commit comments

Comments
 (0)