Class TimedAspect
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 Summary
Fields -
Constructor Summary
ConstructorsConstructorDescriptionCreates aTimedAspectinstance withMetrics.globalRegistry.TimedAspect(MeterRegistry registry) Creates aTimedAspectinstance with the givenregistry.TimedAspect(MeterRegistry registry, Function<org.aspectj.lang.ProceedingJoinPoint, Iterable<Tag>> tagsBasedOnJoinPoint) Creates aTimedAspectinstance with the givenregistryand tags provider function.TimedAspect(MeterRegistry registry, Function<org.aspectj.lang.ProceedingJoinPoint, Iterable<Tag>> tagsBasedOnJoinPoint, Predicate<org.aspectj.lang.ProceedingJoinPoint> shouldSkip) Creates aTimedAspectinstance with the givenregistry, tags provider function and skip predicate.TimedAspect(MeterRegistry registry, Predicate<org.aspectj.lang.ProceedingJoinPoint> shouldSkip) Creates aTimedAspectinstance with the givenregistryand skip predicate. -
Method Summary
Modifier and TypeMethodDescriptionvoidsetMeterTagAnnotationHandler(MeterTagAnnotationHandler meterTagAnnotationHandler) Setting this enables support forMeterTag.@Nullable ObjecttimedClass(org.aspectj.lang.ProceedingJoinPoint pjp) @Nullable ObjecttimedMethod(org.aspectj.lang.ProceedingJoinPoint pjp)
-
Field Details
-
DEFAULT_METRIC_NAME
- See Also:
-
DEFAULT_EXCEPTION_TAG_VALUE
- See Also:
-
EXCEPTION_TAG
Tag key for an exception.- Since:
- 1.1.0
- See Also:
-
-
Constructor Details
-
TimedAspect
public TimedAspect()Creates aTimedAspectinstance withMetrics.globalRegistry.- Since:
- 1.2.0
-
TimedAspect
Creates aTimedAspectinstance with the givenregistry.- Parameters:
registry- Where we're going to register metrics.
-
TimedAspect
public TimedAspect(MeterRegistry registry, Function<org.aspectj.lang.ProceedingJoinPoint, Iterable<Tag>> tagsBasedOnJoinPoint) Creates aTimedAspectinstance with the givenregistryand 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 aTimedAspectinstance with the givenregistryand 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 aTimedAspectinstance with the givenregistry, 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