Skip to content

Commit ad17a8f

Browse files
licanbrian-brazil
authored andcommitted
fix spring_web aop no such method found exception (#376)
Signed-off-by: candyleer <[email protected]>
1 parent 5e33fe6 commit ad17a8f

File tree

2 files changed

+35
-4
lines changed

2 files changed

+35
-4
lines changed

simpleclient_spring_web/src/main/java/io/prometheus/client/spring/web/MethodTimer.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,10 @@
88
import org.aspectj.lang.reflect.MethodSignature;
99
import org.springframework.context.annotation.Scope;
1010
import org.springframework.core.annotation.AnnotationUtils;
11+
import org.springframework.util.ReflectionUtils;
1112
import org.springframework.web.bind.annotation.ControllerAdvice;
1213

14+
import java.lang.reflect.Method;
1315
import java.util.HashMap;
1416
import java.util.concurrent.locks.Lock;
1517
import java.util.concurrent.locks.ReadWriteLock;
@@ -46,8 +48,8 @@ private PrometheusTimeMethod getAnnotation(ProceedingJoinPoint pjp) throws NoSuc
4648
// When target is an AOP interface proxy but annotation is on class method (rather than Interface method).
4749
final String name = signature.getName();
4850
final Class[] parameterTypes = signature.getParameterTypes();
49-
50-
return AnnotationUtils.findAnnotation(pjp.getTarget().getClass().getDeclaredMethod(name, parameterTypes), PrometheusTimeMethod.class);
51+
Method method = ReflectionUtils.findMethod(pjp.getTarget().getClass(), name, parameterTypes);
52+
return AnnotationUtils.findAnnotation(method, PrometheusTimeMethod.class);
5153
}
5254

5355
private Summary ensureSummary(ProceedingJoinPoint pjp, String key) throws IllegalStateException {

simpleclient_spring_web/src/test/java/io/prometheus/client/spring/web/MethodTimerTest.java

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,40 @@ public void timeMe() throws Exception {
2424

2525
}
2626

27+
private class TestCglibClassParent implements Timeable{
28+
@PrometheusTimeMethod(name = "test_sub_class", help = "help one sub")
29+
public void timeMe() throws Exception {
30+
Thread.sleep(20);
31+
}
32+
}
33+
34+
/**
35+
* mock cglib proxy by subclass and in this class don't contain timMe() method
36+
*/
37+
private final class MockCglibProxyTestClass extends TestCglibClassParent {
38+
39+
}
40+
2741
private interface Time2 {
2842
void timeMe() throws Exception;
2943
void aSecondMethod() throws Exception;
3044
}
3145

46+
@Test
47+
public void timeMethodInSubClassModel() throws Exception {
48+
Timeable cprime = new MockCglibProxyTestClass();
49+
AspectJProxyFactory factory = new AspectJProxyFactory(cprime);
50+
factory.addAspect(MethodTimer.class);
51+
Timeable proxy = factory.getProxy();
52+
53+
proxy.timeMe();
54+
55+
final Double tot = CollectorRegistry.defaultRegistry.getSampleValue("test_sub_class_sum");
56+
Assert.assertNotNull(tot);
57+
assertEquals(0.02, tot, 0.01);
58+
}
59+
60+
3261
@Test
3362
public void timeMethod() throws Exception {
3463
Timeable cprime = new TestClass();
@@ -187,7 +216,7 @@ private interface SameMethodNameTest {
187216
@Test
188217
public void testOverloadedMethodName() throws Exception {
189218
final int sleep1 = 100, sleep2 = 200;
190-
219+
191220
SameMethodNameTest r = getProxy(new SameMethodNameTest() {
192221
@Override
193222
@PrometheusTimeMethod(name="dosomething_one_test_seconds", help = "halp")
@@ -201,7 +230,7 @@ public void doSomething(String s) throws Exception {
201230
Thread.sleep(sleep2);
202231
}
203232
});
204-
233+
205234
r.doSomething();
206235
r.doSomething("foobar");
207236

0 commit comments

Comments
 (0)