From 1fcaa31de243858dc06d2eca82b07d0d3b314c0e Mon Sep 17 00:00:00 2001 From: Urban Malc Date: Tue, 29 Jan 2019 15:36:11 +0100 Subject: [PATCH] Fixed non reusable metric bug --- .../RegisterMetricsInterceptor.java | 22 +++++++++++++++---- .../ee/metrics/utils/AnnotationMetadata.java | 14 ++++++++++++ 2 files changed, 32 insertions(+), 4 deletions(-) diff --git a/core/src/main/java/com/kumuluz/ee/metrics/interceptors/RegisterMetricsInterceptor.java b/core/src/main/java/com/kumuluz/ee/metrics/interceptors/RegisterMetricsInterceptor.java index a294abc..c9c59e5 100644 --- a/core/src/main/java/com/kumuluz/ee/metrics/interceptors/RegisterMetricsInterceptor.java +++ b/core/src/main/java/com/kumuluz/ee/metrics/interceptors/RegisterMetricsInterceptor.java @@ -20,6 +20,9 @@ */ package com.kumuluz.ee.metrics.interceptors; +import com.kumuluz.ee.metrics.api.CounterImpl; +import com.kumuluz.ee.metrics.api.MeterImpl; +import com.kumuluz.ee.metrics.api.TimerImpl; import com.kumuluz.ee.metrics.interceptors.utils.RegisterMetricsBinding; import com.kumuluz.ee.metrics.utils.AnnotationMetadata; import org.eclipse.microprofile.metrics.Metadata; @@ -37,6 +40,8 @@ import java.lang.reflect.Member; import java.lang.reflect.Method; import java.lang.reflect.Modifier; +import java.util.HashSet; +import java.util.Set; /** * Interceptor for registering Timed, Metered and Counted annotations on bean construct. @@ -50,6 +55,8 @@ @Priority(Interceptor.Priority.LIBRARY_BEFORE - 10) public class RegisterMetricsInterceptor { + private static final Set processedElements = new HashSet<>(); + @Inject private MetricRegistry registry; @@ -71,18 +78,25 @@ private Object registerMetrics(InvocationContext context) throws Exception { return context.proceed(); } - private void registerMetrics(Class bean, E element) { + private synchronized void registerMetrics(Class bean, E element) { + + if (processedElements.contains(element)) { + return; + } + if (AnnotationMetadata.getAnnotation(bean, element, Counted.class) != null) { Metadata m = AnnotationMetadata.buildMetadata(bean, element, Counted.class); - registry.counter(m); + registry.register(m, new CounterImpl()); } if (AnnotationMetadata.getAnnotation(bean, element, Timed.class) != null) { Metadata m = AnnotationMetadata.buildMetadata(bean, element, Timed.class); - registry.timer(m); + registry.register(m, new TimerImpl()); } if (AnnotationMetadata.getAnnotation(bean, element, Metered.class) != null) { Metadata m = AnnotationMetadata.buildMetadata(bean, element, Metered.class); - registry.meter(m); + registry.register(m, new MeterImpl()); } + + processedElements.add(element); } } diff --git a/core/src/main/java/com/kumuluz/ee/metrics/utils/AnnotationMetadata.java b/core/src/main/java/com/kumuluz/ee/metrics/utils/AnnotationMetadata.java index 2a70573..83f5a83 100644 --- a/core/src/main/java/com/kumuluz/ee/metrics/utils/AnnotationMetadata.java +++ b/core/src/main/java/com/kumuluz/ee/metrics/utils/AnnotationMetadata.java @@ -30,6 +30,10 @@ import java.lang.annotation.Annotation; import java.lang.reflect.*; import java.util.Arrays; +import java.util.List; +import java.util.logging.Level; +import java.util.logging.Logger; +import java.util.stream.Collectors; /** * Metadata Builders. @@ -40,6 +44,8 @@ */ public class AnnotationMetadata { + private static final Logger LOG = Logger.getLogger(AnnotationMetadata.class.getName()); + public static T getAnnotation (Class bean, E element, Class annotationClass) { @@ -149,6 +155,14 @@ private static Metadata buildMetadata(C } Metadata metadata = new Metadata(finalName, type); + List missingEqualSign = Arrays.stream(tags) + .filter(tag -> tag != null && !tag.isEmpty() && !tag.contains("=")) + .collect(Collectors.toList()); + if (!missingEqualSign.isEmpty()) { + LOG.log(Level.WARNING, String.format("Annotation %s at %s#%s has tags that don't contain equal sign (=)." + + "They will be ignored. [%s]", type, bean.getName(), member.getName(), + String.join(",", missingEqualSign))); + } Arrays.stream(tags).forEach(metadata::addTag); metadata.setDisplayName(displayName); metadata.setDescription(description);