Skip to content

4.x MDC propagation without context #8957

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

Merged
merged 2 commits into from
Jul 24, 2024
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
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2019, 2022 Oracle and/or its affiliates.
* Copyright (c) 2019, 2024 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 Down Expand Up @@ -123,41 +123,55 @@ protected <T> Collection<? extends Callable<T>> wrap(Collection<? extends Callab

}

@SuppressWarnings(value = "unchecked")
protected <T> Callable<T> wrap(Callable<T> task) {
Map<Class<?>, Object> properties = new HashMap<>();
PROVIDERS.forEach(provider -> properties.put(provider.getClass(), provider.data()));
Optional<Context> context = Contexts.context();
if (context.isPresent()) {
Map<Class<?>, Object> properties = new HashMap<>();
PROVIDERS.forEach(provider -> properties.put(provider.getClass(), provider.data()));
return () -> {
try {
PROVIDERS.forEach(provider -> provider.propagateData(properties.get(provider.getClass())));
return Contexts.runInContext(context.get(), task);
} finally {
PROVIDERS.forEach(provider -> provider.clearData(properties.get(provider.getClass())));
}
};
} else {
return task;
}
return context.<Callable<T>>map(value -> () -> {
try {
propagateMdcData(properties);
return Contexts.runInContext(value, task);
} finally {
clearMdcData(properties);
}
}).orElseGet(() -> () -> {
try {
propagateMdcData(properties);
return task.call();
} finally {
clearMdcData(properties);
}
});
}

@SuppressWarnings(value = "unchecked")
protected Runnable wrap(Runnable command) {
Optional<Context> context = Contexts.context();
if (context.isPresent()) {
Map<Class<?>, Object> properties = new HashMap<>();
PROVIDERS.forEach(provider -> properties.put(provider.getClass(), provider.data()));
return () -> {
try {
PROVIDERS.forEach(provider -> provider.propagateData(properties.get(provider.getClass())));
Contexts.runInContext(context.get(), command);
} finally {
PROVIDERS.forEach(provider -> provider.clearData(properties.get(provider.getClass())));
}
};
} else {
return command;
}
Map<Class<?>, Object> properties = new HashMap<>();
PROVIDERS.forEach(provider -> properties.put(provider.getClass(), provider.data()));
return context.<Runnable>map(value -> () -> {
try {
propagateMdcData(properties);
Contexts.runInContext(value, command);
} finally {
clearMdcData(properties);
}
}).orElseGet(() -> () -> {
try {
propagateMdcData(properties);
command.run();
} finally {
clearMdcData(properties);
}
});
}

@SuppressWarnings(value = "unchecked")
private static void propagateMdcData(Map<Class<?>, Object> properties) {
PROVIDERS.forEach(provider -> provider.propagateData(properties.get(provider.getClass())));
}

@SuppressWarnings(value = "unchecked")
private static void clearMdcData(Map<Class<?>, Object> properties) {
PROVIDERS.forEach(provider -> provider.clearData(properties.get(provider.getClass())));
}
}
17 changes: 15 additions & 2 deletions logging/jul/src/test/java/io/helidon/logging/jul/JulMdcTest.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2020, 2022 Oracle and/or its affiliates.
* Copyright (c) 2020, 2024 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 Down Expand Up @@ -82,7 +82,7 @@ private String logMessage(String message) {
}

@Test
public void testThreadPropagation() {
public void testThreadPropagationWithContext() {
HelidonMdc.set(TEST_KEY, TEST_VALUE);
Context context = Context.create();
ExecutorService executor = Contexts.wrap(Executors.newFixedThreadPool(1));
Expand All @@ -97,6 +97,19 @@ public void testThreadPropagation() {
});
}

@Test
public void testThreadPropagationWithoutContext() {
HelidonMdc.set(TEST_KEY, TEST_VALUE);
ExecutorService executor = Contexts.wrap(Executors.newFixedThreadPool(1));

try {
String value = executor.submit(new TestCallable()).get();
assertThat(value, is(TEST_VALUE));
} catch (Exception e) {
throw new ExecutorException("failed to execute", e);
}
}

private static final class TestCallable implements Callable<String> {

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2020 Oracle and/or its affiliates.
* Copyright (c) 2020, 2024 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 Down Expand Up @@ -51,7 +51,7 @@ public void testMdc() {
}

@Test
public void testThreadPropagation() {
public void testThreadPropagationWithContext() {
HelidonMdc.set(TEST_KEY, TEST_VALUE);
Context context = Context.create();
ExecutorService executor = Contexts.wrap(Executors.newFixedThreadPool(1));
Expand All @@ -66,6 +66,19 @@ public void testThreadPropagation() {
});
}

@Test
public void testThreadPropagationWithoutContext() {
HelidonMdc.set(TEST_KEY, TEST_VALUE);
ExecutorService executor = Contexts.wrap(Executors.newFixedThreadPool(1));

try {
String value = executor.submit(new TestCallable()).get();
assertThat(value, is(TEST_VALUE));
} catch (Exception e) {
throw new ExecutorException("failed to execute", e);
}
}

private static final class TestCallable implements Callable<String> {

@Override
Expand Down
17 changes: 15 additions & 2 deletions logging/slf4j/src/test/java/Slf4jMdcTest.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2020, 2021 Oracle and/or its affiliates.
* Copyright (c) 2020, 2024 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 Down Expand Up @@ -56,7 +56,7 @@ public void testMdc() {
}

@Test
public void testThreadPropagation() {
public void testThreadPropagationWithContext() {
HelidonMdc.set(TEST_KEY, TEST_VALUE);
Context context = Context.create();
ExecutorService executor = Contexts.wrap(Executors.newFixedThreadPool(1));
Expand All @@ -71,6 +71,19 @@ public void testThreadPropagation() {
});
}

@Test
public void testThreadPropagationWithoutContext() {
HelidonMdc.set(TEST_KEY, TEST_VALUE);
ExecutorService executor = Contexts.wrap(Executors.newFixedThreadPool(1));

try {
String value = executor.submit(new TestCallable()).get();
assertThat(value, is(TEST_VALUE));
} catch (Exception e) {
throw new ExecutorException("failed to execute", e);
}
}

@Test
public void testThreadPropagationWithEmptyMdc() {
Context context = Context.create();
Expand Down