Skip to content

Commit

Permalink
Merge pull request #7 from kumuluz/mp-metrics-2.1.0
Browse files Browse the repository at this point in the history
Mp metrics 2.1.0
  • Loading branch information
jmezna authored Feb 3, 2020
2 parents 0161281 + a6d56af commit 7fa21b5
Show file tree
Hide file tree
Showing 12 changed files with 54 additions and 47 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -293,7 +293,7 @@ public long getCount() {
}
};
} else {
return (Gauge<Object>) ((com.codahale.metrics.Gauge) metric)::getValue;
return (Gauge<Number>) ((com.codahale.metrics.Gauge<Number>) metric)::getValue;
}
} else {
return null;
Expand Down
16 changes: 14 additions & 2 deletions core/src/main/java/com/kumuluz/ee/metrics/MetricsExtensionCDI.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,15 @@
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;
import javax.enterprise.inject.Default;
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;
Expand Down Expand Up @@ -84,7 +86,7 @@ private void metricProducerField(@Observes ProcessProducerField<? extends Metric
Field member = ppf.getAnnotatedProducerField().getJavaMember();
Class<?> 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));
}
Expand All @@ -96,7 +98,7 @@ private void metricProducerMethod(@Observes ProcessProducerMethod<? extends Metr
Method member = ppm.getAnnotatedProducerMethod().getJavaMember();
Class<?> 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));
}
Expand Down Expand Up @@ -128,4 +130,14 @@ private static boolean hasInjectionPoints(AnnotatedMember<?> member) {
private static <T> T getReference(BeanManager manager, Type type, Bean<?> bean) {
return (T) manager.getReference(bean, type, manager.createCreationalContext(bean));
}

private static <E extends Member> 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());
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
* @author Aljaž Blažej
* @since 1.0.0
*/
public class ForwardingGauge implements Gauge {
public class ForwardingGauge implements Gauge<Number> {

private final Method method;

Expand All @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -70,7 +71,7 @@ private Object countedMethod(InvocationContext context) throws Exception {

private <E extends Member & AnnotatedElement> 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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -71,7 +72,7 @@ private Object countedMethod(InvocationContext context) throws Exception {

private <E extends Member & AnnotatedElement> 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 ["
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -71,7 +72,7 @@ private Object meteredMethod(InvocationContext context) throws Exception {

private <E extends Member & AnnotatedElement> 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 ["
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -89,19 +90,19 @@ private <E extends Member & AnnotatedElement> 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());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -71,7 +72,7 @@ private Object timedMethod(InvocationContext context) throws Exception {

private <E extends Member & AnnotatedElement> 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 ["
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 <T> Gauge<T> produceGauge(InjectionPoint injectionPoint) {
MetadataWithTags metadataWithTags = AnnotationMetadata.buildProducerMetadata(injectionPoint);
public <T extends Number> Gauge<T> produceGauge(InjectionPoint injectionPoint) {
MetadataWithTags metadataWithTags = AnnotationMetadata.buildProducerMetadata(injectionPoint, MetricType.GAUGE);

return () -> (T) applicationRegistry.getGauges().get(metadataWithTags.getMetricID()).getValue();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -65,14 +67,14 @@ public class AnnotationMetadata {
}

public static <E extends Member & AnnotatedElement, T extends Annotation> MetadataWithTags buildMetadata
(Class<?> bean, E element, Class<T> annotationClass) {
(Class<?> bean, E element, Class<T> 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 <M extends Member, T extends Annotation> MetadataWithTags buildMetadata(Class<?> bean, M member,
T annotation, boolean fromElement) {
T annotation, boolean fromElement, MetricType metricType) {

MetricType type;
boolean absolute;
Expand Down Expand Up @@ -133,7 +135,7 @@ private static <M extends Member, T extends Annotation> 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();
Expand All @@ -142,7 +144,7 @@ private static <M extends Member, T extends Annotation> MetadataWithTags buildMe
unit = a.unit();
} else {
absolute = false;
type = getMetricType(member);
type = metricType;
}

String finalName;
Expand Down Expand Up @@ -183,18 +185,14 @@ private static <M extends Member, T extends Annotation> 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) {
Expand All @@ -204,13 +202,4 @@ private static String memberName(Member member) {
return member.getName();
}

private static <E extends Member> 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());
}
}
}
6 changes: 3 additions & 3 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -30,16 +30,16 @@

<jaxb-api.version>2.3.1</jaxb-api.version>

<kumuluzee.version>3.3.0</kumuluzee.version>
<kumuluzee.version>3.6.0</kumuluzee.version>
<kumuluzee-config-mp.version>1.3.0</kumuluzee-config-mp.version>

<microprofile-metrics.version>2.0.1</microprofile-metrics.version>
<microprofile-metrics.version>2.1.0</microprofile-metrics.version>
<dropwizard-metrics.version>4.1.0</dropwizard-metrics.version>
<jackson.version>2.9.9</jackson.version>

<junit.version>4.12</junit.version>
<hamcrest.version>1.3</hamcrest.version>
<arquillian.version>1.4.1.Final</arquillian.version>
<arquillian.version>1.5.0.Final</arquillian.version>
<kumuluzee-arquillian-container.version>1.1.0</kumuluzee-arquillian-container.version>

<maven-compiler-plugin.version>3.8.0</maven-compiler-plugin.version>
Expand Down

0 comments on commit 7fa21b5

Please sign in to comment.