我不知道你是否能够将 Spring Boot 与 Prometheus 集成,但是现在 Prometheus 官方client-java
项目中有一个专用的连接器。
项目的Github页面如下:simpleclient_spring_boot
您可以使用它为您添加以下依赖项pom.xml
<dependency>
<groupId>io.prometheus</groupId>
<artifactId>simpleclient_spring_boot</artifactId>
<version>0.0.17</version>
</dependency>
要使用它,请将 Spring Boot 配置添加到您的项目中,如下所示。
@Configuration
public class MetricsConfiguration {
@Bean
public ServletRegistrationBean servletRegistrationBean() {
DefaultExports.initialize();
return new ServletRegistrationBean(new MetricsServlet(), "/prometheus");
}
@Bean
public SpringBootMetricsCollector springBootMetricsCollector(Collection<PublicMetrics> publicMetrics) {
SpringBootMetricsCollector springBootMetricsCollector = new SpringBootMetricsCollector(
publicMetrics);
springBootMetricsCollector.register();
return springBootMetricsCollector;
}
}
现在,Spring Boot Actuator 公开的指标将作为 Prometheus Counters 和 Gauges 提供。
信息将发布到您的应用程序的路径/prometheus
。然后你必须指示 Prometheus 使用这些信息,配置如下。
# my global config
global:
scrape_interval: 15s # By default, scrape targets every 15 seconds.
evaluation_interval: 15s # By default, scrape targets every 15 seconds.
# A scrape configuration containing exactly one endpoint to scrape:
# Here it's Prometheus itself.
scrape_configs:
- job_name: 'your-application-name'
scrape_interval: 5s
metrics_path: '/prometheus'
static_configs:
- targets: ['localhost:8080']
如果您将浏览器指向/metrics
您将继续看到 Spring Boot 格式的信息。但是,将浏览器指向http://localhost:9090/graph
您将直接在 Prometheus 查询浏览器中查询此类信息。
试着看看这个Github pull-request。
更新
在simpleclient_spring_boot
0.0.18 的下一个版本中,将注解添加@EnablePrometheusEndpoint
到 Spring Boot 的配置类中就足以自动配置 Prometheus 适配器(看看这个测试)!
希望能帮助到你。