Spring Boot Actuator: Caching /actuator/health/readiness and /actuator/health/custom health check results

Viewed 720

So I have added the Spring Boot Actuator to my application and I specified in the application.properties property management.endpoint.health.cache.time-to-live=120s to cache health check results. So when I call /actuator/health, the result is cached and it's good.

The problem starts when I call /actuator/health/readiness or custom created group /actuator/health/deep this request results are not cached. I went through Spring Docs and found only information for the main health endpoint, nothing for specific groups.

So my question is: did I miss sth? Or how can I implement caching for specific spring boot actuator endpoints?

Thanks

1 Answers

So I didn't find out of a box solution, so decided to go with the AOP approach.

I found HealthEndpointWebExtension class that manages health rest endpoints. This class has two health methods, one one of them called for common /health endpoint and second when we call health/<group name>.

So I created Aspect around the second method, which allowed me to add cache for endpoints I need.

@Around("execution(@org.springframework.boot.actuate.endpoint.annotation.ReadOperation * org.springframework.boot.actuate.health.HealthEndpointWebExtension.health(..,java.lang.String...))) && args(.., path)")
public Object cache(ProceedingJoinPoint joinPoint, String... path) throws Throwable {
    // Logic to get cache per endpoint and process if cache specifed.
}

At the end application.properties looks like this:

// out of the box
management.endpoint.health.cache.time-to-live=120s 

// customly created properties
management.endpoint.health.group.readiness.cache.time-to-live=60s
management.endpoint.health.group.deep.cache.time-to-live=120s
Related