英文:
How to implement test to check that actuator metrics are exist ? 404 for /actuator/prometheus in tests
问题
I have a spring boot application (version 3.0.4):
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>3.0.4</version>
<relativePath/>
</parent>
In application.yaml
of the application (src/main/resources
), I have the following configuration:
...
management:
server:
port: 8081
endpoints:
web:
exposure:
include: health, prometheus, info
endpoint:
info:
enabled: true
health:
enabled: true
probes:
enabled: true
show-details: always
prometheus:
enabled: true
...
When I run the application and access the URL localhost:8081/actuator/prometheus
in my browser, I see a long list of metrics, which is expected.
Now I want to implement a test to ensure that Prometheus metrics are available:
@Slf4j
@SpringBootTest(webEnvironment = RANDOM_PORT)
public class ActuatorTests {
@Autowired
protected TestRestTemplate testRestTemplate;
@LocalManagementPort
private int managementPort;
@Test
public void test1() {
ResponseEntity<String> forEntity = testRestTemplate.getForEntity("/actuator/prometheus", String.class);
assertEquals(HttpStatus.OK, forEntity.getStatusCode());
}
@Test
public void test2() {
ResponseEntity<String> actuatorResponse = testRestTemplate.getForEntity("http://localhost:" + managementPort + "/actuator/prometheus", String.class);
assertEquals(HttpStatus.OK, actuatorResponse.getStatusCode());
}
}
Both tests fail with the same error:
org.opentest4j.AssertionFailedError:
Expected :200 OK
Actual :404 NOT_FOUND
How can I fix it?
P.S.
I also tried to copy this test as is: Stack Overflow Answer
But the result is:
org.springframework.web.client.HttpClientErrorException$NotFound: 404 : {"timestamp":"2023-05-31T22:09:49.535+00:00","status":404,"error":"Not Found","path":"/actuator/metrics"}
如何修复此问题?
英文:
I have a spring boot appication(version is 3.0.4):
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>3.0.4</version>
<relativePath/>
</parent>
In application.yaml
of application(src/main/resources
) I have following config:
....
management:
server:
port: 8081
endpoints:
web:
exposure:
include: health,prometheus,info
endpoint:
info:
enabled: true
health:
enabled: true
probes:
enabled: true
show-details: always
prometheus:
enabled: true
...
When I run application and access url localhost:8081/actuator/prometheus
in my browser I see a long list of metrics and it is expected
Now I want to implement test to make sure that prometheus metrics are available:
@Slf4j
@SpringBootTest(webEnvironment = RANDOM_PORT)
public class ActuatorTests {
@Autowired
protected TestRestTemplate testRestTemplate;
@LocalManagementPort
private int managementPort;
@Test
public void test1() {
ResponseEntity<String> forEntity = testRestTemplate.getForEntity("/actuator/prometheus", String.class);
assertEquals(HttpStatus.OK, forEntity.getStatusCode());
}
@Test
public void test2() {
ResponseEntity<String> actuatorResponse = testRestTemplate.getForEntity("http://localhost:" + managementPort + "/actuator/prometheus", String.class);
assertEquals(HttpStatus.OK, actuatorResponse.getStatusCode());
}
}
Both tests are failed with the same error:
org.opentest4j.AssertionFailedError:
Expected :200 OK
Actual :404 NOT_FOUND
How can I fix it ?
P.S.
ALso I tried to copy this test as is: https://stackoverflow.com/a/75334796/2674303
but result is:
org.springframework.web.client.HttpClientErrorException$NotFound: 404 : "{"timestamp":"2023-05-31T22:09:49.535+00:00","status":404,"error":"Not Found","path":"/actuator/metrics"}"
答案1
得分: 0
以下是已翻译的内容:
这个测试配置中的配置解决了这个问题:
management:
prometheus:
metrics.export.enabled: true
这个选项在应用程序中不需要(因为默认值是true)。
package org.springframework.boot.actuate.autoconfigure.metrics.export.prometheus;
...
@ConfigurationProperties(prefix = "management.prometheus.metrics.export")
public class PrometheusProperties {
/**
* 是否启用将指标导出到此后端。
*/
private boolean enabled = true;
...
我找不到一个地方,但似乎一些Spring Boot测试启动器覆盖了这个默认值。不确定当我需要更改配置以使其工作时,这个测试是否有价值。
英文:
This config in test configuration fixes the issue:
management:
prometheus:
metrics.export.enabled: true
The tricky thing that this option is not needed in applicaton(because default value is true).
package org.springframework.boot.actuate.autoconfigure.metrics.export.prometheus;
...
@ConfigurationProperties(prefix = "management.prometheus.metrics.export")
public class PrometheusProperties {
/**
* Whether exporting of metrics to this backend is enabled.
*/
private boolean enabled = true;
...
I wasn't able to find a place but looks like some spring boot test starter overwrite this default value. Not sure if this test has a value in case when I need to change configuration to make it working.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论