如何从Spring Boot Singleton收集Prometheus度量信息

时间:2018-01-05 21:57:56

标签: java spring spring-boot metrics prometheus

我想使用Prometheus来监控我的Java Spring-Boot应用程序。在声明了Prometheus度量标准(本例中的摘要)并使用它时,我已成功收集了Prometheus度量标准,并在使用preventDefault()注释的同一个类中。例如:

@SpringBootApplication

但是,如果我将Prometheus @SpringBootApplication @RestController public class Application{ /* Prometheus Metric*/ private static final Summary summary = Summary.build() .name("api_latency") .help("elapsed time in call to api") .register(); /* Prometheus Metric*/ private static SimpleTimer timer; @RequestMapping("/") public void home(){ timer = new SimpleTimer(); //a call to an API goes here summary.observe(timer.elapsedSeconds()); } public static void main(String[] args){ SpringApplication.run(Application.class, args); } } 声明移出Summary类,并进入注释为@SpringBootApplication的类,Prometheus似乎无法再检测到@Component度量。

示例:

Summary

如何让Prometheus从单身内部检测我的@SpringBootApplication @EnableScheduling public class Application{ /*Code that invokes SomeClass goes here.*/ public static void main(String[] args){ SpringApplication.run(Application.class, args); } } @Component public class SomeClass { /* Prometheus Metric*/ private static final Summary summary = Summary.build() .name("api_latency") .help("elapsed time in call to api") .register(); /* Prometheus Metric*/ private static SimpleTimer timer; . . . @Scheduled(cron = "${scheduled.job.time}") public void run(){ timer = new SimpleTimer(); //an API call goes here summary.observe(timer.elapsedSeconds()) } } 指标数据?

1 个答案:

答案 0 :(得分:0)

如果Someclass.javaApplication.java不在同一个包或子包中,则默认情况下spring不会扫描它。

您可能需要明确调出您要扫描的课程

@SpringBootApplication(scanBasePackageClasses={SomeClass.class})
@EnableScheduling
public class Application { 
}