Mas*_*ile 17 java metrics spring-boot-actuator spring-boot-test spring-micrometer
我正在使用 springboot 2.4.0,并添加了以下依赖项以启用 prometheus 指标:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>io.micrometer</groupId>
<artifactId>micrometer-registry-prometheus</artifactId>
</dependency>
Run Code Online (Sandbox Code Playgroud)
然后在我的 application.properties 我有以下属性
management.endpoints.web.exposure.include=*
management.metrics.enable.all=true
Run Code Online (Sandbox Code Playgroud)
我正在尝试运行一个简单的集成测试,以查看我的自定义指标出现在 /actuator/prometheus 端点上。代码下方
package com.example.demo;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.web.server.LocalServerPort;
import static io.restassured.RestAssured.given;
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
public class IntegrationTest {
@LocalServerPort
private int port;
private String baseUrl;
@BeforeEach
public void setup() {
baseUrl = "http://localhost:" + port;
}
@Test
public void metricsEndpoint() throws Exception {
given().when().get(baseUrl + "/demo/actuator/prometheus")
.then()
.statusCode(200);
}
}
Run Code Online (Sandbox Code Playgroud)
我在这里得到的错误是
java.lang.AssertionError: 1 expectation failed.
Expected status code <200> but was <404>.
Run Code Online (Sandbox Code Playgroud)
而如果我对 springboot 执行器提供的任何其他端点重复相同的请求,我会正确地得到响应,例如我尝试了 /actuator/health、/actuator/info、/actuator/metrics 等。
这仅在使用 @Springboot 注释的集成测试期间发生,这很奇怪,因为如果我运行我的应用程序并使用邮递员向地址 localhost:8080/actuator/prometheus 发出请求,我会正确地得到响应。
这就像在测试期间未加载 prometheus 注册表一样。
任何人都可以帮忙吗?
提前致谢。
编辑:解决方案是 Johannes Klug 建议的解决方案。添加注释@AutoConfigureMetrics 解决了我的问题
小智 34
我遇到了同样的问题。在通过 spring-context ConditionEvaluator 进行一些跟踪后,我发现新引入的@ConditionalOnEnabledMetricsExport("prometheus")条件 onPrometheusMetricsExportAutoConfiguration阻止了端点加载。
这是由于https://github.com/spring-projects/spring-boot/pull/21658 的预期行为并影响 spring-boot 2.4.x
修复:将 @AutoConfigureMetrics 添加到您的测试中
@AutoConfigureMetrics
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
public class IntegrationTest {
Run Code Online (Sandbox Code Playgroud)
Spring Boot 2.4.1 也发生在我身上
我唯一能想到的是 @SpringBootTest 中包含的端点正在使用 @Endpoint 注释。
例如:
@Endpoint(id = "metrics")
public class MetricsEndpoint {}
@Endpoint(id = "loggers")
public class LoggersEndpoint {}
Run Code Online (Sandbox Code Playgroud)
并且 prometheus 端点正在使用 @WebEndpoint 注释
@WebEndpoint(id = "prometheus")
public class PrometheusScrapeEndpoint {}
Run Code Online (Sandbox Code Playgroud)
我不知道这是否应该如何工作,或者是一个可能的错误
| 归档时间: |
|
| 查看次数: |
1508 次 |
| 最近记录: |