Class CountedAspect

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

@NonNullApi
public class CountedAspect
extends java.lang.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:
Counted
  • Field Summary

    Fields
    Modifier and Type Field Description
    java.lang.String DEFAULT_EXCEPTION_TAG_VALUE  
    java.lang.String RESULT_TAG_FAILURE_VALUE  
    java.lang.String RESULT_TAG_SUCCESS_VALUE  
  • Constructor Summary

    Constructors
    Constructor Description
    CountedAspect()
    Creates a CountedAspect instance with Metrics.globalRegistry.
    CountedAspect​(MeterRegistry registry)
    Creates a CountedAspect instance with the given registry.
    CountedAspect​(MeterRegistry registry, java.util.function.Function<org.aspectj.lang.ProceedingJoinPoint,​java.lang.Iterable<Tag>> tagsBasedOnJoinPoint)
    Creates a CountedAspect instance with the given registry and tags provider function.
    CountedAspect​(MeterRegistry registry, java.util.function.Function<org.aspectj.lang.ProceedingJoinPoint,​java.lang.Iterable<Tag>> tagsBasedOnJoinPoint, java.util.function.Predicate<org.aspectj.lang.ProceedingJoinPoint> shouldSkip)
    Creates a CountedAspect instance with the given registry, tags provider function and skip predicate.
    CountedAspect​(MeterRegistry registry, java.util.function.Predicate<org.aspectj.lang.ProceedingJoinPoint> shouldSkip)
    Creates a CountedAspect instance with the given registry and skip predicate.
  • Method Summary

    Modifier and Type Method Description
    java.lang.Object interceptAndRecord​(org.aspectj.lang.ProceedingJoinPoint pjp, Counted counted)
    Intercept methods annotated with the Counted annotation and expose a few counters about their execution status.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Field Details

    • DEFAULT_EXCEPTION_TAG_VALUE

      public final java.lang.String DEFAULT_EXCEPTION_TAG_VALUE
      See Also:
      Constant Field Values
    • RESULT_TAG_FAILURE_VALUE

      public final java.lang.String RESULT_TAG_FAILURE_VALUE
      See Also:
      Constant Field Values
    • RESULT_TAG_SUCCESS_VALUE

      public final java.lang.String RESULT_TAG_SUCCESS_VALUE
      See Also:
      Constant Field Values
  • 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, java.util.function.Function<org.aspectj.lang.ProceedingJoinPoint,​java.lang.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, java.util.function.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, java.util.function.Function<org.aspectj.lang.ProceedingJoinPoint,​java.lang.Iterable<Tag>> tagsBasedOnJoinPoint, java.util.function.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 java.lang.Object interceptAndRecord​(org.aspectj.lang.ProceedingJoinPoint pjp, Counted counted) throws java.lang.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:
      java.lang.Throwable - When the intercepted method throws one.