Search code examples
javaspring-bootspring-boot-actuator

Java Spring Boot get list of exposed actuator endpoints


I have a Spring Boot 2.7 app that runs in different environments. Depending on the environment, different actuator endpoints are exposed (development environment has all of them exposed).

The actuator base path is altered to:

management.endpoints.web.base-path=/

So, for example, the health path is:

localhost:8080/health

I have a OncePerRequestFilter that I need to not filter the actuator paths.

If I use management.endpoints.web.exposure.include, the value is * for my development environment. I can't use that for the shouldNotFilter check though.

I need an actual list, like [/info,/health,...]. I don't even need the /, as I can add that to my matcher, but it would be nice if it just had it.

Is there something that can pull the exposed actuator endpoints into a list without me having to hardcode a list?

Thanks.


Solution

  • You could try something like this, this also lists any extra custom endpoints you added to the actuator:

    import java.util.List;
    
    import org.springframework.boot.actuate.endpoint.EndpointId;
    import org.springframework.boot.actuate.endpoint.EndpointsSupplier;
    import org.springframework.boot.actuate.endpoint.web.ExposableWebEndpoint;
    import org.springframework.stereotype.Component;
    
    @Component
    public class ActuatorEndpoints {
    
        private final EndpointsSupplier<ExposableWebEndpoint> endpointsSupplier;
    
        public ActuatorEndpoints(final EndpointsSupplier<ExposableWebEndpoint> endpointsSupplier) {
            this.endpointsSupplier = endpointsSupplier;
        }
    
        public List<String> list() {
            return endpointsSupplier.getEndpoints().stream()
                    .map(ExposableWebEndpoint::getEndpointId)
                    .map(EndpointId::toString)
                    .toList();
        }
    }
    

    Then you can inject this as a bean wherever you need the list, for example:

    @Bean
    public MyBean myBean(ActuatorEndpoints actuatorEndpoints) {
        return new MyBean(actuatorEndpoints.list());
    }