I want to display the following Health-Information:
{
"status":"UP",
"components": {
"myHealthComposite": {
"status":"UP",
"components": {
"myFirstHealthIndicator": { "status":"UP" },
"mySecondHealthIndicator": { "status":"UP" }
}
}
},
"groups":[]
}
From the latest Spring Boot Actuator Documentation I should implement "myFirstHealthIndactor" and "mySecondHealthIndicator" with the "HealthIndicator"-Interface and composite them with the "CompositeHealthContributor"-Interface. But for "CompositeHealthContributor" I need an excample and in the Documentation I can not found something usefull for me.
Can someone help me with that, please?
Greetings
Edit:
myFirstHealthIndicator
@Component
public class MyFirstHealthIndicator implements HealthIndicator {
@Override
public Health health() {
return Health.up().build();
}
}
mySecondHealthIndicator
@Component
public class MySecondHealthIndicator implements HealthIndicator {
@Override
public Health health() {
return Health.up().build();
}
}
myHealthComposite
@Component
public class MyHealthComposite implements CompositeHealthContributor {
@Override
public HealthContributor getContributor(String name) {
return null;
}
@Override
public Iterator<NamedContributor<HealthContributor>> iterator() {
return null;
}
}
A very simple and hard coded solution that outputs the expected.
Please remove the @Component
annotation from MyFirstHealthIndicator and MySecondHealthIndicator. Otherwise, they are also listed as separate components.
MyFirstHealthIndicator
public class MyFirstHealthIndicator implements HealthIndicator {
@Override
public Health health() {
return Health.up().build();
}
}
MySecondHealthIndicator
public class MySecondHealthIndicator implements HealthIndicator {
@Override
public Health health() {
return Health.up().build();
}
}
MyHealthComposite
@Component
public class MyHealthComposite implements CompositeHealthContributor {
private Map<String, HealthContributor> contributors = new LinkedHashMap<>();
MyHealthComposite() {
this.contributors.put("myFirstHealthIndicator", new MyFirstHealthIndicator());
this.contributors.put("mySecondHealthIndicator", new MySecondHealthIndicator());
}
@Override
public HealthContributor getContributor(String name) {
return contributors.get(name);
}
@Override
public Iterator<NamedContributor<HealthContributor>> iterator() {
return contributors.entrySet().stream()
.map((entry) -> NamedContributor.of(entry.getKey(), entry.getValue())).iterator();
}
}
Please enable the management.endpoint.health.show-components=always
in your application.properties
.
The output looks like:
{
"status": "UP",
"components": {
"diskSpace": {
"status": "UP"
},
"myHealthComposite": {
"status": "UP",
"components": {
"myFirstHealthIndicator": {
"status": "UP"
},
"mySecondHealthIndicator": {
"status": "UP"
}
}
},
"ping": {
"status": "UP"
}
}
}
However, this is not my solution, but is adopted from BindersHealthIndicatorAutoConfiguration from spring-cloud-stream and adapted to the requirements.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With