From a Sprig Boot application is it possible access the actuator/health data directly without making a rest call and parsing the results?
Ideally I could autowire a bean and then be able to fetch the object representation of the health data with a method call.
For example if my health endpoint is showing something like this:
{
"status": "UP",
"components": {
"db": {
"status": "UP",
"details": {
"database": "PostgreSQL",
"result": 1,
"validationQuery": "SELECT 1"
}
},
"diskSpace": {
"status": "UP",
"details": {
"total": 499963174912,
"free": 389081399296,
"threshold": 10485760
}
},
"ping": {
"status": "UP"
},
"redis": {
"status": "UP",
"details": {
"version": "3.2.12"
}
}
}
}
Then which components could I autowire to find out each of those bits of information?
4
Answers
Sure you can inject the according
Endpoint
. For example if you are interested in theHealthEndpoint
you could do:Also, the info and health points are enabled in actuator by default and you don’t need to expose them manually. The moment you add the dependency to your pom.xml it will be enabled and you can access the url endpoints without exposing them
There is probably a better way to get the health data but this worked for me.
Example Output:
To Mock the HealthContributor[] you may try to use Mockito like below:
This example uses the HealthContributorRegistry to retrieve the name of the HealthIndicator. It also takes into account CompositeHealthContributors.