io.prometheus.client.spring.boot.SpringBootMetricsCollector Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of simpleclient_spring_boot Show documentation
Show all versions of simpleclient_spring_boot Show documentation
Collect information from Spring Boot actuator.
package io.prometheus.client.spring.boot;
import io.prometheus.client.Collector;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.actuate.endpoint.PublicMetrics;
import org.springframework.boot.actuate.metrics.Metric;
import org.springframework.stereotype.Component;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
/**
* Spring boot metrics integration for Prometheus exporter.
*
* {@literal @}Bean
* public SpringBootMetricsCollector springBootMetricsCollector(Collection{@literal <}PublicMetrics{@literal >} publicMetrics) {
* SpringBootMetricsCollector springBootMetricsCollector = new SpringBootMetricsCollector(publicMetrics);
* springBootMetricsCollector.register();
* return springBootMetricsCollector;
* }
*
*/
@Component
public class SpringBootMetricsCollector extends Collector implements Collector.Describable {
private final Collection publicMetrics;
@Autowired
public SpringBootMetricsCollector(Collection publicMetrics) {
this.publicMetrics = publicMetrics;
}
@Override
public List collect() {
ArrayList samples = new ArrayList();
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, new ArrayList(), new ArrayList(), value)));
samples.add(metricFamilySamples);
}
}
return samples;
}
@Override
public List describe() {
return new ArrayList();
}
}