在springboot的管理端口使用prometheus metrics servlet

Dan*_*ela 2 java spring-boot prometheus

我正在使用prometheus metric servlet来公开我的指标,使用java客户端api prometheus供应.

我注册servlet的方式与注册任何servelt相同,见下文:

 @Bean
public ServletRegistrationBean registerPrometheusExporterServlet(CollectorRegistry metricRegistry) {
    return new ServletRegistrationBean(new MetricsServlet(metricRegistry), "/metrics");
}
Run Code Online (Sandbox Code Playgroud)

但是,我想将此servlet添加到管理端口,或者prometheus版本是否可能替换springboot的默认/ metrics服务.可以这样做吗?如何?

谢谢,丹妮拉

ric*_*din 5

我不知道你是否能够将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>
Run Code Online (Sandbox Code Playgroud)

要使用它,请将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;
    }
}
Run Code Online (Sandbox Code Playgroud)

现在,Spring Boot Actuator公开的指标将作为Prometheus计数器和仪表提供.

信息将发布到/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']
Run Code Online (Sandbox Code Playgroud)

如果您指向浏览器,/metrics您将继续以Spring Boot格式查看信息.但是,将浏览器指向http://localhost:9090/graph您将直接将此类信息查询到Prometheus查询浏览器中.

试着看看这个 Github拉请求.

更新
simpleclient_spring_boot0.0.18 的下一个版本中,将注释添加@EnablePrometheusEndpoint到Spring Boot的配置类以自动配置Prometheus适配器(看看此测试)就足够了!

希望能帮助到你.