Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to implement a custom CompositeHealthContributor

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;
    }

}
like image 756
JCodebreaker Avatar asked Oct 16 '22 08:10

JCodebreaker


1 Answers

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.

like image 89
Rudolf Grauberger Avatar answered Dec 28 '22 06:12

Rudolf Grauberger