|
15 | 15 | */ |
16 | 16 | package org.springframework.modulith.observability.support; |
17 | 17 |
|
| 18 | +import io.micrometer.core.instrument.Counter; |
18 | 19 | import io.micrometer.observation.Observation; |
19 | 20 | import io.micrometer.observation.Observation.Scope; |
20 | 21 |
|
@@ -111,7 +112,54 @@ public Object invoke(MethodInvocation invocation) throws Throwable { |
111 | 112 | var observation = Observation.createNotStarted(convention, DEFAULT, |
112 | 113 | () -> modulithContext, observationRegistry); |
113 | 114 |
|
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 | + } |
115 | 163 | } |
116 | 164 |
|
117 | 165 | private class ObservingInvocationAdapter implements MethodInterceptor { |
|
0 commit comments