当前位置: 首页 > 知识库问答 >
问题:

如何配置Prometheus.yml文件来收集Spring-Boot应用程序中的Prometheus度量标准?

仲孙俊贤
2023-03-14
@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);
    }

}
<?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>
# 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']

共有1个答案

马野
2023-03-14

在使用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");
    }
}

现在,如果您达到了endpoint/普罗米修斯,您应该得到度量标准

 类似资料:
  • 我有一个简单的Maven Spring-Boot应用程序(Java),并使用Prometheus从其中收集度量信息。我在pom文件中有所有必要的Prometheus依赖项,并且我在@SpringBootApplication类中包含了@EnablePrometheusEndpoint注释,但是当我运行应用程序并尝试访问localhost:8080/Prometheus(我认为这是Prometheu

  • 我的Spring Boot应用程序只是有一个计数器指标。我只是不知道如何将这些信息发送给普罗米修斯。我正在使用Maven(构建工具)和Spring Boot(Java)。

  • 我正在尝试使用以下自定义conf文件用docker加载prometheus:danilo@machine://prometheus-data/prometheus.yml:

  • 我正在尝试在我的Spring Boot(1.2.0.m1)应用程序中设置HikariCP,这样我就可以使用它来代替Tomcat DBCP进行测试。我想在application.properties文件中配置连接池,就像使用Tomcat一样,但我不知道应该怎么做。我找到的所有示例都显示JavaConfig样式,或者使用单独的HikariCP属性文件。有人能帮我找出属性名称来在application.

  • 我在我的spring boot应用程序中使用log4j2。这在所有方面都有效:不包括slf4j,包括log4j2等。 我确实发现了一个示例,建议在-classpath参数中为Java指定上述目录。但那也无济于事。 有人知道如何让Spring Boot应用程序读取log4j2.xml文件吗?

  • 问题内容: 我正在尝试在我的Spring Boot(1.2.0.M1)应用程序中设置HikariCP,以便可以测试使用它来代替Tomcat DBCP。我想像在使用Tomcat一样在我的application.properties文件中配置连接池,但是我不知道该怎么做。我发现的所有示例都显示JavaConfig样式,或使用单独的HikariCP属性文件。有人可以帮我找出属性名称以在applicati