Class TimedAspect

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

@NonNullApi
@Incubating(since="1.0.0")
public class TimedAspect
extends java.lang.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);
 }
 
Since:
1.0.0
  • Field Summary

    Fields
    Modifier and Type Field Description
    static java.lang.String DEFAULT_EXCEPTION_TAG_VALUE  
    static java.lang.String DEFAULT_METRIC_NAME  
    static java.lang.String EXCEPTION_TAG
    Tag key for an exception.
  • Constructor Summary

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

    Modifier and Type Method Description
    java.lang.Object timedMethod​(org.aspectj.lang.ProceedingJoinPoint pjp)  

    Methods inherited from class java.lang.Object

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

    • DEFAULT_METRIC_NAME

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

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

      public static final java.lang.String EXCEPTION_TAG
      Tag key for an exception.
      Since:
      1.1.0
      See Also:
      Constant Field Values
  • 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, java.util.function.Function<org.aspectj.lang.ProceedingJoinPoint,​java.lang.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, java.util.function.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, java.util.function.Function<org.aspectj.lang.ProceedingJoinPoint,​java.lang.Iterable<Tag>> tagsBasedOnJoinPoint, java.util.function.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

    • timedMethod

      public java.lang.Object timedMethod​(org.aspectj.lang.ProceedingJoinPoint pjp) throws java.lang.Throwable
      Throws:
      java.lang.Throwable