Commit 9a29080 1 parent d12c583 commit 9a29080 Copy full SHA for 9a29080
File tree 2 files changed +31
-10
lines changed
main/java/io/micronaut/tracing/instrument/util
test/groovy/io/micronaut/tracing/instrument/util
2 files changed +31
-10
lines changed Original file line number Diff line number Diff line change 20
20
import io .micronaut .scheduling .instrument .InvocationInstrumenter ;
21
21
import io .micronaut .scheduling .instrument .InvocationInstrumenterFactory ;
22
22
import io .micronaut .scheduling .instrument .ReactiveInvocationInstrumenterFactory ;
23
- import org .slf4j .Logger ;
24
- import org .slf4j .LoggerFactory ;
25
23
import org .slf4j .MDC ;
26
24
27
25
import javax .inject .Singleton ;
39
37
@ Internal
40
38
public final class MdcInstrumenter implements InvocationInstrumenterFactory , ReactiveInvocationInstrumenterFactory {
41
39
42
- private static final Logger LOG = LoggerFactory .getLogger (MdcInstrumenter .class );
43
-
44
-
45
40
/**
46
41
* Creates optional invocation instrumenter.
47
42
*
@@ -57,10 +52,7 @@ public InvocationInstrumenter newInvocationInstrumenter() {
57
52
@ Override
58
53
public void beforeInvocation () {
59
54
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 ()) {
64
56
MDC .setContextMap (contextMap );
65
57
}
66
58
}
@@ -70,7 +62,6 @@ public void afterInvocation(boolean cleanup) {
70
62
if (oldContextMap != null && !oldContextMap .isEmpty ()) {
71
63
MDC .setContextMap (oldContextMap );
72
64
} else {
73
- LOG .debug ("{} clears MDC" , this );
74
65
MDC .clear ();
75
66
}
76
67
}
Original file line number Diff line number Diff line change @@ -112,6 +112,36 @@ class MdcInstrumenterSpec extends Specification {
112
112
MDC . get(key) == value2
113
113
}
114
114
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
+
115
145
void " test MDC instrumenter with Executor" () {
116
146
117
147
given :
You can’t perform that action at this time.
0 commit comments