Skip to content

Commit 919d565

Browse files
committed
GH-612 - Properly nest spans for nested module invocations.
1 parent e29853f commit 919d565

File tree

1 file changed

+9
-12
lines changed

1 file changed

+9
-12
lines changed

spring-modulith-observability/src/main/java/org/springframework/modulith/observability/ModuleEntryInterceptor.java

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
*/
1616
package org.springframework.modulith.observability;
1717

18+
import io.micrometer.tracing.BaggageInScope;
1819
import io.micrometer.tracing.Tracer;
1920
import io.micrometer.tracing.Tracer.SpanInScope;
2021

@@ -31,6 +32,7 @@ class ModuleEntryInterceptor implements MethodInterceptor {
3132

3233
private static Logger LOGGER = LoggerFactory.getLogger(ModuleEntryInterceptor.class);
3334
private static Map<String, ModuleEntryInterceptor> CACHE = new HashMap<>();
35+
private static final String MODULE_KEY = ModuleTracingBeanPostProcessor.MODULE_BAGGAGE_KEY;
3436

3537
private final ObservedModule module;
3638
private final Tracer tracer;
@@ -66,14 +68,11 @@ public Object invoke(MethodInvocation invocation) throws Throwable {
6668

6769
var moduleName = module.getName();
6870
var currentSpan = tracer.currentSpan();
71+
var currentBaggage = tracer.getBaggage(MODULE_KEY);
72+
var currentModule = currentBaggage != null ? currentBaggage.get() : null;
6973

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();
7776
}
7877

7978
var invokedMethod = module.getInvokedMethod(invocation);
@@ -83,19 +82,17 @@ public Object invoke(MethodInvocation invocation) throws Throwable {
8382
var span = tracer.spanBuilder()
8483
.name(moduleName)
8584
.tag("module.method", invokedMethod)
86-
.tag(ModuleTracingBeanPostProcessor.MODULE_BAGGAGE_KEY, moduleName)
85+
.tag(MODULE_KEY, moduleName)
8786
.start();
8887

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)) {
9290

9391
return invocation.proceed();
9492

9593
} finally {
9694

9795
LOGGER.trace("Leaving {}", module.getDisplayName());
98-
9996
span.end();
10097
}
10198
}

0 commit comments

Comments
 (0)