36 lines
1.3 KiB
Java
36 lines
1.3 KiB
Java
package eu.dnetlib.common.metrics;
|
|
|
|
import java.io.IOException;
|
|
import java.io.StringWriter;
|
|
import java.io.Writer;
|
|
import java.util.Set;
|
|
|
|
import io.micrometer.core.instrument.composite.CompositeMeterRegistry;
|
|
import io.prometheus.client.CollectorRegistry;
|
|
import io.prometheus.client.Gauge;
|
|
import io.prometheus.client.exporter.common.TextFormat;
|
|
|
|
public class MetricUtils {
|
|
|
|
private static final CompositeMeterRegistry meterRegistry = new CompositeMeterRegistry();
|
|
private static final CollectorRegistry collectorRegistry = new CollectorRegistry(true);
|
|
|
|
public static MetricInfo register(final String name, final MetricInfo metric) {
|
|
final MetricInfo meter = meterRegistry.gauge(name, metric, MetricInfo::obtainValue);
|
|
Gauge.build(name, name).register(collectorRegistry);
|
|
return meter;
|
|
}
|
|
|
|
public static String output(final Set<String> metricsToInclude, final String contentType) {
|
|
try {
|
|
final Writer writer = new StringWriter();
|
|
TextFormat.writeFormat(contentType, writer, collectorRegistry.filteredMetricFamilySamples(metricsToInclude));
|
|
return writer.toString();
|
|
} catch (final IOException e) {
|
|
// This actually never happens since StringWriter::write() doesn't throw any IOException
|
|
throw new RuntimeException("Writing metrics failed", e);
|
|
}
|
|
}
|
|
|
|
}
|