Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support @Counted on classes #4758

Merged
merged 1 commit into from
Feb 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
* @see io.micrometer.core.aop.CountedAspect
*/
@Inherited
@Target(ElementType.METHOD)
@Target({ ElementType.ANNOTATION_TYPE, ElementType.TYPE, ElementType.METHOD })
@Retention(RetentionPolicy.RUNTIME)
public @interface Counted {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
package io.micrometer.core.aop;

import io.micrometer.common.lang.NonNullApi;
import io.micrometer.common.lang.Nullable;
import io.micrometer.core.annotation.Counted;
import io.micrometer.core.instrument.*;
import org.aspectj.lang.ProceedingJoinPoint;
Expand Down Expand Up @@ -67,6 +68,7 @@
*
* @author Ali Dehghani
* @author Jonatan Ivanov
* @author Johnny Lim
* @since 1.2.0
* @see Counted
*/
Expand Down Expand Up @@ -164,6 +166,23 @@ public CountedAspect(MeterRegistry registry, Function<ProceedingJoinPoint, Itera
this.shouldSkip = shouldSkip;
}

@Around("@within(io.micrometer.core.annotation.Counted)")
@Nullable
public Object countedClass(ProceedingJoinPoint pjp) throws Throwable {
if (shouldSkip.test(pjp)) {
return pjp.proceed();
}

Method method = ((MethodSignature) pjp.getSignature()).getMethod();
Class<?> declaringClass = method.getDeclaringClass();
if (!declaringClass.isAnnotationPresent(Counted.class)) {
declaringClass = pjp.getTarget().getClass();
}
Counted counted = declaringClass.getAnnotation(Counted.class);

return perform(pjp, counted);
}

/**
* Intercept methods annotated with the {@link Counted} annotation and expose a few
* counters about their execution status. By default, this aspect records both failed
Expand All @@ -188,6 +207,10 @@ public Object interceptAndRecord(ProceedingJoinPoint pjp, Counted counted) throw
return pjp.proceed();
}

return perform(pjp, counted);
}

private Object perform(ProceedingJoinPoint pjp, Counted counted) throws Throwable {
final Method method = ((MethodSignature) pjp.getSignature()).getMethod();
final boolean stopWhenCompleted = CompletionStage.class.isAssignableFrom(method.getReturnType());

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@
* Unit tests for the {@link CountedAspect} aspect.
*
* @author Ali Dehghani
* @author Tommy Ludwig
* @author Johnny Lim
*/
class CountedAspectTest {

Expand Down Expand Up @@ -291,4 +293,51 @@ synchronized void complete(RuntimeException withException) {

}

@Test
void countClassWithSuccess() {
CountedClassService service = getAdvisedService(new CountedClassService());

service.hello();

assertThat(meterRegistry.get("class.counted")
.tag("class", "io.micrometer.core.aop.CountedAspectTest$CountedClassService")
.tag("method", "hello")
.tag("result", "success")
.tag("exception", "none")
.counter()
.count()).isEqualTo(1);
}

@Test
void countClassWithFailure() {
CountedClassService service = getAdvisedService(new CountedClassService());

assertThatThrownBy(() -> service.fail()).isInstanceOf(RuntimeException.class);

meterRegistry.forEachMeter((m) -> {
System.out.println(m.getId().getTags());
});

assertThat(meterRegistry.get("class.counted")
.tag("class", "io.micrometer.core.aop.CountedAspectTest$CountedClassService")
.tag("method", "fail")
.tag("result", "failure")
.tag("exception", "RuntimeException")
.counter()
.count()).isEqualTo(1);
}

@Counted("class.counted")
static class CountedClassService {

String hello() {
return "hello";
}

void fail() {
throw new RuntimeException("Oops");
}

}

}