Search in sources :

Example 1 with HealthIndicatorDetail

use of com.alipay.sofa.healthcheck.startup.StartUpHealthCheckStatus.HealthIndicatorDetail in project sofa-boot by alipay.

the class SpringContextStartUpHealthCheckStatusCheckInfo method invoke.

@Override
public Health invoke() {
    // spring
    boolean springContextStatus = StartUpHealthCheckStatus.getSpringContextStatus();
    // component
    boolean componentStatus = StartUpHealthCheckStatus.getComponentStatus();
    Map<String, Health> componentDetail = StartUpHealthCheckStatus.getComponentDetail();
    // HealthIndicator
    boolean healthIndicatorStatus = StartUpHealthCheckStatus.getHealthIndicatorStatus();
    List<HealthIndicatorDetail> healthIndicatorDetails = StartUpHealthCheckStatus.getHealthIndicatorDetails();
    // AfterHealthCheckCallback
    boolean afterHealthCheckCallbackStatus = StartUpHealthCheckStatus.getAfterHealthCheckCallbackStatus();
    Map<String, Health> afterHealthCheckCallbackDetails = StartUpHealthCheckStatus.getAfterHealthCheckCallbackDetails();
    Map<String, Health> healths = new HashMap<String, Health>();
    // spring
    if (springContextStatus) {
        healths.put("springContextHealthCheckInfo", Health.up().build());
    } else {
        healths.put("springContextHealthCheckInfo", Health.down().build());
    }
    // component and callback
    Builder builder = null;
    if (componentStatus && healthIndicatorStatus && afterHealthCheckCallbackStatus) {
        builder = Health.up();
    } else {
        builder = Health.down();
    }
    if (!CollectionUtils.isEmpty(componentDetail)) {
        builder = builder.withDetail("Middleware-start-period", componentDetail);
    }
    if (!CollectionUtils.isEmpty(afterHealthCheckCallbackDetails)) {
        builder = builder.withDetail("Middleware-operation-period", afterHealthCheckCallbackDetails);
    }
    healths.put("sofaBootComponentHealthCheckInfo", builder.build());
    // HealthIndicator
    for (HealthIndicatorDetail healthIndicatorDetail : healthIndicatorDetails) {
        String name = healthIndicatorDetail.getName();
        Health health = healthIndicatorDetail.getHealth();
        healths.put(name, health);
    }
    return this.healthAggregator.aggregate(healths);
}
Also used : Health(org.springframework.boot.actuate.health.Health) HashMap(java.util.HashMap) Builder(org.springframework.boot.actuate.health.Health.Builder) HealthIndicatorDetail(com.alipay.sofa.healthcheck.startup.StartUpHealthCheckStatus.HealthIndicatorDetail)

Example 2 with HealthIndicatorDetail

use of com.alipay.sofa.healthcheck.startup.StartUpHealthCheckStatus.HealthIndicatorDetail in project sofa-boot by alipay.

the class HealthIndicatorCheckProcessor method checkIndicator.

public boolean checkIndicator() {
    if (skipHealthIndicator()) {
        logger.info("Skip startup healthIndicator check.");
        return true;
    }
    logger.info("Begin startup healthIndicator check.");
    List<HealthIndicator> healthIndicators = HealthCheckManager.getHealthIndicator();
    if (healthIndicators == null) {
        return true;
    }
    boolean result = true;
    for (HealthIndicator healthIndicator : healthIndicators) {
        try {
            Health health = healthIndicator.health();
            Status status = health.getStatus();
            if (!status.equals(Status.UP)) {
                result = false;
                logger.error("healthIndicator (" + healthIndicator.getClass() + ")check fail. And the status is[" + status + "]; the detail is: " + JSON.toJSONString(health.getDetails()));
            } else {
                logger.info("healthIndicator (" + healthIndicator.getClass() + ")check success.");
            }
            StartUpHealthCheckStatus.addHealthIndicatorDetail(new HealthIndicatorDetail(getKey(healthIndicator), health));
        } catch (Exception e) {
            result = false;
            logger.error("Error occurred while doing healthIndicator health check(" + healthIndicator.getClass() + ")", e);
        }
    }
    if (result) {
        logger.info("Startup healthIndicator check result: success.");
    } else {
        logger.error("Startup healthIndicator check result: fail.");
    }
    StartUpHealthCheckStatus.setHealthIndicatorStatus(result);
    return result;
}
Also used : Status(org.springframework.boot.actuate.health.Status) StartUpHealthCheckStatus(com.alipay.sofa.healthcheck.startup.StartUpHealthCheckStatus) HealthIndicator(org.springframework.boot.actuate.health.HealthIndicator) Health(org.springframework.boot.actuate.health.Health) HealthIndicatorDetail(com.alipay.sofa.healthcheck.startup.StartUpHealthCheckStatus.HealthIndicatorDetail)

Aggregations

HealthIndicatorDetail (com.alipay.sofa.healthcheck.startup.StartUpHealthCheckStatus.HealthIndicatorDetail)2 Health (org.springframework.boot.actuate.health.Health)2 StartUpHealthCheckStatus (com.alipay.sofa.healthcheck.startup.StartUpHealthCheckStatus)1 HashMap (java.util.HashMap)1 Builder (org.springframework.boot.actuate.health.Health.Builder)1 HealthIndicator (org.springframework.boot.actuate.health.HealthIndicator)1 Status (org.springframework.boot.actuate.health.Status)1