From 3b6509e355c48a0b2bebc235dd02281554b7ee4e Mon Sep 17 00:00:00 2001 From: schulzp Date: Wed, 16 Sep 2020 07:26:32 +0200 Subject: [PATCH] refactoring: extracted timer registration --- .../interceptors/AsyncTimedInterceptor.java | 109 ++++++++---------- 1 file changed, 49 insertions(+), 60 deletions(-) diff --git a/src/main/java/com/traum/metrics/interceptors/AsyncTimedInterceptor.java b/src/main/java/com/traum/metrics/interceptors/AsyncTimedInterceptor.java index dc0eb3d..f66597f 100644 --- a/src/main/java/com/traum/metrics/interceptors/AsyncTimedInterceptor.java +++ b/src/main/java/com/traum/metrics/interceptors/AsyncTimedInterceptor.java @@ -20,7 +20,6 @@ import javax.interceptor.Interceptor; import javax.interceptor.InvocationContext; import org.eclipse.microprofile.metrics.Metadata; -import org.eclipse.microprofile.metrics.MetricID; import org.eclipse.microprofile.metrics.MetricRegistry; import org.eclipse.microprofile.metrics.MetricType; import org.eclipse.microprofile.metrics.SimpleTimer; @@ -34,6 +33,8 @@ public class AsyncTimedInterceptor { private final MetricRegistry registry; + private final Map cache = new HashMap<>(); + @Inject AsyncTimedInterceptor(MetricRegistry registry) { this.registry = registry; @@ -41,72 +42,60 @@ public class AsyncTimedInterceptor { @AroundInvoke Object meteredMethod(InvocationContext context) throws Exception { - return this.timedCallable(context, context.getMethod()); + return this.meterTime(context, context.getMethod()); } - private Map cache = new HashMap<>(); - - private Object timedCallable( + private Object meterTime( InvocationContext context, E element) throws Exception { - final MetricID metricID = - cache.computeIfAbsent( - element.getName(), - key -> { - Of resolvedAnnotation = AsyncTimedOf.of(element); - - final Metadata metadata = - Metadata.builder() - .withType(MetricType.SIMPLE_TIMER) - .withName(resolvedAnnotation.metricName()) - .reusable(resolvedAnnotation.metricAnnotation().reusable()) - .withOptionalDisplayName(resolvedAnnotation.metricAnnotation().displayName()) - .withOptionalDescription(resolvedAnnotation.metricAnnotation().description()) - .withOptionalUnit(resolvedAnnotation.metricAnnotation().unit()) - .build(); - - registry.simpleTimer(metadata, resolvedAnnotation.tags()); - - return new MetricID(metadata.getName(), resolvedAnnotation.tags()); - }); - - final SimpleTimer timer = (SimpleTimer) registry.getMetrics().get(metricID); - - if (timer == null) { - throw new IllegalStateException( - "No simple timer with metricID [" - + metricID - + "] found in registry [" - + this.registry - + "]"); - } else { - Context time = timer.time(); - try { - if (context.getMethod().getReturnType().isAssignableFrom(CompletionStage.class)) { - return ((CompletionStage) context.proceed()) - .whenComplete( - (result, error) -> { - time.stop(); - }); - } - if (context.getMethod().getReturnType().isAssignableFrom(Uni.class)) { - return ((Uni) context.proceed()) - .onItem() - .invoke(result -> time.stop()) - .onFailure() - .invoke(error -> time.stop()); - } - throw new IllegalArgumentException( - "Unsupported return type " - + context.getMethod().getReturnType() - + " from " - + context.getMethod()); - } catch (Throwable t) { - time.stop(); - throw t; + final SimpleTimer timer = registerTimer(element); + + Context time = timer.time(); + try { + if (context.getMethod().getReturnType().isAssignableFrom(CompletionStage.class)) { + return ((CompletionStage) context.proceed()) + .whenComplete( + (result, error) -> { + time.stop(); + }); } + if (context.getMethod().getReturnType().isAssignableFrom(Uni.class)) { + return ((Uni) context.proceed()) + .onItem() + .invoke(result -> time.stop()) + .onFailure() + .invoke(error -> time.stop()); + } + throw new IllegalArgumentException( + "Unsupported return type " + + context.getMethod().getReturnType() + + " from " + + context.getMethod()); + } catch (Throwable t) { + time.stop(); + throw t; } } + + private SimpleTimer registerTimer(E element) { + return cache.computeIfAbsent( + element.getName(), + key -> { + Of resolvedAnnotation = AsyncTimedOf.of(element); + + final Metadata metadata = + Metadata.builder() + .withType(MetricType.SIMPLE_TIMER) + .withName(resolvedAnnotation.metricName()) + .reusable(resolvedAnnotation.metricAnnotation().reusable()) + .withOptionalDisplayName(resolvedAnnotation.metricAnnotation().displayName()) + .withOptionalDescription(resolvedAnnotation.metricAnnotation().description()) + .withOptionalUnit(resolvedAnnotation.metricAnnotation().unit()) + .build(); + + return registry.simpleTimer(metadata, resolvedAnnotation.tags()); + }); + } } class AsyncTimedOf implements Of {