All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.prometheus.client.spring.boot.SpringBootMetricsCollector Maven / Gradle / Ivy

There is a newer version: 0.16.0
Show newest version
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(); } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy