【问题标题】:How do you export metrics from a Spring Boot application into Prometheus?如何将 Spring Boot 应用程序中的指标导出到 Prometheus?
【发布时间】:2018-05-14 17:59:31
【问题描述】:

我有一个简单的 Maven Spring-Boot 应用程序 (Java),并且正在使用 Prometheus 从中收集指标信息。我的 pom 文件中有所有必要的 Prometheus 依赖项,并且我在 @SpringBootApplication 类中包含了 @EnablePrometheusEndpoint 注释,但是当我运行我的应用程序并尝试访问 localhost:8080/prometheus 上的指标时(我认为是Prometheus 发送检测指标的默认端点?),我收到 401 错误。我是否正确地检测了我的应用程序,以便 Prometheus 可以使用 @EnablePrometheusEndpoint 注释收集指标? Prometheus 在哪里显示我的检测指标(在 localhost:8080/prometheus 中)?我也尝试在 localhost:8080/metrics 中查找这些指标,但没有运气。非常感谢任何帮助。

@SpringBootApplication
@RestController
@EnablePrometheusEndpoint
public class Example {

    //Just a logger that keeps track of relevant information:
    private static final Logger LOGGER = Logger.getLogger(Example.class.getName());

    //counter for counting how many times an endpoint has been hit
    static final Counter myCounter = Counter.build()    
                                              .name("CounterName") //note: by convention, counters should have "_total" suffix
                                              .help("Total requests recorded by a specific endpoint")
                                              .labelNames("status")
                                              .register();
    @RequestMapping("/hello")
    String hello() {

        myCounter.labels("customLabel1").inc(); //increment the number of requests by one
        LOGGER.log(Level.INFO, "Number of times /hello has been hit: " + myCounter.labels("customLabel1").get());

        return "Hello world! This is an example response!";
    }

    @RequestMapping("/homepage")
    String homePage() {

        myCounter.labels("customLabel2").inc(); //increment the number of requests by one
        LOGGER.log(Level.INFO, "Number of times /homepage has been hit: " + myCounter.labels("customLabel2").get());

        return "this is the home page!!";
    }


    public static void main(String[] args) throws Exception {
        SpringApplication.run(Example.class, args);
    }

}

下面是我的 pom.xml 文件:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.example</groupId>
    <artifactId>springboot</artifactId>
    <version>0.0.1-SNAPSHOT</version>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.5.8.RELEASE</version>
    </parent>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>


        <!-- Prometheus dependencies -->
        <!-- The client -->
        <dependency>
            <groupId>io.prometheus</groupId>
            <artifactId>simpleclient</artifactId>
            <version>0.1.0</version>
        </dependency>

        <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>
        <!-- Hotspot JVM metrics -->
        <dependency>
            <groupId>io.prometheus</groupId>
            <artifactId>simpleclient_hotspot</artifactId>
            <version>0.1.0</version>
        </dependency>
        <!-- Exposition HTTPServer -->
        <dependency>
            <groupId>io.prometheus</groupId>
            <artifactId>simpleclient_httpserver</artifactId>
            <version>0.1.0</version>
        </dependency>
        <!-- Pushgateway exposition -->
        <dependency>
            <groupId>io.prometheus</groupId>
            <artifactId>simpleclient_pushgateway</artifactId>
            <version>0.1.0</version>
        </dependency>

        <!-- Spring Boot Actuator for exposing metrics -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
            <version>1.5.8.RELEASE</version>
        </dependency>


    </dependencies>


</project>

下面是我的 prometheus.yml 文件,设置(我认为)在最后几行中在 localhost:8080 上抓取我的 spring-boot 应用程序:

# my global config
global:
  scrape_interval:     15s # Set the scrape interval to every 15 seconds. Default is every 1 minute.
  evaluation_interval: 15s # Evaluate rules every 15 seconds. The default is every 1 minute.
  # scrape_timeout is set to the global default (10s).

  # Attach these labels to any time series or alerts when communicating with
  # external systems (federation, remote storage, Alertmanager).
  external_labels:
      monitor: 'codelab-monitor'

# Load rules once and periodically evaluate them according to the global 'evaluation_interval'.
rule_files:
  # - "first.rules"
  # - "second.rules"

# A scrape configuration containing exactly one endpoint to scrape:
# Here it's Prometheus itself.
scrape_configs:
  # The job name is added as a label `job=<job_name>` to any timeseries scraped from this config.
  - job_name: 'prometheus'

    # metrics_path defaults to '/metrics'
    # scheme defaults to 'http'.
    scrape_interval: 5s

    static_configs:
      - targets: ['localhost:9090']

#The following lines are meant to monitor my  spring boot app
  - job_name: 'hello_world_spring_boot'
    scrape_interval: 5s

    static_configs:
      - targets: ['localhost:8080']

这是我查看我的 prometheus 仪表板时看到的,在 localhost:9090 上运行

Prometheus dashboard status 401 message

【问题讨论】:

    标签: maven spring-boot instrumentation grafana prometheus


    【解决方案1】:

    您的 Prometheus 配置为在 http://localhost:8080/metrics(默认)处查找指标,而 @EnablePrometheusEndpointhttp://localhost:8080/prometheus 处公开指标。

    因此,您应该在您的prometheus.yml 中将metrics_path 设置为prometheus

    发生 401,因为默认情况下 Spring Boot Actuator 的 metrics 端点受到保护。

    【讨论】:

    • 我在我的 prometheus.yml 文件中添加了“metrics_path: /prometheus”(在标签“-job_name: 'hello_world_spring_boot'”下,但我仍然看到相同的 401 错误。我还有什么可能必须更改我的 prometheus.yml 文件吗?
    • 另外,当我将配置更改为“metrics_path: '/prometheus'”(向 /prometheus 添加单引号)时,我得到的是 406 错误而不是 401 错误。
    【解决方案2】:

    尝试在您的 application.properties 或清单中添加 endpoints.prometheus.sensitive: false。这将使您的 prometheus 端点不受保护,并可用于抓取。

    【讨论】:

      【解决方案3】:

      要将自定义指标发布到 Prometheus,您需要使用 Prometheus Pushgateway

      可以在这两个链接上找到更多文档

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-10-06
        • 2018-01-07
        • 2021-06-23
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多