15
15
*/
16
16
package org .springframework .modulith .observability ;
17
17
18
+ import io .micrometer .tracing .BaggageInScope ;
18
19
import io .micrometer .tracing .Tracer ;
19
20
import io .micrometer .tracing .Tracer .SpanInScope ;
20
21
@@ -31,6 +32,7 @@ class ModuleEntryInterceptor implements MethodInterceptor {
31
32
32
33
private static Logger LOGGER = LoggerFactory .getLogger (ModuleEntryInterceptor .class );
33
34
private static Map <String , ModuleEntryInterceptor > CACHE = new HashMap <>();
35
+ private static final String MODULE_KEY = ModuleTracingBeanPostProcessor .MODULE_BAGGAGE_KEY ;
34
36
35
37
private final ObservedModule module ;
36
38
private final Tracer tracer ;
@@ -66,14 +68,11 @@ public Object invoke(MethodInvocation invocation) throws Throwable {
66
68
67
69
var moduleName = module .getName ();
68
70
var currentSpan = tracer .currentSpan ();
71
+ var currentBaggage = tracer .getBaggage (MODULE_KEY );
72
+ var currentModule = currentBaggage != null ? currentBaggage .get () : null ;
69
73
70
- if (currentSpan != null ) {
71
-
72
- var currentBaggage = tracer .getBaggage (ModuleTracingBeanPostProcessor .MODULE_BAGGAGE_KEY );
73
-
74
- if (currentBaggage != null && moduleName .equals (currentBaggage .get ())) {
75
- return invocation .proceed ();
76
- }
74
+ if (currentSpan != null && moduleName .equals (currentModule )) {
75
+ return invocation .proceed ();
77
76
}
78
77
79
78
var invokedMethod = module .getInvokedMethod (invocation );
@@ -83,19 +82,17 @@ public Object invoke(MethodInvocation invocation) throws Throwable {
83
82
var span = tracer .spanBuilder ()
84
83
.name (moduleName )
85
84
.tag ("module.method" , invokedMethod )
86
- .tag (ModuleTracingBeanPostProcessor . MODULE_BAGGAGE_KEY , moduleName )
85
+ .tag (MODULE_KEY , moduleName )
87
86
.start ();
88
87
89
- tracer .createBaggage (ModuleTracingBeanPostProcessor .MODULE_BAGGAGE_KEY , moduleName );
90
-
91
- try (SpanInScope ws = tracer .withSpan (span )) {
88
+ try (SpanInScope ws = tracer .withSpan (span );
89
+ BaggageInScope baggage = tracer .createBaggageInScope (MODULE_KEY , moduleName )) {
92
90
93
91
return invocation .proceed ();
94
92
95
93
} finally {
96
94
97
95
LOGGER .trace ("Leaving {}" , module .getDisplayName ());
98
-
99
96
span .end ();
100
97
}
101
98
}
0 commit comments