Skip to content
Permalink
Browse files
making tags binding in @Metric
  • Loading branch information
rmannibucau committed Jan 2, 2020
1 parent 3f482b6 commit 6e9ddc360c7038bd61e4a3dd8fd10aa082c6dfae
Showing 5 changed files with 125 additions and 95 deletions.
@@ -33,8 +33,8 @@ public class ConcurrentGaugeImpl implements ConcurrentGauge {

private volatile Instant currentMinute = CLOCK.instant();

private volatile long lastMax = -1;
private volatile long lastMin = -1;
private volatile long lastMax = 0;
private volatile long lastMin = 0;
private final String unit;

public ConcurrentGaugeImpl(final String unit) {
@@ -20,12 +20,14 @@
import static java.util.stream.Collectors.toMap;

import java.util.Map;
import java.util.Objects;
import java.util.SortedMap;
import java.util.SortedSet;
import java.util.TreeMap;
import java.util.TreeSet;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
import java.util.concurrent.atomic.AtomicBoolean;

import org.eclipse.microprofile.metrics.ConcurrentGauge;
import org.eclipse.microprofile.metrics.Counter;
@@ -48,7 +50,12 @@ public class RegistryImpl extends MetricRegistry {

@Override
public <T extends Metric> T register(final Metadata metadata, final T metric) throws IllegalArgumentException {
final MetricID metricID = new MetricID(metadata.getName());
return register(metadata, metric, NO_TAG);
}

@Override
public <T extends Metric> T register(final Metadata metadata, final T metric, final Tag... tags) throws IllegalArgumentException {
final MetricID metricID = new MetricID(metadata.getName(), tags);
final Holder<? extends Metric> holder = metrics.putIfAbsent(
metricID, new Holder<>(metric, metadata, metricID));
if (holder != null && !metadata.isReusable() && !holder.metadata.isReusable()) {
@@ -57,11 +64,6 @@ public <T extends Metric> T register(final Metadata metadata, final T metric) th
return metric;
}

@Override
public <T extends Metric> T register(final Metadata metadata, final T metric, final Tag... tags) throws IllegalArgumentException {
return register(Metadata.builder(metadata).build(), metric);
}

@Override
public <T extends Metric> T register(final String name, final T metric) throws IllegalArgumentException {
final MetricType type;
@@ -90,10 +92,11 @@ public Counter counter(final Metadata metadata) {

@Override
public Counter counter(final Metadata metadata, final Tag... tags) {
Holder<? extends Metric> holder = metrics.get(metadata.getName());
final MetricID metricID = new MetricID(metadata.getName(), tags);
Holder<? extends Metric> holder = metrics.get(metricID);
if (holder == null) {
holder = new Holder<>(new CounterImpl(
metadata.getUnit().orElse("")), metadata, new MetricID(metadata.getName(), tags));
metadata.getUnit().orElse("")), metadata, metricID);
final Holder<? extends Metric> existing = metrics.putIfAbsent(holder.metricID, holder);
if (existing != null) {
holder = existing;
@@ -266,7 +269,15 @@ public Timer timer(final String name, final Tag... tags) {

@Override
public boolean remove(final String name) {
return remove(new MetricID(name));
final AtomicBoolean done = new AtomicBoolean(false);
removeMatching((metricID, metric) -> {
final boolean equals = Objects.equals(metricID.getName(), name);
if (equals) {
done.set(true);
}
return equals;
});
return done.get();
}

@Override
@@ -27,6 +27,7 @@

import org.apache.meecrowave.Meecrowave;
import org.apache.meecrowave.junit.MeecrowaveRule;
import org.eclipse.microprofile.metrics.MetricID;
import org.eclipse.microprofile.metrics.MetricRegistry;
import org.eclipse.microprofile.metrics.annotation.RegistryType;
import org.junit.ClassRule;
@@ -48,7 +49,8 @@ public void test() {
final List<String> keys = registry.getGauges()
.keySet()
.stream()
.filter(it -> it.startsWith("sigar."))
.filter(it -> it.getName().startsWith("sigar."))
.map(MetricID::getName)
.sorted()
.collect(toList());
assertTrue(keys.toString(), keys.size() > 10 /*whatever, just check it is registered*/);

0 comments on commit 6e9ddc3

Please sign in to comment.