From 16e278b64454243fb4eca201c06a68eb01fd0d5b Mon Sep 17 00:00:00 2001 From: Gregor Porocnik Date: Fri, 10 Jan 2020 10:28:42 +0100 Subject: [PATCH 1/2] MP-metrics 2.2.1 integration, upped arquillian & kumuluz version --- .../ee/metrics/MetricsExtensionCDI.java | 16 +++++++-- .../ConcurrentGaugeInterceptor.java | 3 +- .../interceptors/CountedInterceptor.java | 3 +- .../interceptors/GaugeInterceptor.java | 3 +- .../interceptors/MeteredInterceptor.java | 3 +- .../RegisterMetricsInterceptor.java | 9 ++--- .../interceptors/TimedInterceptor.java | 3 +- .../ee/metrics/producers/MetricProducer.java | 12 +++---- .../ee/metrics/utils/AnnotationMetadata.java | 33 +++++++------------ pom.xml | 6 ++-- 10 files changed, 49 insertions(+), 42 deletions(-) diff --git a/core/src/main/java/com/kumuluz/ee/metrics/MetricsExtensionCDI.java b/core/src/main/java/com/kumuluz/ee/metrics/MetricsExtensionCDI.java index 35e991a..04ad60a 100644 --- a/core/src/main/java/com/kumuluz/ee/metrics/MetricsExtensionCDI.java +++ b/core/src/main/java/com/kumuluz/ee/metrics/MetricsExtensionCDI.java @@ -29,6 +29,7 @@ import com.kumuluz.ee.metrics.utils.ProducerMemberRegistration; import org.eclipse.microprofile.metrics.Metric; import org.eclipse.microprofile.metrics.MetricRegistry; +import org.eclipse.microprofile.metrics.MetricType; import org.eclipse.microprofile.metrics.annotation.*; import javax.enterprise.event.Observes; @@ -36,6 +37,7 @@ import javax.enterprise.inject.spi.*; import javax.enterprise.util.AnnotationLiteral; import java.lang.reflect.Field; +import java.lang.reflect.Member; import java.lang.reflect.Method; import java.lang.reflect.Type; import java.util.LinkedList; @@ -84,7 +86,7 @@ private void metricProducerField(@Observes ProcessProducerField bean = member.getDeclaringClass(); MetadataWithTags metadata = AnnotationMetadata.buildMetadata(bean, member, - org.eclipse.microprofile.metrics.annotation.Metric.class); + org.eclipse.microprofile.metrics.annotation.Metric.class, getMetricType(member)); producerMembers.add(new ProducerMemberRegistration(ppf.getBean(), ppf.getAnnotatedProducerField(), metadata)); } @@ -96,7 +98,7 @@ private void metricProducerMethod(@Observes ProcessProducerMethod bean = member.getDeclaringClass(); MetadataWithTags metadata = AnnotationMetadata.buildMetadata(bean, member, - org.eclipse.microprofile.metrics.annotation.Metric.class); + org.eclipse.microprofile.metrics.annotation.Metric.class, getMetricType(member)); producerMembers.add(new ProducerMemberRegistration(ppm.getBean(), ppm.getAnnotatedProducerMethod(), metadata)); } @@ -128,4 +130,14 @@ private static boolean hasInjectionPoints(AnnotatedMember member) { private static T getReference(BeanManager manager, Type type, Bean bean) { return (T) manager.getReference(bean, type, manager.createCreationalContext(bean)); } + + private static MetricType getMetricType(E element) { + if (element instanceof Field) { + return MetricType.from(((Field) element).getType()); + } else if (element instanceof Method) { + return MetricType.from(((Method) element).getReturnType()); + } else { + return MetricType.from(element.getClass()); + } + } } diff --git a/core/src/main/java/com/kumuluz/ee/metrics/interceptors/ConcurrentGaugeInterceptor.java b/core/src/main/java/com/kumuluz/ee/metrics/interceptors/ConcurrentGaugeInterceptor.java index b01af94..e24a637 100644 --- a/core/src/main/java/com/kumuluz/ee/metrics/interceptors/ConcurrentGaugeInterceptor.java +++ b/core/src/main/java/com/kumuluz/ee/metrics/interceptors/ConcurrentGaugeInterceptor.java @@ -23,6 +23,7 @@ import com.kumuluz.ee.metrics.utils.AnnotationMetadata; import com.kumuluz.ee.metrics.utils.MetadataWithTags; import org.eclipse.microprofile.metrics.MetricRegistry; +import org.eclipse.microprofile.metrics.MetricType; import org.eclipse.microprofile.metrics.annotation.ConcurrentGauge; import javax.annotation.Priority; @@ -70,7 +71,7 @@ private Object countedMethod(InvocationContext context) throws Exception { private Object applyInterceptor(InvocationContext context, E member) throws Exception { - MetadataWithTags metadata = AnnotationMetadata.buildMetadata(bean.getBeanClass(), member, ConcurrentGauge.class); + MetadataWithTags metadata = AnnotationMetadata.buildMetadata(bean.getBeanClass(), member, ConcurrentGauge.class, MetricType.CONCURRENT_GAUGE); org.eclipse.microprofile.metrics.ConcurrentGauge concurrentGauge = applicationRegistry.getConcurrentGauges() .get(metadata.getMetricID()); if (concurrentGauge == null) { diff --git a/core/src/main/java/com/kumuluz/ee/metrics/interceptors/CountedInterceptor.java b/core/src/main/java/com/kumuluz/ee/metrics/interceptors/CountedInterceptor.java index 370ea8f..22601af 100644 --- a/core/src/main/java/com/kumuluz/ee/metrics/interceptors/CountedInterceptor.java +++ b/core/src/main/java/com/kumuluz/ee/metrics/interceptors/CountedInterceptor.java @@ -24,6 +24,7 @@ import com.kumuluz.ee.metrics.utils.MetadataWithTags; import org.eclipse.microprofile.metrics.Counter; import org.eclipse.microprofile.metrics.MetricRegistry; +import org.eclipse.microprofile.metrics.MetricType; import org.eclipse.microprofile.metrics.annotation.Counted; import javax.annotation.Priority; @@ -71,7 +72,7 @@ private Object countedMethod(InvocationContext context) throws Exception { private Object applyInterceptor(InvocationContext context, E member) throws Exception { - MetadataWithTags metadata = AnnotationMetadata.buildMetadata(bean.getBeanClass(), member, Counted.class); + MetadataWithTags metadata = AnnotationMetadata.buildMetadata(bean.getBeanClass(), member, Counted.class, MetricType.COUNTER); Counter counter = applicationRegistry.getCounters().get(metadata.getMetricID()); if (counter == null) { throw new IllegalStateException("No counter with ID [" + metadata.getMetricID() + "] found in registry [" diff --git a/core/src/main/java/com/kumuluz/ee/metrics/interceptors/GaugeInterceptor.java b/core/src/main/java/com/kumuluz/ee/metrics/interceptors/GaugeInterceptor.java index 202c65b..26e1183 100644 --- a/core/src/main/java/com/kumuluz/ee/metrics/interceptors/GaugeInterceptor.java +++ b/core/src/main/java/com/kumuluz/ee/metrics/interceptors/GaugeInterceptor.java @@ -26,6 +26,7 @@ import com.kumuluz.ee.metrics.utils.MetadataWithTags; import org.eclipse.microprofile.metrics.Gauge; import org.eclipse.microprofile.metrics.MetricRegistry; +import org.eclipse.microprofile.metrics.MetricType; import javax.annotation.Priority; import javax.inject.Inject; @@ -61,7 +62,7 @@ private Object gaugeBeanConstructor(InvocationContext context) throws Exception for (Method method : type.getDeclaredMethods()) { if (method.isAnnotationPresent(org.eclipse.microprofile.metrics.annotation.Gauge.class)) { MetadataWithTags metadata = AnnotationMetadata.buildMetadata(type, method, - org.eclipse.microprofile.metrics.annotation.Gauge.class); + org.eclipse.microprofile.metrics.annotation.Gauge.class, MetricType.GAUGE); Gauge gauge = applicationRegistry.getGauges().get(metadata.getMetricID()); if (gauge == null) { diff --git a/core/src/main/java/com/kumuluz/ee/metrics/interceptors/MeteredInterceptor.java b/core/src/main/java/com/kumuluz/ee/metrics/interceptors/MeteredInterceptor.java index 5cbc92e..9d5b0b5 100644 --- a/core/src/main/java/com/kumuluz/ee/metrics/interceptors/MeteredInterceptor.java +++ b/core/src/main/java/com/kumuluz/ee/metrics/interceptors/MeteredInterceptor.java @@ -24,6 +24,7 @@ import com.kumuluz.ee.metrics.utils.MetadataWithTags; import org.eclipse.microprofile.metrics.Meter; import org.eclipse.microprofile.metrics.MetricRegistry; +import org.eclipse.microprofile.metrics.MetricType; import org.eclipse.microprofile.metrics.annotation.Metered; import javax.annotation.Priority; @@ -71,7 +72,7 @@ private Object meteredMethod(InvocationContext context) throws Exception { private Object applyInterceptor(InvocationContext context, E member) throws Exception { - MetadataWithTags metadata = AnnotationMetadata.buildMetadata(bean.getBeanClass(), member, Metered.class); + MetadataWithTags metadata = AnnotationMetadata.buildMetadata(bean.getBeanClass(), member, Metered.class, MetricType.METERED); Meter meter = applicationRegistry.getMeters().get(metadata.getMetricID()); if (meter == null) { throw new IllegalStateException("No meter with ID [" + metadata.getMetricID() + "] found in registry [" 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 ea9c997..b3dead5 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 @@ -28,6 +28,7 @@ import com.kumuluz.ee.metrics.utils.AnnotationMetadata; import com.kumuluz.ee.metrics.utils.MetadataWithTags; import org.eclipse.microprofile.metrics.MetricRegistry; +import org.eclipse.microprofile.metrics.MetricType; import org.eclipse.microprofile.metrics.annotation.ConcurrentGauge; import org.eclipse.microprofile.metrics.annotation.Counted; import org.eclipse.microprofile.metrics.annotation.Metered; @@ -89,19 +90,19 @@ private void registerMetrics(Class bean } if (AnnotationMetadata.getAnnotation(bean, element, Counted.class) != null) { - MetadataWithTags m = AnnotationMetadata.buildMetadata(bean, element, Counted.class); + MetadataWithTags m = AnnotationMetadata.buildMetadata(bean, element, Counted.class, MetricType.COUNTER); registry.register(m.getMetadata(), new CounterImpl(), m.getTags()); } if (AnnotationMetadata.getAnnotation(bean, element, Timed.class) != null) { - MetadataWithTags m = AnnotationMetadata.buildMetadata(bean, element, Timed.class); + MetadataWithTags m = AnnotationMetadata.buildMetadata(bean, element, Timed.class, MetricType.TIMER); registry.register(m.getMetadata(), new TimerImpl(), m.getTags()); } if (AnnotationMetadata.getAnnotation(bean, element, Metered.class) != null) { - MetadataWithTags m = AnnotationMetadata.buildMetadata(bean, element, Metered.class); + MetadataWithTags m = AnnotationMetadata.buildMetadata(bean, element, Metered.class, MetricType.METERED); registry.register(m.getMetadata(), new MeterImpl(), m.getTags()); } if (AnnotationMetadata.getAnnotation(bean, element, ConcurrentGauge.class) != null) { - MetadataWithTags m = AnnotationMetadata.buildMetadata(bean, element, ConcurrentGauge.class); + MetadataWithTags m = AnnotationMetadata.buildMetadata(bean, element, ConcurrentGauge.class, MetricType.CONCURRENT_GAUGE); registry.register(m.getMetadata(), new ConcurrentGaugeImpl(), m.getTags()); } diff --git a/core/src/main/java/com/kumuluz/ee/metrics/interceptors/TimedInterceptor.java b/core/src/main/java/com/kumuluz/ee/metrics/interceptors/TimedInterceptor.java index 96b9978..ca03ded 100644 --- a/core/src/main/java/com/kumuluz/ee/metrics/interceptors/TimedInterceptor.java +++ b/core/src/main/java/com/kumuluz/ee/metrics/interceptors/TimedInterceptor.java @@ -23,6 +23,7 @@ import com.kumuluz.ee.metrics.utils.AnnotationMetadata; import com.kumuluz.ee.metrics.utils.MetadataWithTags; import org.eclipse.microprofile.metrics.MetricRegistry; +import org.eclipse.microprofile.metrics.MetricType; import org.eclipse.microprofile.metrics.Timer; import org.eclipse.microprofile.metrics.annotation.Timed; @@ -71,7 +72,7 @@ private Object timedMethod(InvocationContext context) throws Exception { private Object applyInterceptor(InvocationContext context, E member) throws Exception { - MetadataWithTags metadata = AnnotationMetadata.buildMetadata(bean.getBeanClass(), member, Timed.class); + MetadataWithTags metadata = AnnotationMetadata.buildMetadata(bean.getBeanClass(), member, Timed.class, MetricType.TIMER); Timer timer = applicationRegistry.getTimers().get(metadata.getMetricID()); if (timer == null) { throw new IllegalStateException("No timer with ID [" + metadata.getMetricID() + "] found in registry [" diff --git a/core/src/main/java/com/kumuluz/ee/metrics/producers/MetricProducer.java b/core/src/main/java/com/kumuluz/ee/metrics/producers/MetricProducer.java index 8450f8e..13ef69a 100644 --- a/core/src/main/java/com/kumuluz/ee/metrics/producers/MetricProducer.java +++ b/core/src/main/java/com/kumuluz/ee/metrics/producers/MetricProducer.java @@ -49,38 +49,38 @@ public class MetricProducer { @Produces public Meter produceMeter(InjectionPoint injectionPoint) { - MetadataWithTags metadataWithTags = AnnotationMetadata.buildProducerMetadata(injectionPoint); + MetadataWithTags metadataWithTags = AnnotationMetadata.buildProducerMetadata(injectionPoint, MetricType.METERED); return applicationRegistry.meter(metadataWithTags.getMetadata(), metadataWithTags.getTags()); } @Produces public Timer produceTimer(InjectionPoint injectionPoint) { - MetadataWithTags metadataWithTags = AnnotationMetadata.buildProducerMetadata(injectionPoint); + MetadataWithTags metadataWithTags = AnnotationMetadata.buildProducerMetadata(injectionPoint, MetricType.TIMER); return applicationRegistry.timer(metadataWithTags.getMetadata(), metadataWithTags.getTags()); } @Produces public Counter produceCounter(InjectionPoint injectionPoint) { - MetadataWithTags metadataWithTags = AnnotationMetadata.buildProducerMetadata(injectionPoint); + MetadataWithTags metadataWithTags = AnnotationMetadata.buildProducerMetadata(injectionPoint, MetricType.COUNTER); return applicationRegistry.counter(metadataWithTags.getMetadata(), metadataWithTags.getTags()); } @Produces public ConcurrentGauge produceConcurrentGauge(InjectionPoint injectionPoint) { - MetadataWithTags metadataWithTags = AnnotationMetadata.buildProducerMetadata(injectionPoint); + MetadataWithTags metadataWithTags = AnnotationMetadata.buildProducerMetadata(injectionPoint, MetricType.CONCURRENT_GAUGE); return applicationRegistry.concurrentGauge(metadataWithTags.getMetadata(), metadataWithTags.getTags()); } @Produces public Histogram produceHistogram(InjectionPoint injectionPoint) { - MetadataWithTags metadataWithTags = AnnotationMetadata.buildProducerMetadata(injectionPoint); + MetadataWithTags metadataWithTags = AnnotationMetadata.buildProducerMetadata(injectionPoint, MetricType.HISTOGRAM); return applicationRegistry.histogram(metadataWithTags.getMetadata(), metadataWithTags.getTags()); } @SuppressWarnings("unchecked") @Produces public Gauge produceGauge(InjectionPoint injectionPoint) { - MetadataWithTags metadataWithTags = AnnotationMetadata.buildProducerMetadata(injectionPoint); + MetadataWithTags metadataWithTags = AnnotationMetadata.buildProducerMetadata(injectionPoint, MetricType.GAUGE); return () -> (T) applicationRegistry.getGauges().get(metadataWithTags.getMetricID()).getValue(); } 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 5744d27..7f590c3 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 @@ -29,7 +29,9 @@ import javax.enterprise.inject.spi.InjectionPoint; import java.lang.annotation.Annotation; -import java.lang.reflect.*; +import java.lang.reflect.AnnotatedElement; +import java.lang.reflect.Constructor; +import java.lang.reflect.Member; import java.util.Arrays; import java.util.List; import java.util.logging.Level; @@ -65,14 +67,14 @@ public class AnnotationMetadata { } public static MetadataWithTags buildMetadata - (Class bean, E element, Class annotationClass) { + (Class bean, E element, Class annotationClass, MetricType metricType) { T annotation = getAnnotation(bean, element, annotationClass); boolean fromElement = element.isAnnotationPresent(annotationClass); - return buildMetadata(bean, element, annotation, fromElement); + return buildMetadata(bean, element, annotation, fromElement, metricType); } private static MetadataWithTags buildMetadata(Class bean, M member, - T annotation, boolean fromElement) { + T annotation, boolean fromElement, MetricType metricType) { MetricType type; boolean absolute; @@ -133,7 +135,7 @@ private static MetadataWithTags buildMe unit = a.unit(); } else if (annotation instanceof Metric) { Metric a = (Metric) annotation; - type = getMetricType(member); + type = metricType; absolute = a.absolute(); name = a.name(); tags = a.tags(); @@ -142,7 +144,7 @@ private static MetadataWithTags buildMe unit = a.unit(); } else { absolute = false; - type = getMetricType(member); + type = metricType; } String finalName; @@ -183,18 +185,14 @@ private static MetadataWithTags buildMe metadataBuilder.withDisplayName(displayName); metadataBuilder.withDescription(description); metadataBuilder.withUnit(unit); - if (reusable) { - metadataBuilder.reusable(); - } else { - metadataBuilder.notReusable(); - } + metadataBuilder.reusable(reusable); return new MetadataWithTags(metadataBuilder.build(), parsedTags); } - public static MetadataWithTags buildProducerMetadata(InjectionPoint injectionPoint) { + public static MetadataWithTags buildProducerMetadata(InjectionPoint injectionPoint, MetricType metricType) { return buildMetadata(injectionPoint.getMember().getDeclaringClass(), injectionPoint.getMember(), - injectionPoint.getAnnotated().getAnnotation(Metric.class), true); + injectionPoint.getAnnotated().getAnnotation(Metric.class), true, metricType); } private static String memberName(Member member) { @@ -204,13 +202,4 @@ private static String memberName(Member member) { return member.getName(); } - private static MetricType getMetricType(E element) { - if (element instanceof Field) { - return MetricType.from(((Field) element).getType()); - } else if (element instanceof Method) { - return MetricType.from(((Method) element).getReturnType()); - } else { - return MetricType.from(element.getClass()); - } - } } diff --git a/pom.xml b/pom.xml index 97dc1b5..88e623d 100644 --- a/pom.xml +++ b/pom.xml @@ -30,16 +30,16 @@ 2.3.1 - 3.3.0 + 3.6.0 1.3.0 - 2.0.1 + 2.2.1 4.1.0 2.9.9 4.12 1.3 - 1.4.1.Final + 1.5.0.Final 1.1.0 3.8.0 From a6d56afdd7fc29a73f2859527994ccbc092b6e2c Mon Sep 17 00:00:00 2001 From: Gregor Porocnik Date: Tue, 14 Jan 2020 13:21:16 +0100 Subject: [PATCH 2/2] MP-metrics 2.1.0 integration --- .../main/java/com/kumuluz/ee/metrics/MetricsExtension.java | 2 +- .../java/com/kumuluz/ee/metrics/api/ForwardingGauge.java | 6 +++--- .../com/kumuluz/ee/metrics/producers/MetricProducer.java | 2 +- pom.xml | 2 +- 4 files changed, 6 insertions(+), 6 deletions(-) diff --git a/core/src/main/java/com/kumuluz/ee/metrics/MetricsExtension.java b/core/src/main/java/com/kumuluz/ee/metrics/MetricsExtension.java index ac48ad0..c9e8561 100644 --- a/core/src/main/java/com/kumuluz/ee/metrics/MetricsExtension.java +++ b/core/src/main/java/com/kumuluz/ee/metrics/MetricsExtension.java @@ -293,7 +293,7 @@ public long getCount() { } }; } else { - return (Gauge) ((com.codahale.metrics.Gauge) metric)::getValue; + return (Gauge) ((com.codahale.metrics.Gauge) metric)::getValue; } } else { return null; diff --git a/core/src/main/java/com/kumuluz/ee/metrics/api/ForwardingGauge.java b/core/src/main/java/com/kumuluz/ee/metrics/api/ForwardingGauge.java index 336a6fe..834c07c 100644 --- a/core/src/main/java/com/kumuluz/ee/metrics/api/ForwardingGauge.java +++ b/core/src/main/java/com/kumuluz/ee/metrics/api/ForwardingGauge.java @@ -32,7 +32,7 @@ * @author Aljaž Blažej * @since 1.0.0 */ -public class ForwardingGauge implements Gauge { +public class ForwardingGauge implements Gauge { private final Method method; @@ -45,8 +45,8 @@ public ForwardingGauge(Method method, Object object) { } @Override - public Object getValue() { - return invokeMethod(method, object); + public Number getValue() { + return (Number) invokeMethod(method, object); } private static Object invokeMethod(Method method, Object object) { diff --git a/core/src/main/java/com/kumuluz/ee/metrics/producers/MetricProducer.java b/core/src/main/java/com/kumuluz/ee/metrics/producers/MetricProducer.java index 13ef69a..0829903 100644 --- a/core/src/main/java/com/kumuluz/ee/metrics/producers/MetricProducer.java +++ b/core/src/main/java/com/kumuluz/ee/metrics/producers/MetricProducer.java @@ -79,7 +79,7 @@ public Histogram produceHistogram(InjectionPoint injectionPoint) { @SuppressWarnings("unchecked") @Produces - public Gauge produceGauge(InjectionPoint injectionPoint) { + public Gauge produceGauge(InjectionPoint injectionPoint) { MetadataWithTags metadataWithTags = AnnotationMetadata.buildProducerMetadata(injectionPoint, MetricType.GAUGE); return () -> (T) applicationRegistry.getGauges().get(metadataWithTags.getMetricID()).getValue(); diff --git a/pom.xml b/pom.xml index 88e623d..245e1bd 100644 --- a/pom.xml +++ b/pom.xml @@ -33,7 +33,7 @@ 3.6.0 1.3.0 - 2.2.1 + 2.1.0 4.1.0 2.9.9