Class CountedAspect
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 Tag
s 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:
-
Field Summary
-
Constructor Summary
ConstructorDescriptionCreates aCountedAspect
instance withMetrics.globalRegistry
.CountedAspect
(MeterRegistry registry) Creates aCountedAspect
instance with the givenregistry
.CountedAspect
(MeterRegistry registry, Function<org.aspectj.lang.ProceedingJoinPoint, Iterable<Tag>> tagsBasedOnJoinPoint) Creates aCountedAspect
instance with the givenregistry
and tags provider function.CountedAspect
(MeterRegistry registry, Function<org.aspectj.lang.ProceedingJoinPoint, Iterable<Tag>> tagsBasedOnJoinPoint, Predicate<org.aspectj.lang.ProceedingJoinPoint> shouldSkip) Creates aCountedAspect
instance with the givenregistry
, tags provider function and skip predicate.CountedAspect
(MeterRegistry registry, Predicate<org.aspectj.lang.ProceedingJoinPoint> shouldSkip) Creates aCountedAspect
instance with the givenregistry
and skip predicate. -
Method Summary
Modifier and TypeMethodDescriptioninterceptAndRecord
(org.aspectj.lang.ProceedingJoinPoint pjp, Counted counted) Intercept methods annotated with theCounted
annotation and expose a few counters about their execution status.
-
Field Details
-
DEFAULT_EXCEPTION_TAG_VALUE
- See Also:
-
RESULT_TAG_FAILURE_VALUE
- See Also:
-
RESULT_TAG_SUCCESS_VALUE
- See Also:
-
-
Constructor Details
-
CountedAspect
public CountedAspect()Creates aCountedAspect
instance withMetrics.globalRegistry
.- Since:
- 1.7.0
-
CountedAspect
Creates aCountedAspect
instance with the givenregistry
.- Parameters:
registry
- Where we're going to register metrics.
-
CountedAspect
public CountedAspect(MeterRegistry registry, Function<org.aspectj.lang.ProceedingJoinPoint, Iterable<Tag>> tagsBasedOnJoinPoint) Creates aCountedAspect
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.
-
CountedAspect
public CountedAspect(MeterRegistry registry, Predicate<org.aspectj.lang.ProceedingJoinPoint> shouldSkip) Creates aCountedAspect
instance with the givenregistry
and skip predicate.- Parameters:
registry
- Where we're going to register metrics.shouldSkip
- A predicate to decide if creating the counter should be skipped or not.- Since:
- 1.7.0
-
CountedAspect
public CountedAspect(MeterRegistry registry, Function<org.aspectj.lang.ProceedingJoinPoint, Iterable<Tag>> tagsBasedOnJoinPoint, Predicate<org.aspectj.lang.ProceedingJoinPoint> shouldSkip) Creates aCountedAspect
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 counter should be skipped or not.- Since:
- 1.7.0
-
-
Method Details
-
interceptAndRecord
@Nullable public Object interceptAndRecord(org.aspectj.lang.ProceedingJoinPoint pjp, Counted counted) throws Throwable Intercept methods annotated with theCounted
annotation and expose a few counters about their execution status. By default, this aspect records both failed and successful attempts. If theCounted.recordFailuresOnly()
is set totrue
, 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 theCompletionStage
is completed. If completed exceptionally a failure is recorded, otherwise ifCounted.recordFailuresOnly()
is set tofalse
, a success is recorded.- Parameters:
pjp
- Encapsulates some information about the intercepted area.counted
- The annotation.- Returns:
- Whatever the intercepted method returns.
- Throws:
Throwable
- When the intercepted method throws one.
-