2

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

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

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

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

谢谢,丹妮拉

4

1 回答 1

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>

要使用它,请将 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_boot0.0.18 的下一个版本中,将注解添加@EnablePrometheusEndpoint到 Spring Boot 的配置类中就足以自动配置 Prometheus 适配器(看看这个测试)!

希望能帮助到你。

于 2016-10-19T13:13:43.730 回答