Skip to content

Commit

Permalink
Add metrics to all method calls withing a class when class is annotat…
Browse files Browse the repository at this point in the history
…ed with @timed
  • Loading branch information
mohkar authored and shakuzen committed Mar 4, 2022
1 parent 96ba184 commit 9839e17
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,15 @@ public TimedAspect(MeterRegistry registry, Function<ProceedingJoinPoint, Iterabl
this.shouldSkip = shouldSkip;
}

@Around("@within(io.micrometer.core.annotation.Timed)")
public Object timedClass(ProceedingJoinPoint pjp) throws Throwable {
Method method = ((MethodSignature) pjp.getSignature()).getMethod();
Class<?> declaringClass = method.getDeclaringClass();
Timed timed = declaringClass.getAnnotation(Timed.class);

return perform(pjp, timed, method);
}

@Around("execution (@io.micrometer.core.annotation.Timed * *.*(..))")
public Object timedMethod(ProceedingJoinPoint pjp) throws Throwable {
if (shouldSkip.test(pjp)) {
Expand All @@ -163,6 +172,10 @@ public Object timedMethod(ProceedingJoinPoint pjp) throws Throwable {
timed = method.getAnnotation(Timed.class);
}

return perform(pjp, timed, method);
}

private Object perform(ProceedingJoinPoint pjp, Timed timed, Method method) throws Throwable {
final String metricName = timed.value().isEmpty() ? DEFAULT_METRIC_NAME : timed.value();
final boolean stopWhenCompleted = CompletionStage.class.isAssignableFrom(method.getReturnType());

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ void timeMethodWithLongTaskTimer() {
.tag("extra", "tag")
.longTaskTimers().size()).isEqualTo(1);
}

@Test
void timeMethodFailure() {
MeterRegistry failingRegistry = new FailingMeterRegistry();
Expand Down Expand Up @@ -289,6 +289,44 @@ void timeMethodFailureWithLongTaskTimerWhenCompleted() {
});
}

@Test
void timeClass() {
MeterRegistry registry = new SimpleMeterRegistry();

AspectJProxyFactory pf = new AspectJProxyFactory(new TimedClass());
pf.addAspect(new TimedAspect(registry));

TimedClass service = pf.getProxy();

service.call();

assertThat(registry.get("call")
.tag("class", "io.micrometer.core.aop.TimedAspectTest$TimedClass")
.tag("method", "call")
.tag("extra", "tag")
.timer().count()).isEqualTo(1);
}

@Test
void timeClassFailure() {
MeterRegistry failingRegistry = new FailingMeterRegistry();

AspectJProxyFactory pf = new AspectJProxyFactory(new TimedClass());
pf.addAspect(new TimedAspect(failingRegistry));

TimedClass service = pf.getProxy();

service.call();

assertThatExceptionOfType(MeterNotFoundException.class).isThrownBy(() -> {
failingRegistry.get("call")
.tag("class", "io.micrometer.core.aop.TimedAspectTest$TimedClass")
.tag("method", "call")
.tag("extra", "tag")
.timer();
});
}

private final class FailingMeterRegistry extends SimpleMeterRegistry {
private FailingMeterRegistry() {
super();
Expand Down Expand Up @@ -363,4 +401,10 @@ synchronized void complete(RuntimeException withException) {
}

}

@Timed(value = "call", extraTags = {"extra", "tag"})
static class TimedClass {
void call() {
}
}
}

0 comments on commit 9839e17

Please sign in to comment.