
de.marcoaust.prometheus.SpringBootMetricsCollector Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of spring-boot-prometheus Show documentation
Show all versions of spring-boot-prometheus Show documentation
Spring-Boot-Prometheus makes it easy to use Prometheus with Spring Boot. It exposes an /prometheus
endpoint with prometheus-compatible metrics just by adding @EnablePrometheusEndpoint to your configuration.
The newest version!
package de.marcoaust.prometheus;
import java.util.*;
import org.springframework.boot.actuate.endpoint.PublicMetrics;
import org.springframework.boot.actuate.metrics.Metric;
import io.prometheus.client.Collector;
class SpringBootMetricsCollector extends Collector {
private final Collection publicMetrics;
SpringBootMetricsCollector(Collection publicMetrics) {
this.publicMetrics = publicMetrics;
}
@Override
public List collect() {
Map samples = new HashMap();
for (PublicMetrics publicMetrics : this.publicMetrics) {
for (Metric> metric : publicMetrics.metrics()) {
String name = Collector.sanitizeMetricName(metric.getName());
double value = metric.getValue().doubleValue();
MetricFamilySamples metricFamilySamples = new MetricFamilySamples(
name, Type.GAUGE, name, Collections.singletonList(new MetricFamilySamples.Sample(
name, Collections.emptyList(), Collections.emptyList(), value)));
samples.put(name, metricFamilySamples);
}
}
return new ArrayList(samples.values());
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy