3 Aralık 2022 Cumartesi

SpringBoot Actuator HealthIndicator Arayüzü - Custom Health Monitoring

Giriş
Şu satırı dahil ederiz.
import org.springframework.boot.actuate.health.Health;
import org.springframework.boot.actuate.health.HealthIndicator;
1. Kendi Health Endpoint'imizi yazmak için bu arayüzden kalıtırız. Nesneyi @Component olarak işaretleriz.
2. Health.up() veya Health.down() ile bir sonuç döneriz. 
3. Sonuç nesnesine withDetail() ile açıklama verilebilir.

health metodu
Örnek
Şöyle yaparız
@Component
public class MyHealthIndicator implements HealthIndicator {
  @Override
  public Health health() {
    // Perform some specific health check
    int errorCode = check();
    if (errorCode != 0) {
      return Health.down().withDetail("Error Code", errorCode).build();
     }
     return Health.up().build();
  }
    
  private int check() {
    // Perform some health check and return an error code
    // (0 if the check is successful, non-zero if there is an error)
  }
}
Örnek
Şöyle yaparız
@Component
public class DemoHealthIndicator implements HealthIndicator {
    private static int count = 0;
    private static Status WARMING = new Status("WARMING");

    @Override
    public Health health() {
        return ++count > 2
            ? Health.up().build()
            : Health.status(WARMING).build();
    }
}
Burada yeni bir Status çeşidi döndürülüyor. Bunu Spring'e tanıtmak için şöyle yaparız
management.endpoint.health.status.order=down,out-of-service,warming,unknown,up
Örnek
Şöyle yaparız.
@Component
public class BridgeArtioHealthCheck implements HealthIndicator {

  @Override
  public Health health() {
    BrokerConnection connection = ...
    if (connection != null) {
      return Health.down().withDetail("Broker Connection is not running ",
        connection.getName()).build();
    }
    return Health.up().build();
  }
  ...
}
Çıktı olarak şuna benzer bir şey alırız
{
  "status": "UP",
  "checks": [
    {
      "name": "MongoDB connection health check",
      "status": "UP",
      "data": {
        "default": "admin, config, hello, local"
      }
    }
  ]
}
Örnek
Şöyle yaparız.
@Component
public class InventoryChecker implements HealthIndicator {

    private final BookRepository bookRepository;
    private static final int MIN_AMOUNT = 10;

    public InventoryChecker(BookRepository bookRepository) {
        this.bookRepository = bookRepository;
    }

  @Override
  public Health health() {
    List<Book> books = bookRepository.findAll();
    for (Book book : books) {
      if (book.getAmount() < MIN_AMOUNT) {
        return Health.down().withDetail("This book is running out of copies: " 
          + book.getName() + ". The minimum amount is " + MIN_AMOUNT 
          + ". Please supply more!", book.getAmount()).build();
      }
    }
    return Health.up()
      .withDetail("All books have enough copies. The minimum required amount is:", 
        MIN_AMOUNT)
      .build();
  }
}
Örnek
Şöyle yaparız.
@Component
public class LoggerService implements HealthIndicator {
  @Override
  public Health health() {
    if (isLoggingEnabled()) {
      return Health.up()
        .withDetail("logging", "Loggers are enabled")
        .build();
    }
    return Health
      .down()
      .withDetail("logging", "Loggers are disabled")
      .build();
  }

  private boolean isLoggingEnabled() {
    // dummy - this is the place we need a proper implementation. 
    return true; 
  }
}

Hiç yorum yok:

Yorum Gönder