Skip to content

Commit 9a29080

Browse files
authored
Fix MDC context cleanup (micronaut-projects#3328)
1 parent d12c583 commit 9a29080

File tree

2 files changed

+31
-10
lines changed

2 files changed

+31
-10
lines changed

tracing/src/main/java/io/micronaut/tracing/instrument/util/MdcInstrumenter.java

+1-10
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,6 @@
2020
import io.micronaut.scheduling.instrument.InvocationInstrumenter;
2121
import io.micronaut.scheduling.instrument.InvocationInstrumenterFactory;
2222
import io.micronaut.scheduling.instrument.ReactiveInvocationInstrumenterFactory;
23-
import org.slf4j.Logger;
24-
import org.slf4j.LoggerFactory;
2523
import org.slf4j.MDC;
2624

2725
import javax.inject.Singleton;
@@ -39,9 +37,6 @@
3937
@Internal
4038
public final class MdcInstrumenter implements InvocationInstrumenterFactory, ReactiveInvocationInstrumenterFactory {
4139

42-
private static final Logger LOG = LoggerFactory.getLogger(MdcInstrumenter.class);
43-
44-
4540
/**
4641
* Creates optional invocation instrumenter.
4742
*
@@ -57,10 +52,7 @@ public InvocationInstrumenter newInvocationInstrumenter() {
5752
@Override
5853
public void beforeInvocation() {
5954
oldContextMap = MDC.getCopyOfContextMap();
60-
if (oldContextMap == null || oldContextMap.isEmpty()) {
61-
LOG.debug("{} pushes data to MDC: {}", this, contextMap);
62-
}
63-
if (contextMap != null) {
55+
if (contextMap != null && !contextMap.isEmpty()) {
6456
MDC.setContextMap(contextMap);
6557
}
6658
}
@@ -70,7 +62,6 @@ public void afterInvocation(boolean cleanup) {
7062
if (oldContextMap != null && !oldContextMap.isEmpty()) {
7163
MDC.setContextMap(oldContextMap);
7264
} else {
73-
LOG.debug("{} clears MDC", this);
7465
MDC.clear();
7566
}
7667
}

tracing/src/test/groovy/io/micronaut/tracing/instrument/util/MdcInstrumenterSpec.groovy

+30
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,36 @@ class MdcInstrumenterSpec extends Specification {
112112
MDC.get(key) == value2
113113
}
114114

115+
def "empty context should't clean MDC"() {
116+
when:
117+
MDC.put(key, value)
118+
MDC.remove(key) // Make empty context
119+
def mdcBeforeNew = MDC.copyOfContextMap
120+
def instrumenterWithEmptyContext = mdcInstrumenter.newInvocationInstrumenter()
121+
MDC.put("contextValue", "contextValue")
122+
def instrumenterWithContext = mdcInstrumenter.newInvocationInstrumenter()
123+
MDC.clear()
124+
Runnable runnable = InvocationInstrumenter.instrument({
125+
MDC.put("inside1", "inside1")
126+
InvocationInstrumenter.instrument({
127+
assert MDC.get("contextValue") == "contextValue"
128+
assert MDC.get("inside1") == "inside1"
129+
130+
MDC.put("inside2", "inside2")
131+
} as Runnable, instrumenterWithEmptyContext).run()
132+
assert MDC.get("contextValue") == "contextValue"
133+
assert MDC.get("inside1") == "inside1"
134+
assert MDC.get("inside2") == null
135+
} as Runnable, instrumenterWithContext)
136+
runnable.run()
137+
138+
then:
139+
mdcBeforeNew.isEmpty()
140+
MDC.get(key) == null
141+
MDC.get("XXX") == null
142+
MDC.get("aaa") == null
143+
}
144+
115145
void "test MDC instrumenter with Executor"() {
116146

117147
given:

0 commit comments

Comments
 (0)