InfoContributor lets a Spring Boot Actuator application add dynamic information to the /info endpoint. Unlike static info.* properties, an implementation can query application data and publish the result. section 15.3.1
Publishing taco statistics
Create a component that implements InfoContributor and injects TacoRepository. Its contribute() method reads the repository count, places it in a map, and passes that map to the builder with the label taco-stats. A reactive repository returns Mono<Long>, so the example calls block() before publishing the count. section 15.3.1
Listing 15.3 A custom implementation of InfoContributor
package tacos.actuator;
import java.util.HashMap;
import java.util.Map;
import org.springframework.boot.actuate.info.Info.Builder;
import org.springframework.boot.actuate.info.InfoContributor;
import org.springframework.stereotype.Component;
import tacos.data.TacoRepository;
@Component
public class TacoCountInfoContributor implements InfoContributor {
private TacoRepository tacoRepo;
public TacoCountInfoContributor(TacoRepository tacoRepo) {
this.tacoRepo = tacoRepo;
}
@Override
public void contribute(Builder builder) {
long tacoCount = tacoRepo.count().block();
Map<String, Object> tacoMap = new HashMap<String, Object>();
tacoMap.put("count", tacoCount);
builder.withDetail("taco-stats", tacoMap);
}
}
Use the complete listing to see the implementation, constructor injection, @Component registration, repository query, and withDetail() call together. section 15.3.1
