From 3754d28367b0b3fa70026e1e16409d048309ceeb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?David=20Kr=C3=A1l?= Date: Thu, 18 Mar 2021 16:40:32 +0100 Subject: [PATCH] Slf4j MDC context propagation with null MDC map fixed (#2861) Slf4j MDC context propagation with null MDC map fixed Signed-off-by: David Kral --- .../logging/slf4j/Slf4jMdcPropagator.java | 5 +-- logging/slf4j/src/test/java/Slf4jMdcTest.java | 31 ++++++++++++++++++- 2 files changed, 33 insertions(+), 3 deletions(-) diff --git a/logging/slf4j/src/main/java/io/helidon/logging/slf4j/Slf4jMdcPropagator.java b/logging/slf4j/src/main/java/io/helidon/logging/slf4j/Slf4jMdcPropagator.java index 28e5f0dfef9..acb841f156f 100644 --- a/logging/slf4j/src/main/java/io/helidon/logging/slf4j/Slf4jMdcPropagator.java +++ b/logging/slf4j/src/main/java/io/helidon/logging/slf4j/Slf4jMdcPropagator.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2020 Oracle and/or its affiliates. + * Copyright (c) 2020, 2021 Oracle and/or its affiliates. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -16,6 +16,7 @@ package io.helidon.logging.slf4j; import java.util.Map; +import java.util.Optional; import io.helidon.common.context.spi.DataPropagationProvider; @@ -29,7 +30,7 @@ public class Slf4jMdcPropagator implements DataPropagationProvider data() { - return MDC.getCopyOfContextMap(); + return Optional.ofNullable(MDC.getCopyOfContextMap()).orElseGet(Map::of); } @Override diff --git a/logging/slf4j/src/test/java/Slf4jMdcTest.java b/logging/slf4j/src/test/java/Slf4jMdcTest.java index f5e0d243fa2..25454a7e37f 100644 --- a/logging/slf4j/src/test/java/Slf4jMdcTest.java +++ b/logging/slf4j/src/test/java/Slf4jMdcTest.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2020 Oracle and/or its affiliates. + * Copyright (c) 2020, 2021 Oracle and/or its affiliates. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -23,6 +23,7 @@ import io.helidon.common.context.ExecutorException; import io.helidon.logging.common.HelidonMdc; +import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.Test; import org.slf4j.MDC; @@ -38,6 +39,11 @@ public class Slf4jMdcTest { private static final String TEST_KEY = "test"; private static final String TEST_VALUE = "value"; + @AfterEach + public void clearMdc() { + HelidonMdc.clear(); + } + @Test public void testMdc() { HelidonMdc.set(TEST_KEY, TEST_VALUE); @@ -65,6 +71,21 @@ public void testThreadPropagation() { }); } + @Test + public void testThreadPropagationWithEmptyMdc() { + Context context = Context.create(); + ExecutorService executor = Contexts.wrap(Executors.newFixedThreadPool(1)); + + Contexts.runInContext(context, () -> { + try { + Boolean value = executor.submit(new TestEmptyMdc()).get(); + assertThat(value, is(true)); + } catch (Exception e) { + throw new ExecutorException("failed to execute", e); + } + }); + } + private static final class TestCallable implements Callable { @Override @@ -73,4 +94,12 @@ public String call() { } } + private static final class TestEmptyMdc implements Callable { + + @Override + public Boolean call() { + return MDC.getCopyOfContextMap().isEmpty(); + } + } + }