【问题标题】:Use prometheus metrics servlet in management port of springboot在spring boot的管理端口中使用prometheus metrics servlet
【发布时间】:2016-04-22 14:16:47
【问题描述】:

我正在使用 prometheus 度量 servlet 来公开我的度量,并提供 java 客户端 api prometheus。

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

 @Bean
public ServletRegistrationBean registerPrometheusExporterServlet(CollectorRegistry metricRegistry) {
    return new ServletRegistrationBean(new MetricsServlet(metricRegistry), "/metrics");
}

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

谢谢, 丹妮拉

【问题讨论】:

    标签: java spring-boot prometheus


    【解决方案1】:

    我不知道你是否能够将 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 查询浏览器中查询此类信息。

    试试看this Github pull-request。

    更新
    simpleclient_spring_boot 的下一个版本 0.0.18 中,将注解 @EnablePrometheusEndpoint 添加到 Spring Boot 的配置类中就足以自动配置 Prometheus 适配器(看看这个test)!

    希望对你有帮助。

    【讨论】:

    • 我做了这个并且它有效,但是在一个工作区中我得到了这个构造函数 ServletRegistrationBean(MetricsServlet, String) 是未定义的,我不知道如何解决 ti。这是什么原因造成的?
    • 忘记配置类。在您的应用程序中使用注释 @EnablePrometheusEndpoint 直接从 Boot 启用 Prometheus :)
    • 感谢您的快速响应。我这样做了,现在在哪条路线上查看指标?
    • 通过配置,我可以设置指标 servlet 和 url (/prometheus),我可以在那里看到指标。使用此注释可以在哪里查看指标。我正在访问 /prometheus,我得到:未找到
    • 对不起,已经过去了很多时间。你在/metrics 下看到了什么吗?
    猜你喜欢
    • 1970-01-01
    • 2021-05-07
    • 2015-09-17
    • 1970-01-01
    • 2019-04-17
    • 2021-07-13
    • 2018-12-05
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多