Skip to content
Permalink
Browse files
fixing lookup based on MetricId and not String anymore
  • Loading branch information
rmannibucau committed Dec 31, 2019
1 parent 5230350 commit 3d5b86a1b376db24655faf0589b4634aad1a24e5
Showing 5 changed files with 29 additions and 9 deletions.
@@ -39,6 +39,7 @@
import org.eclipse.microprofile.metrics.ConcurrentGauge;
import org.eclipse.microprofile.metrics.MetricID;
import org.eclipse.microprofile.metrics.MetricRegistry;
import org.eclipse.microprofile.metrics.Tag;

@Interceptor
@Priority(Interceptor.Priority.LIBRARY_BEFORE)
@@ -54,6 +55,9 @@ public class ConcurrentGaugeInterceptor implements Serializable {
@Inject
private BeanManager beanManager;

@Inject
private MetricsExtension extension;

private transient volatile ConcurrentMap<Executable, Meta> gauges = new ConcurrentHashMap<>();

@AroundConstruct
@@ -89,24 +93,25 @@ private Meta find(final Executable executable) {
Meta meta = gauges.get(executable);
if (meta == null) {
final AnnotatedType<?> type = beanManager.createAnnotatedType(bean.getBeanClass());
final org.eclipse.microprofile.metrics.annotation.ConcurrentGauge counted = Stream.concat(type.getMethods().stream(), type.getConstructors().stream())
final org.eclipse.microprofile.metrics.annotation.ConcurrentGauge concurrentGauge = Stream.concat(type.getMethods().stream(), type.getConstructors().stream())
.filter(it -> it.getJavaMember().equals(executable))
.findFirst()
.map(m -> m.getAnnotation(org.eclipse.microprofile.metrics.annotation.ConcurrentGauge.class))
.orElse(null);
final String name = Names.findName(
Modifier.isAbstract(executable.getDeclaringClass().getModifiers()) ? type.getJavaClass() : executable.getDeclaringClass(),
executable, counted == null ? null : counted.name(),
counted != null && counted.absolute(),
executable, concurrentGauge == null ? null : concurrentGauge.name(),
concurrentGauge != null && concurrentGauge.absolute(),
ofNullable(type.getAnnotation(org.eclipse.microprofile.metrics.annotation.ConcurrentGauge.class))
.map(org.eclipse.microprofile.metrics.annotation.ConcurrentGauge::name)
.orElse(""));

final ConcurrentGauge counter = ConcurrentGauge.class.cast(registry.getMetrics().get(new MetricID(name)));
final ConcurrentGauge counter = ConcurrentGauge.class.cast(registry.getMetrics().get(
new MetricID(name, concurrentGauge == null ? new Tag[0] : extension.createTags(concurrentGauge.tags()))));
if (counter == null) {
throw new IllegalStateException("No counter with name [" + name + "] found in registry [" + registry + "]");
}
meta = new Meta(counter, !ofNullable(counted)
meta = new Meta(counter, !ofNullable(concurrentGauge)
.orElseGet(() -> type.getAnnotation(org.eclipse.microprofile.metrics.annotation.ConcurrentGauge.class)).absolute());
gauges.putIfAbsent(executable, meta);
}
@@ -37,7 +37,9 @@
import javax.interceptor.InvocationContext;

import org.eclipse.microprofile.metrics.Counter;
import org.eclipse.microprofile.metrics.MetricID;
import org.eclipse.microprofile.metrics.MetricRegistry;
import org.eclipse.microprofile.metrics.Tag;
import org.eclipse.microprofile.metrics.annotation.Counted;

@Counted
@@ -54,6 +56,9 @@ public class CountedInterceptor implements Serializable {
@Inject
private BeanManager beanManager;

@Inject
private MetricsExtension extension;

private transient volatile ConcurrentMap<Executable, Meta> counters = new ConcurrentHashMap<>();

@AroundConstruct
@@ -94,7 +99,8 @@ private Meta findCounter(final Executable executable) {
counted != null && counted.absolute(),
ofNullable(type.getAnnotation(Counted.class)).map(Counted::name).orElse(""));

final Counter counter = Counter.class.cast(registry.getMetrics().get(name));
final Counter counter = Counter.class.cast(registry.getMetrics().get(
new MetricID(name, counted == null ? new Tag[0] : extension.createTags(counted.tags()))));
if (counter == null) {
throw new IllegalStateException("No counter with name [" + name + "] found in registry [" + registry + "]");
}
@@ -37,7 +37,9 @@
import javax.interceptor.InvocationContext;

import org.eclipse.microprofile.metrics.Meter;
import org.eclipse.microprofile.metrics.MetricID;
import org.eclipse.microprofile.metrics.MetricRegistry;
import org.eclipse.microprofile.metrics.Tag;
import org.eclipse.microprofile.metrics.annotation.Metered;

@Metered
@@ -54,6 +56,9 @@ public class MeteredInterceptor implements Serializable {
@Inject
private BeanManager beanManager;

@Inject
private MetricsExtension extension;

private transient volatile ConcurrentMap<Executable, Meter> meters = new ConcurrentHashMap<>();

@AroundConstruct
@@ -90,7 +95,8 @@ private Meter findMeter(final Executable executable) {
metered != null && metered.absolute(),
ofNullable(type.getAnnotation(Metered.class)).map(Metered::name).orElse(""));

meter = Meter.class.cast(registry.getMetrics().get(name));
meter = Meter.class.cast(registry.getMetrics().get(
new MetricID(name, metered == null ? new Tag[0] : extension.createTags(metered.tags()))));
if (meter == null) {
throw new IllegalStateException("No meter with name [" + name + "] found in registry [" + registry + "]");
}
@@ -193,7 +193,7 @@ void onMetric(@Observes final ProcessInjectionPoint<?, ?> metricInjectionPointPr
}
}

private Tag[] createTags(final String[] tags) {
public Tag[] createTags(final String[] tags) {
return Stream.of(tags).filter(it -> it.contains("=")).map(it -> {
final int sep = it.indexOf("=");
return new Tag(it.substring(0, sep), it.substring(sep + 1));
@@ -36,7 +36,9 @@
import javax.interceptor.Interceptor;
import javax.interceptor.InvocationContext;

import org.eclipse.microprofile.metrics.MetricID;
import org.eclipse.microprofile.metrics.MetricRegistry;
import org.eclipse.microprofile.metrics.Tag;
import org.eclipse.microprofile.metrics.Timer;
import org.eclipse.microprofile.metrics.annotation.Timed;

@@ -89,7 +91,8 @@ private Timer findTimer(final Executable executable) {
Modifier.isAbstract(executable.getDeclaringClass().getModifiers()) ? type.getJavaClass() : executable.getDeclaringClass(),
executable, timed == null ? null : timed.name(), timed != null && timed.absolute(),
ofNullable(type.getAnnotation(Timed.class)).map(Timed::name).orElse(""));
timer = Timer.class.cast(registry.getMetrics().get(name));
timer = Timer.class.cast(registry.getMetrics().get(
new MetricID(name, timed == null ? new Tag[0] : extension.createTags(timed.tags()))));
if (timer == null) {
throw new IllegalStateException("No timer with name [" + name + "] found in registry [" + registry + "]");
}

0 comments on commit 3d5b86a

Please sign in to comment.