import io.prometheus.client.exporter.MetricsServlet;
import io.prometheus.client.hotspot.DefaultExports;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class AppConfig {
@Bean
public ServletRegistrationBean servletRegistrationBean(){
DefaultExports.initialize();
return new ServletRegistrationBean(new MetricsServlet(), "/metrics"); //第二个参数可以自定义,但是不可省略
}
}
3.自定义监控指标
name方法定义指标名
help方法定义指标描述
import io.prometheus.client.Counter;
import io.prometheus.client.Gauge;
//Counter
static final Counter counter = Counter.build()
.name("my_counter_total")
.help("A counter used to describe the debt")
.register();
counter.inc();
//Gauge
static final Gauge gauge = Gauge.build()
.name("my_gauge")
.help("A gauge to describe the rest of the money")
.register();
gauge.inc();
gauge.dec();