javaspring-bootprometheusactuator

Spring Boot /actuator/prometheus is not available during integration test


The /actuator/prometheus endpoint is available and working when the application starts normally, but it's missing during the integration test when I run my test class annotated with @SpringBootTest, while /health is available in both cases.

I see the logs clearly say exposing 2 endpoints in normal run:

o.s.b.a.e.web.EndpointLinksResolver      : Exposing 2 endpoints beneath base path '/actuator'

And only 1 endpoint during test:

o.s.b.a.e.web.EndpointLinksResolver      : Exposing 1 endpoints beneath base path '/actuator'

pom.xml:

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>3.3.2</version>
    </parent>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>
        <dependency>
            <groupId>io.micrometer</groupId>
            <artifactId>micrometer-registry-prometheus</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>

    </dependencies>

application.yml:

management:
  endpoints:
    web:
      exposure:
        include: health, prometheus
  endpoint:
    prometheus:
      enabled: true
    health:
      enabled: true
      show-details: always

What am I missing?


Solution

  • With Spring Boot 3.x, you'll have to annotate your test class with @AutoConfigureObservability. Doc here.

    Example test class

    import org.junit.jupiter.api.Test;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.boot.test.autoconfigure.actuate.observability.AutoConfigureObservability;
    import org.springframework.boot.test.context.SpringBootTest;
    import org.springframework.boot.test.web.client.TestRestTemplate;
    import org.springframework.http.HttpStatus;
    import org.springframework.http.ResponseEntity;
    
    import static org.junit.jupiter.api.Assertions.assertEquals;
    
    
    @AutoConfigureObservability
    @SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
    public class ActuatorTest {
    
        @Autowired
        private TestRestTemplate restTemplate;
    
        @Test
        void getPrometheusMetrics_expectOK() {
            ResponseEntity<String> response = restTemplate.getForEntity("/actuator/prometheus", String.class);
            assertEquals(HttpStatus.OK, response.getStatusCode());
        }
    }