Class CountedAspect

java.lang.Object
io.micrometer.core.aop.CountedAspect

@NonNullApi public class CountedAspect extends Object

Aspect responsible for intercepting all methods annotated with the @Counted annotation and recording a few counter metrics about their execution status.
The aspect supports programmatic customizations through constructor-injectable custom logic.

You might want to add tags programmatically to the Counter.
In this case, the tags provider function (Function<ProceedingJoinPoint, Iterable<Tag>>) can help. It receives a ProceedingJoinPoint and returns the Tags that will be attached to the Counter.

You might also want to skip the Counter creation programmatically.
One use-case can be having another component in your application that already processes the @Counted annotation in some cases so that CountedAspect should not intercept these methods. By using the skip predicate (Predicate<ProceedingJoinPoint>) you can tell the CountedAspect when not to create a Counter. Here's a theoretic example to disable Counter creation for Spring controllers:

 @Bean
 public CountedAspect countedAspect(MeterRegistry meterRegistry) {
     return new CountedAspect(meterRegistry, this::skipControllers);
 }

 private boolean skipControllers(ProceedingJoinPoint pjp) {
     Class<?> targetClass = pjp.getTarget().getClass();
     return targetClass.isAnnotationPresent(RestController.class) || targetClass.isAnnotationPresent(Controller.class);
 }
 
Since:
1.2.0
See Also:
  • Field Details

  • Constructor Details

    • CountedAspect

      public CountedAspect()
      Creates a CountedAspect instance with Metrics.globalRegistry.
      Since:
      1.7.0
    • CountedAspect

      public CountedAspect(MeterRegistry registry)
      Creates a CountedAspect instance with the given registry.
      Parameters:
      registry - Where we're going to register metrics.
    • CountedAspect

      public CountedAspect(MeterRegistry registry, Function<org.aspectj.lang.ProceedingJoinPoint,Iterable<Tag>> tagsBasedOnJoinPoint)
      Creates a CountedAspect instance with the given registry and tags provider function.
      Parameters:
      registry - Where we're going to register metrics.
      tagsBasedOnJoinPoint - A function to generate tags given a join point.
    • CountedAspect

      public CountedAspect(MeterRegistry registry, Predicate<org.aspectj.lang.ProceedingJoinPoint> shouldSkip)
      Creates a CountedAspect instance with the given registry and skip predicate.
      Parameters:
      registry - Where we're going to register metrics.
      shouldSkip - A predicate to decide if creating the timer should be skipped or not.
      Since:
      1.7.0
    • CountedAspect

      public CountedAspect(MeterRegistry registry, Function<org.aspectj.lang.ProceedingJoinPoint,Iterable<Tag>> tagsBasedOnJoinPoint, Predicate<org.aspectj.lang.ProceedingJoinPoint> shouldSkip)
      Creates a CountedAspect instance with the given registry, tags provider function and skip predicate.
      Parameters:
      registry - Where we're going to register metrics.
      tagsBasedOnJoinPoint - A function to generate tags given a join point.
      shouldSkip - A predicate to decide if creating the timer should be skipped or not.
      Since:
      1.7.0
  • Method Details

    • interceptAndRecord

      public Object interceptAndRecord(org.aspectj.lang.ProceedingJoinPoint pjp, Counted counted) throws Throwable
      Intercept methods annotated with the Counted annotation and expose a few counters about their execution status. By default, this aspect records both failed and successful attempts. If the Counted.recordFailuresOnly() is set to true, then the aspect would record only failed attempts. In case of a failure, the aspect tags the counter with the simple name of the thrown exception.

      When the annotated method returns a CompletionStage or any of its subclasses, the counters will be incremented only when the CompletionStage is completed. If completed exceptionally a failure is recorded, otherwise if Counted.recordFailuresOnly() is set to false, a success is recorded.

      Parameters:
      pjp - Encapsulates some information about the intercepted area.
      counted - The annotation.
      Returns:
      Whatever the intercepted method returns.
      Throws:
      Throwable - When the intercepted method throws one.