我想使用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())
}
}
指标数据?
答案 0 :(得分:0)
如果Someclass.java
与Application.java
不在同一个包或子包中,则默认情况下spring不会扫描它。
您可能需要明确调出您要扫描的课程
@SpringBootApplication(scanBasePackageClasses={SomeClass.class})
@EnableScheduling
public class Application {
}