Spring Boot中的指标未在Prometheus中显示

时间:2018-09-10 17:49:46

标签: java spring spring-boot prometheus code-metrics

按照以下步骤将指标从Spring Boot发送到Prometheus:

注意:我已使用Docker映像在Mac上本地安装了Prometheus。

  1. 在pom.xml中添加了以下内容:

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-actuator</artifactId>
        <version>2.0.4.RELEASE</version>
    </dependency>
    <!-- Micormeter core dependecy  -->
    <dependency>
        <groupId>io.micrometer</groupId>
        <artifactId>micrometer-core</artifactId>
        <version>1.0.6</version>
    </dependency>
    <!-- Micrometer Prometheus registry  -->
    <dependency>
        <groupId>io.micrometer</groupId>
        <artifactId>micrometer-registry-prometheus</artifactId>
        <version>1.0.6</version>
    </dependency>
    
  2. 在application.properties中添加了以下内容:

server.port: 9000
management.server.port: 9001
management.server.address: 127.0.0.1

management.endpoint.metrics.enabled=true
management.endpoints.web.exposure.include=*
management.endpoint.prometheus.enabled=true
management.metrics.export.prometheus.enabled=true
  1. 在配置文件中以以下几行启动Prometheus:

全局配置

global:
  scrape_interval:     5s # Set the scrape interval to every 5 seconds.
  evaluation_interval: 5s # Evaluate rules every 5 seconds.
scrape_configs:
- job_name: 'hello-world-promethus'
  metrics_path: '/actuator/prometheus'
  static_configs:
  - targets: ['localhost:9001']

当我点击http://localhost:9001/actuator/prometheus时,我可以看到这些指标,但是在Prometheus UI上看不到它们。

我想念什么?

1 个答案:

答案 0 :(得分:0)

解决方案很简单。仅在运行Prometheus Docker容器时,您才会遇到此问题。目标已从``本地主机:9001''更改为``docker.for.mac.localhost:9001''。例如:

- job_name: hello-world-promethus
  scrape_interval: 5s
  scrape_timeout: 5s
  metrics_path: /actuator/prometheus
  scheme: http
  static_configs:
  - targets:
    - docker.for.mac.localhost:9001