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"}"
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.