【问题标题】:How do you configure prometheus.yml file to collect Prometheus metrics in a Spring-Boot application?如何配置 prometheus.yml 文件以在 Spring-Boot 应用程序中收集 Prometheus 指标?
【发布时间】:2017-12-01 04:39:36
【问题描述】:

我有一个简单的 Maven Spring-Boot 应用程序 (Java),并且正在使用 Prometheus 从中收集指标信息。我的 pom 文件中有所有必要的 Prometheus 依赖项,并且我在我的 @SpringBootApplication 类中包含了 @EnablePrometheusEndpoint 注释,并在我的 prometheus.yml 文件中包含了“metrics_path:'/prometheus'”(如下所示),但是当我尝试访问指标时收到 406 错误(在 localhost:8080/prometheus 上)。我应该如何修改我的 prometheus.yml 文件以查看我导出的指标?

以下是我要检测的所有(简单)spring boot 代码:

@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 文件,包含所有必需的 prometheus 依赖项:

<?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 文件。注意,最后几行是我指定普罗米修斯刮什么的地方,我怀疑这是我的问题所在:

# 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

    metrics_path: '/prometheus'

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

【问题讨论】:

    标签: java maven spring-boot prometheus


    【解决方案1】:

    在使用 Spring boot 时,可以使用以下 Prometheus maven 依赖项

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

    删除 @EnablePrometheusEndpoint 并创建以下类以启用传播指标

    @Configuration
    public class MonitoringConfig {
    
        @Bean
        SpringBootMetricsCollector springBootMetricsCollector(Collection<PublicMetrics> publicMetrics) {
    
            SpringBootMetricsCollector springBootMetricsCollector = new SpringBootMetricsCollector(publicMetrics);
            springBootMetricsCollector.register();
    
            return springBootMetricsCollector;
        }
    
        @Bean
        ServletRegistrationBean servletRegistrationBean() {
            DefaultExports.initialize();
            return new ServletRegistrationBean(new MetricsServlet(), "/prometheus");
        }
    }
    

    现在,如果您点击端点 /prometheus,您应该会获得指标

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-10-31
      • 2019-11-03
      • 2018-05-14
      • 1970-01-01
      • 1970-01-01
      • 2015-04-22
      • 1970-01-01
      • 2021-06-23
      相关资源
      最近更新 更多