Class TimedAspect

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

@NonNullApi @Incubating(since="1.0.0") public class TimedAspect extends Object

AspectJ aspect for intercepting types or methods annotated with @Timed.
The aspect supports programmatic customizations through constructor-injectable custom logic.

You might want to add tags programmatically to the Timer.
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 Timer.

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

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

 private boolean skipControllers(ProceedingJoinPoint pjp) {
     Class<?> targetClass = pjp.getTarget().getClass();
     return targetClass.isAnnotationPresent(RestController.class) || targetClass.isAnnotationPresent(Controller.class);
 }
 
To add support for MeterTag annotations set the MeterTagAnnotationHandler via setMeterTagAnnotationHandler(MeterTagAnnotationHandler).
Since:
1.0.0
  • Field Details

  • Constructor Details

    • TimedAspect

      public TimedAspect()
      Creates a TimedAspect instance with Metrics.globalRegistry.
      Since:
      1.2.0
    • TimedAspect

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

      public TimedAspect(MeterRegistry registry, Function<org.aspectj.lang.ProceedingJoinPoint,Iterable<Tag>> tagsBasedOnJoinPoint)
      Creates a TimedAspect 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.
    • TimedAspect

      public TimedAspect(MeterRegistry registry, Predicate<org.aspectj.lang.ProceedingJoinPoint> shouldSkip)
      Creates a TimedAspect 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
    • TimedAspect

      public TimedAspect(MeterRegistry registry, Function<org.aspectj.lang.ProceedingJoinPoint,Iterable<Tag>> tagsBasedOnJoinPoint, Predicate<org.aspectj.lang.ProceedingJoinPoint> shouldSkip)
      Creates a TimedAspect 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

    • timedClass

      @Nullable public Object timedClass(org.aspectj.lang.ProceedingJoinPoint pjp) throws Throwable
      Throws:
      Throwable
    • timedMethod

      @Nullable public Object timedMethod(org.aspectj.lang.ProceedingJoinPoint pjp) throws Throwable
      Throws:
      Throwable
    • setMeterTagAnnotationHandler

      public void setMeterTagAnnotationHandler(MeterTagAnnotationHandler meterTagAnnotationHandler)
      Setting this enables support for MeterTag.
      Parameters:
      meterTagAnnotationHandler - meter tag annotation handler