Search in sources :

Example 1 with Timed

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

the class TimedAspect method timedMethod.

@Around("execution (@io.micrometer.core.annotation.Timed * *.*(..))")
public Object timedMethod(ProceedingJoinPoint pjp) throws Throwable {
    Method method = ((MethodSignature) pjp.getSignature()).getMethod();
    Timed timed = method.getAnnotation(Timed.class);
    final String metricName = timed.value().isEmpty() ? DEFAULT_METRIC_NAME : timed.value();
    Timer.Sample sample = Timer.start(registry);
    try {
        return pjp.proceed();
    } finally {
        sample.stop(Timer.builder(metricName).description(timed.description().isEmpty() ? null : timed.description()).tags(timed.extraTags()).tags(tagsBasedOnJoinpoint.apply(pjp)).publishPercentileHistogram(timed.histogram()).publishPercentiles(timed.percentiles().length == 0 ? null : timed.percentiles()).register(registry));
    }
}
Also used : MethodSignature(org.aspectj.lang.reflect.MethodSignature) Timer(io.micrometer.core.instrument.Timer) Timed(io.micrometer.core.annotation.Timed) Method(java.lang.reflect.Method) Around(org.aspectj.lang.annotation.Around)

Example 2 with Timed

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

the class MetricsRequestEventListener method annotations.

private Set<Timed> annotations(RequestEvent event) {
    final Set<Timed> timed = new HashSet<>();
    final ResourceMethod matchingResourceMethod = event.getUriInfo().getMatchedResourceMethod();
    if (matchingResourceMethod != null) {
        // collect on method level
        timed.addAll(timedFinder.findTimedAnnotations(matchingResourceMethod.getInvocable().getHandlingMethod()));
        // fallback on class level
        if (timed.isEmpty()) {
            timed.addAll(timedFinder.findTimedAnnotations(matchingResourceMethod.getInvocable().getHandlingMethod().getDeclaringClass()));
        }
    }
    return timed;
}
Also used : Timed(io.micrometer.core.annotation.Timed) ResourceMethod(org.glassfish.jersey.server.model.ResourceMethod)

Example 3 with Timed

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

the class MetricsRequestEventListener method onEvent.

@Override
public void onEvent(RequestEvent event) {
    ContainerRequest containerRequest = event.getContainerRequest();
    Set<Timed> timedAnnotations;
    switch(event.getType()) {
        case ON_EXCEPTION:
            if (!(event.getException() instanceof NotFoundException)) {
                break;
            }
        case REQUEST_MATCHED:
            timedAnnotations = annotations(event);
            timedAnnotationsOnRequest.put(containerRequest, timedAnnotations);
            shortTaskSample.put(containerRequest, Timer.start(registry));
            List<LongTaskTimer.Sample> longTaskSamples = longTaskTimers(timedAnnotations, event).stream().map(LongTaskTimer::start).collect(Collectors.toList());
            if (!longTaskSamples.isEmpty()) {
                this.longTaskSamples.put(containerRequest, longTaskSamples);
            }
            break;
        case FINISHED:
            timedAnnotations = timedAnnotationsOnRequest.remove(containerRequest);
            Timer.Sample shortSample = shortTaskSample.remove(containerRequest);
            if (shortSample != null) {
                for (Timer timer : shortTimers(timedAnnotations, event)) {
                    shortSample.stop(timer);
                }
            }
            Collection<LongTaskTimer.Sample> longSamples = this.longTaskSamples.remove(containerRequest);
            if (longSamples != null) {
                for (LongTaskTimer.Sample longSample : longSamples) {
                    longSample.stop();
                }
            }
            break;
    }
}
Also used : LongTaskTimer(io.micrometer.core.instrument.LongTaskTimer) Timer(io.micrometer.core.instrument.Timer) Timed(io.micrometer.core.annotation.Timed) NotFoundException(javax.ws.rs.NotFoundException) ContainerRequest(org.glassfish.jersey.server.ContainerRequest) LongTaskTimer(io.micrometer.core.instrument.LongTaskTimer)

Example 4 with Timed

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

the class TimedFinder method findTimedAnnotations.

Set<Timed> findTimedAnnotations(AnnotatedElement element) {
    Timed t = annotationFinder.findAnnotation(element, Timed.class);
    if (t != null)
        return Collections.singleton(t);
    TimedSet ts = annotationFinder.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)

Example 5 with Timed

use of io.micrometer.core.annotation.Timed in project spring-cloud-consul by spring-cloud.

the class ConfigWatch method watchConfigKeyValues.

@Scheduled(fixedDelayString = "${spring.cloud.consul.config.watch.delay:1000}")
@Timed(value = "consul.watch-config-keys")
public void watchConfigKeyValues() {
    if (this.running.get()) {
        for (String context : this.consulIndexes.keySet()) {
            // turn the context into a Consul folder path (unless our config format are FILES)
            if (properties.getFormat() != FILES && !context.endsWith("/")) {
                context = context + "/";
            }
            try {
                Long currentIndex = this.consulIndexes.get(context);
                if (currentIndex == null) {
                    currentIndex = -1L;
                }
                // use the consul ACL token if found
                String aclToken = properties.getAclToken();
                if (StringUtils.isEmpty(aclToken)) {
                    aclToken = null;
                }
                Response<List<GetValue>> response = this.consul.getKVValues(context, aclToken, new QueryParams(this.properties.getWatch().getWaitTime(), currentIndex));
                // reducing churn if there wasn't anything
                if (response.getValue() != null && !response.getValue().isEmpty()) {
                    Long newIndex = response.getConsulIndex();
                    if (newIndex != null && !newIndex.equals(currentIndex)) {
                        // don't publish the same index again, don't publish the first time (-1) so index can be primed
                        if (!this.consulIndexes.containsValue(newIndex) && !currentIndex.equals(-1L)) {
                            RefreshEventData data = new RefreshEventData(context, currentIndex, newIndex);
                            this.publisher.publishEvent(new RefreshEvent(this, data, data.toString()));
                        }
                        this.consulIndexes.put(context, newIndex);
                    }
                }
            } catch (Exception e) {
                // only fail fast on the initial query, otherwise just log the error
                if (firstTime && this.properties.isFailFast()) {
                    log.error("Fail fast is set and there was an error reading configuration from consul.");
                    ReflectionUtils.rethrowRuntimeException(e);
                } else if (log.isTraceEnabled()) {
                    log.trace("Error querying consul Key/Values for context '" + context + "'", e);
                } else if (log.isWarnEnabled()) {
                    // simplified one line log message in the event of an agent failure
                    log.warn("Error querying consul Key/Values for context '" + context + "'. Message: " + e.getMessage());
                }
            }
        }
    }
    firstTime = false;
}
Also used : RefreshEvent(org.springframework.cloud.endpoint.event.RefreshEvent) List(java.util.List) QueryParams(com.ecwid.consul.v1.QueryParams) Scheduled(org.springframework.scheduling.annotation.Scheduled) 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