Search in sources :

Example 6 with Timed

use of io.micrometer.core.annotation.Timed in project micrometer by micrometer-metrics.

the class ScheduledMethodMetrics method timeScheduledOperation.

@Around("execution (@org.springframework.scheduling.annotation.Scheduled  * *.*(..))")
public Object timeScheduledOperation(ProceedingJoinPoint pjp) throws Throwable {
    Method method = ((MethodSignature) pjp.getSignature()).getMethod();
    String signature = pjp.getSignature().toShortString();
    if (method.getDeclaringClass().isInterface()) {
        try {
            method = pjp.getTarget().getClass().getDeclaredMethod(pjp.getSignature().getName(), method.getParameterTypes());
        } catch (final SecurityException | NoSuchMethodException e) {
            logger.warn("Unable to perform metrics timing on " + signature, e);
            return pjp.proceed();
        }
    }
    Timer shortTaskTimer = null;
    LongTaskTimer longTaskTimer = null;
    for (Timed timed : TimedUtils.findTimedAnnotations(method)) {
        if (timed.longTask())
            longTaskTimer = LongTaskTimer.builder(timed.value()).tags(timed.extraTags()).description("Timer of @Scheduled long task").register(registry);
        else {
            Timer.Builder timerBuilder = Timer.builder(timed.value()).tags(timed.extraTags()).description("Timer of @Scheduled task");
            if (timed.percentiles().length > 0) {
                timerBuilder = timerBuilder.publishPercentiles(timed.percentiles());
            }
            shortTaskTimer = timerBuilder.register(registry);
        }
    }
    if (shortTaskTimer != null && longTaskTimer != null) {
        final Timer finalTimer = shortTaskTimer;
        // noinspection NullableProblems
        return recordThrowable(longTaskTimer, () -> recordThrowable(finalTimer, pjp::proceed));
    } else if (shortTaskTimer != null) {
        // noinspection NullableProblems
        return recordThrowable(shortTaskTimer, pjp::proceed);
    } else if (longTaskTimer != null) {
        // noinspection NullableProblems
        return recordThrowable(longTaskTimer, pjp::proceed);
    }
    return pjp.proceed();
}
Also used : MethodSignature(org.aspectj.lang.reflect.MethodSignature) LongTaskTimer(io.micrometer.core.instrument.LongTaskTimer) Timer(io.micrometer.core.instrument.Timer) Timed(io.micrometer.core.annotation.Timed) Method(java.lang.reflect.Method) LongTaskTimer(io.micrometer.core.instrument.LongTaskTimer) Around(org.aspectj.lang.annotation.Around)

Example 7 with Timed

use of io.micrometer.core.annotation.Timed in project micrometer by micrometer-metrics.

the class TimedUtils method findTimedAnnotations.

public static Set<Timed> findTimedAnnotations(AnnotatedElement element) {
    Timed t = AnnotationUtils.findAnnotation(element, Timed.class);
    // noinspection ConstantConditions
    if (t != null)
        return Collections.singleton(t);
    TimedSet ts = AnnotationUtils.findAnnotation(element, TimedSet.class);
    if (ts != null) {
        return Arrays.stream(ts.value()).collect(Collectors.toSet());
    }
    return Collections.emptySet();
}
Also used : TimedSet(io.micrometer.core.annotation.TimedSet) Timed(io.micrometer.core.annotation.Timed)

Aggregations

Timed (io.micrometer.core.annotation.Timed)7 Timer (io.micrometer.core.instrument.Timer)3 TimedSet (io.micrometer.core.annotation.TimedSet)2 LongTaskTimer (io.micrometer.core.instrument.LongTaskTimer)2 Method (java.lang.reflect.Method)2 Around (org.aspectj.lang.annotation.Around)2 MethodSignature (org.aspectj.lang.reflect.MethodSignature)2 QueryParams (com.ecwid.consul.v1.QueryParams)1 List (java.util.List)1 NotFoundException (javax.ws.rs.NotFoundException)1 ContainerRequest (org.glassfish.jersey.server.ContainerRequest)1 ResourceMethod (org.glassfish.jersey.server.model.ResourceMethod)1 RefreshEvent (org.springframework.cloud.endpoint.event.RefreshEvent)1 Scheduled (org.springframework.scheduling.annotation.Scheduled)1