eu.xenit.alfred.telemetry.util.MeterRegistryUtil Maven / Gradle / Ivy
package eu.xenit.alfred.telemetry.util;
import io.micrometer.core.instrument.Meter;
import io.micrometer.core.instrument.MeterRegistry;
import io.micrometer.core.instrument.Statistic;
import io.micrometer.core.instrument.Tag;
import io.micrometer.core.instrument.composite.CompositeMeterRegistry;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Set;
import java.util.function.BiFunction;
public class MeterRegistryUtil {
public static Collection findFirstMatchingMeters(MeterRegistry registry, String name, Iterable tags) {
if (registry instanceof CompositeMeterRegistry) {
return findFirstMatchingMeters((CompositeMeterRegistry) registry, name, tags);
}
return registry.find(name).tags(tags).meters();
}
public static Map getSamples(Collection meters) {
Map samples = new LinkedHashMap<>();
meters.forEach((meter) -> mergeMeasurements(samples, meter));
return samples;
}
public static Map> getAvailableTags(Collection meters) {
Map> availableTags = new HashMap<>();
meters.forEach((meter) -> mergeAvailableTags(availableTags, meter));
return availableTags;
}
/* PRIVATE METHODS */
private static Collection findFirstMatchingMeters(CompositeMeterRegistry composite, String name,
Iterable tags) {
return composite.getRegistries().stream().map((registry) -> findFirstMatchingMeters(registry, name, tags))
.filter((matching) -> !matching.isEmpty()).findFirst().orElse(Collections.emptyList());
}
private static void mergeMeasurements(Map samples, Meter meter) {
meter.measure().forEach((measurement) -> samples.merge(measurement.getStatistic(), measurement.getValue(),
mergeFunction(measurement.getStatistic())));
}
private static BiFunction mergeFunction(Statistic statistic) {
return Statistic.MAX.equals(statistic) ? Double::max : Double::sum;
}
private static void mergeAvailableTags(Map> availableTags, Meter meter) {
meter.getId().getTags().forEach((tag) -> {
Set value = Collections.singleton(tag.getValue());
availableTags.merge(tag.getKey(), value, MeterRegistryUtil::merge);
});
}
private static Set merge(Set set1, Set set2) {
Set result = new HashSet<>(set1.size() + set2.size());
result.addAll(set1);
result.addAll(set2);
return result;
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy