com.xqbase.metric.common.Metric Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of xqbase-metric-common-jdk17 Show documentation
Show all versions of xqbase-metric-common-jdk17 Show documentation
a lightweight metric framework for aggregating, collecting and showing metric data - common part
package com.xqbase.metric.common;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.concurrent.ConcurrentHashMap;
public class Metric {
private static ConcurrentHashMap
map = new ConcurrentHashMap<>();
private static void put(MetricKey key, double value) {
MetricValue current;
do {
current = map.get(key);
} while (current == null ? map.putIfAbsent(key, new MetricValue(value)) != null :
!map.replace(key, current, new MetricValue(current, value)));
}
public static void put(String name, double value, HashMap tagMap) {
put(new MetricKey(name, tagMap), value);
}
public static void put(String name, double value, String... tagPairs) {
put(new MetricKey(name, tagPairs), value);
}
public static ArrayList removeAll() {
ArrayList metrics = new ArrayList<>();
ArrayList keys = new ArrayList<>(map.keySet());
for (MetricKey key : keys) {
MetricValue value = map.remove(key);
if (value != null) {
metrics.add(new MetricEntry(key, value));
}
}
return metrics;
}
}