Maven 如何将Spring引导应用程序中的度量导出到Prometheus中?
我有一个简单的Maven Spring启动应用程序(Java),正在使用Prometheus从中收集度量信息。我的pom文件中包含了所有必要的Prometheus依赖项,并且在@SpringBootApplication类中包含了@EnablePrometheUseEndpoint注释,但是当我运行应用程序并尝试访问localhost:8080/Prometheus上的度量时(我认为这是Prometheus发送检测度量的默认端点?),我得到一个401错误。我是否正确插入应用程序,以便Prometheus可以使用@EnablePrometheUseEndpoint注释收集度量值?普罗米修斯在哪里显示我的仪表化指标(是否在localhost:8080/Prometheus中)?我还尝试在localhost中查找这些指标:8080/metrics,但运气不好。非常感谢您的帮助Maven 如何将Spring引导应用程序中的度量导出到Prometheus中?,maven,spring-boot,instrumentation,grafana,prometheus,Maven,Spring Boot,Instrumentation,Grafana,Prometheus,我有一个简单的Maven Spring启动应用程序(Java),正在使用Prometheus从中收集度量信息。我的pom文件中包含了所有必要的Prometheus依赖项,并且在@SpringBootApplication类中包含了@EnablePrometheUseEndpoint注释,但是当我运行应用程序并尝试访问localhost:8080/Prometheus上的度量时(我认为这是Prometheus发送检测度量的默认端点?),我得到一个401错误。我是否正确插入应用程序,以便Promet
@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>
4.0.0
com.example
您的普罗米修斯被配置为在http://localhost:8080/metrics
(默认值),而@enablePrometheUseEndpoint
在http://localhost:8080/prometheus
因此,您应该在prometheus.yml
中将metrics\u path
设置为prometheus
401之所以发生,是因为默认情况下,Spring Boot Actuator的指标端点受到保护。尝试在应用程序中添加endpoints.prometheus.sensitive:false
。属性或清单。这将使您的普罗米修斯端点不受保护,并可用于刮取。要将自定义度量发布到普罗米修斯,您需要使用普罗米修斯推送网关
在这两个链接上可以找到更多文档
我在我的prometheus.yml文件中添加了“metrics\u path:/prometheus”(在-job\u name:“hello\u world\u spring\u boot”标签下),但仍然看到相同的401错误。在我的prometheus.yml文件中是否还有其他需要更改的内容?另外,当我将配置更改为“metrics\u path:”/prometheus'(在/prometheus文件中添加单引号)我得到的是406错误,而不是401错误。
# 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']