【问题标题】:How to expose metrics to Prometheus from a Java (Spring boot) application如何从 Java (Spring boot) 应用程序向 Prometheus 公开指标
【发布时间】:2017-11-29 18:22:56
【问题描述】:

我的 Spring-Boot 应用程序只有一个计数器指标。我只是不知道如何将此信息发送给 Prometheus。我正在使用 Maven(构建工具)和 Spring Boot (Java)。

【问题讨论】:

    标签: maven spring-boot instrumentation grafana prometheus


    【解决方案1】:

    Prometheus 和 Graphite 一样,是一个时间序列存储引擎。

    然后,Grafana 可以查询 Prometheus 以生成图形和警报。

    https://prometheus.io/docs/introduction/faq/

    正如文档所引用的,Prometheus 与其他指标存储系统不同,它使用(有争议的)“拉动”模型。

    这意味着必须下载/安装一个(独立的)Prometheus 服务器。然后,此服务器会定期向应用服务器列表(例如 Java SpringBoot 服务器)发出 HTTP GET 请求(拉取)以获取(内存中)存储的指标。

    参考:https://prometheus.io/docs/introduction/faq/#why-do-you-pull-rather-than-push?

    因此,(Spring Boot)应用程序必须公开 Prometheus 服务器可以从中提取的指标端点(默认为 /metrics)。

    参考:https://github.com/prometheus/client_java

    因此,Google 上有很多可用的文档,但那是(可以说是令人费解的)拓扑结构 - 以及 SoundCloud 和 Prometheus 人员关于为什么“拉”模型优于“推”模型的论点,因为所有其他度量框架都采用.

    【讨论】:

      【解决方案2】:

      对于集成 Prometheus,在您的 POM.XML 中添加以下依赖项

      <dependency>
          <groupId>io.prometheus</groupId>
          <artifactId>simpleclient_spring_boot</artifactId>
          <version>0.1.0</version>
      </dependency>
      <dependency>
          <groupId>io.prometheus</groupId>
          <artifactId>simpleclient_servlet</artifactId>
          <version>0.1.0</version>
      </dependency>
      <dependency>
          <groupId>io.prometheus</groupId>
          <artifactId>simpleclient_hotspot</artifactId>
          <version>0.1.0</version>
      </dependency>
      

      在您的 SpringBoot 应用程序类中,添加 Annonation @EnablePrometheusEndpoint

      在你的控制器中,你可以定义一个自定义计数器

      private static final Counter myCounter = Counter.build()
              .name("CounterName")
              .labelNames("status")
              .help("Counter desc").register();
      

      在您的服务中,您可以为您的计数器设置适当的逻辑,该逻辑将由 Prometheus 自动拉取

      @RequestMapping("/myService")
          public void endpoint() {
                 String processingResult = yourLogic(myCounter);
                  myCounter.labels("label1",processingResult).inc();
                  }
      

      【讨论】:

      • 注意 Spring Boot 2.0 不需要简单客户端
      • 请注意,几乎没有关于指标在 JVM 中的位置(在内存中)的信息,而时间序列指标会随着时间的推移添加,直到 Prometheus 服务器获取数据。对于像简单求和计数器这样的非时间序列数据,这不是问题,但没有记录是否放弃指标、JVM 是否会崩溃等。
      【解决方案3】:

      Spring Boot 2.0.0

      检查您的 Spring Boot 属性文件以确保启用指标和 Prometheus 导出。

      例如:

      management.endpoints.web.exposure.include=*
      management.endpoint.metrics.enabled=true
      management.endpoint.prometheus.enabled=true
      management.metrics.export.prometheus.enabled=true
      

      【讨论】:

        【解决方案4】:

        从 spring-boot 的角度来看我的几分钱 -

        build.gradle

        compile 'io.prometheus:simpleclient_servlet:0.3.0'
        compile('io.prometheus:simpleclient_hotspot:0.3.0')
        compile('io.prometheus:simpleclient_spring_boot:0.3.0')
        

        您的包含 ma​​in 函数的类应具有以下注释

        @EnableSpringBootMetricsCollector
        @EnablePrometheusEndpoint
        ...
        ...
        @Bean
        public boolean initializePrometheusEssentials() {
            return prometheusUtility.initializePrometheusEssentials();
        }
        

        PometheusUtility.java

        import io.prometheus.client.Counter;
        import org.springframework.stereotype.Component;
        import java.util.HashMap;
        import java.util.Map;
        
        @Component("PrometheusUtility")
        public class PrometheusUtility {
        
        private PrometheusUtility(){
        
        }
        
        private final static Map<String,Counter> counters = new HashMap<>();
        
        public boolean initializePrometheusEssentials() {
        counters.put("SAMPLE-COUNTER",Counter.build()
                    .name("SAMPLE COUNTER")
                    .help("Records the Sample Count").register());
                    return true;
        }
        
        
        public static void incrementCounter(String counterName){
            counters.get(counterName).inc();
        }
        }
        

        使用 incrementCounter 方法来增加它。

        并确保将以下属性添加到您的 env 文件中

        # Prometheus settings
        #change prometheus endpoint to metrics
        endpoints.prometheus.id=
        #change actuator endpoint to springmetrics
        endpoints.metrics.id=
        endpoints.metrics.sensitive=
        endpoints.metrics.enabled=
        endpoints.prometheus.sensitive=
        

        【讨论】:

          猜你喜欢
          • 2020-10-06
          • 2020-08-18
          • 2018-09-16
          • 1970-01-01
          • 2021-07-22
          • 2018-05-14
          • 2019-08-09
          • 2022-06-20
          • 2022-06-17
          相关资源
          最近更新 更多