Skip to content

Commit 95fa9d8

Browse files
committed
Add prototypical metric about the event listener execution.
1 parent 4cfa454 commit 95fa9d8

File tree

1 file changed

+49
-1
lines changed

1 file changed

+49
-1
lines changed

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

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

18+
import io.micrometer.core.instrument.Counter;
1819
import io.micrometer.observation.Observation;
1920
import io.micrometer.observation.Observation.Scope;
2021

@@ -111,7 +112,54 @@ public Object invoke(MethodInvocation invocation) throws Throwable {
111112
var observation = Observation.createNotStarted(convention, DEFAULT,
112113
() -> modulithContext, observationRegistry);
113114

114-
return new ObservingInvocationAdapter(observation).invoke(invocation);
115+
MethodInterceptor adapter = new ObservingInvocationAdapter(observation);
116+
117+
if (context.hasMeterRegistry() && module.isEventListenerInvocation(invocation)) {
118+
adapter = new EventCountingInvocationAdapter(adapter);
119+
}
120+
121+
return adapter.invoke(invocation);
122+
}
123+
124+
private class EventCountingInvocationAdapter implements MethodInterceptor {
125+
126+
private final MethodInterceptor delegate;
127+
128+
EventCountingInvocationAdapter(MethodInterceptor delegate) {
129+
this.delegate = delegate;
130+
}
131+
132+
/*
133+
* (non-Javadoc)
134+
* @see org.aopalliance.intercept.MethodInterceptor#invoke(org.aopalliance.intercept.MethodInvocation)
135+
*/
136+
@Override
137+
public Object invoke(MethodInvocation invocation) throws Throwable {
138+
139+
var builder = Counter.builder("module.listener.invocation")
140+
.tag("method", invocation.getMethod().getName())
141+
.tag("type", invocation.getMethod().getDeclaringClass().getName())
142+
.tag("module", module.getDisplayName());
143+
144+
try {
145+
146+
var result = delegate.invoke(invocation);
147+
148+
builder.tag("success", "true");
149+
150+
return result;
151+
152+
} catch (Exception o_O) {
153+
154+
builder.tag("success", "false");
155+
156+
throw o_O;
157+
158+
} finally {
159+
160+
builder.register(context.getMeterRegistry()).increment();
161+
}
162+
}
115163
}
116164

117165
private class ObservingInvocationAdapter implements MethodInterceptor {

0 commit comments

Comments
 (0)