Combine Spring Boot Actuator /health and /info into one

Viewed 805

We currently have a framework which checks our Microservices, and which checks a URL for information about the health and info about our applications. The limitation here is that this framework can only check 1 URL.

What I'd like to do is to combine the information of /health and /info into one, and have /health also show the information of /info, most notably the version of the deployed application. Is something like this possible out of the box, or should I create my own health check which displays this information?

1 Answers

I don't believe there is any kind of configuration you can do out of the box to achieve this, however you should be able to write your own healthendpoint that achieves this. The below have worked for me.

Spring 1.5.x

@Component
public class InfoHealthEndpoint extends HealthEndpoint {

    @Autowired
    private InfoEndpoint infoEndpoint;

    public InfoHealthEndpoint(HealthAggregator healthAggregator, Map<String, HealthIndicator> healthIndicators) {
        super(healthAggregator, healthIndicators);
    }

    @Override
    public Health invoke() {
        Health health = super.invoke();
        return new Health.Builder(health.getStatus(), health.getDetails())
                .withDetail("info", infoEndpoint.invoke())
                .build();
    }

}

Spring 2.x

public class InfoHealthEndpoint extends HealthEndpoint {

    private InfoEndpoint infoEndpoint;

    public InfoHealthEndpoint(HealthIndicator healthIndicator, InfoEndpoint infoEndpoint) {
        super(healthIndicator);
        this.infoEndpoint = infoEndpoint;
    }

    @Override
    @ReadOperation
    public Health health() {
        Health health = super.health();
        return new Health.Builder(health.getStatus(), health.getDetails())
                .withDetail("info", this.infoEndpoint.info())
                .build();
    }

}

 

@Configuration
class HealthEndpointConfiguration {

    @Bean
    @ConditionalOnEnabledEndpoint
    public HealthEndpoint healthEndpoint(HealthAggregator healthAggregator,
            HealthIndicatorRegistry registry, InfoEndpoint infoEndpoint) {
        return new InfoHealthEndpoint(
                new CompositeHealthIndicator(healthAggregator, registry), infoEndpoint);
    }

}

Then if you need to add this to multiple microservices, you should be able to just create your own jar that autoconfigures this endpoint to replace actuators default healthcheck.

Related