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

Slf4j MDC context propagation with null MDC map fixed #2861

Merged
merged 3 commits into from Mar 18, 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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
@@ -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.
Expand All @@ -16,6 +16,7 @@
package io.helidon.logging.slf4j;

import java.util.Map;
import java.util.Optional;

import io.helidon.common.context.spi.DataPropagationProvider;

Expand All @@ -29,7 +30,7 @@ public class Slf4jMdcPropagator implements DataPropagationProvider<Map<String, S

@Override
public Map<String, String> data() {
return MDC.getCopyOfContextMap();
return Optional.ofNullable(MDC.getCopyOfContextMap()).orElseGet(Map::of);
}

@Override
Expand Down
31 changes: 30 additions & 1 deletion 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.
Expand All @@ -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;

Expand All @@ -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);
Expand Down Expand Up @@ -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<String> {

@Override
Expand All @@ -73,4 +94,12 @@ public String call() {
}
}

private static final class TestEmptyMdc implements Callable<Boolean> {

@Override
public Boolean call() {
return MDC.getCopyOfContextMap().isEmpty();
}
}

}