Skip to content

Commit

Permalink
Fixed non reusable metric bug
Browse files Browse the repository at this point in the history
  • Loading branch information
urbim committed Jan 29, 2019
1 parent 942bf85 commit 1fcaa31
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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.
Expand All @@ -50,6 +55,8 @@
@Priority(Interceptor.Priority.LIBRARY_BEFORE - 10)
public class RegisterMetricsInterceptor {

private static final Set<Member> processedElements = new HashSet<>();

@Inject
private MetricRegistry registry;

Expand All @@ -71,18 +78,25 @@ private Object registerMetrics(InvocationContext context) throws Exception {
return context.proceed();
}

private <E extends Member & AnnotatedElement> void registerMetrics(Class<?> bean, E element) {
private synchronized <E extends Member & AnnotatedElement> 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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -40,6 +44,8 @@
*/
public class AnnotationMetadata {

private static final Logger LOG = Logger.getLogger(AnnotationMetadata.class.getName());

public static <E extends AnnotatedElement, T extends Annotation> T getAnnotation
(Class<?> bean, E element, Class<T> annotationClass) {

Expand Down Expand Up @@ -149,6 +155,14 @@ private static <M extends Member, T extends Annotation> Metadata buildMetadata(C
}

Metadata metadata = new Metadata(finalName, type);
List<String> 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);
Expand Down

0 comments on commit 1fcaa31

Please sign in to comment.