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 Tag
s 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
-
Constructor Summary
ConstructorDescriptionCreates aTimedAspect
instance withMetrics.globalRegistry
.TimedAspect
(MeterRegistry registry) Creates aTimedAspect
instance with the givenregistry
.TimedAspect
(MeterRegistry registry, Function<org.aspectj.lang.ProceedingJoinPoint, Iterable<Tag>> tagsBasedOnJoinPoint) Creates aTimedAspect
instance with the givenregistry
and tags provider function.TimedAspect
(MeterRegistry registry, Function<org.aspectj.lang.ProceedingJoinPoint, Iterable<Tag>> tagsBasedOnJoinPoint, Predicate<org.aspectj.lang.ProceedingJoinPoint> shouldSkip) Creates aTimedAspect
instance with the givenregistry
, tags provider function and skip predicate.TimedAspect
(MeterRegistry registry, Predicate<org.aspectj.lang.ProceedingJoinPoint> shouldSkip) Creates aTimedAspect
instance with the givenregistry
and skip predicate. -
Method Summary
Modifier and TypeMethodDescriptiontimedClass
(org.aspectj.lang.ProceedingJoinPoint pjp) timedMethod
(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 aTimedAspect
instance withMetrics.globalRegistry
.- Since:
- 1.2.0
-
TimedAspect
Creates aTimedAspect
instance 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 aTimedAspect
instance with the givenregistry
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 aTimedAspect
instance with the givenregistry
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 aTimedAspect
instance 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