【问题标题】:Report spring boot actuator health status as a metric将 spring boot 执行器健康状态报告为指标
【发布时间】:2021-11-08 16:49:11
【问题描述】:

我想将应用程序的健康状态报告为指标,并且我希望使用与 spring-boot-actuator 相同的健康指标,但是,我没有看到 spring-boot-actuator 依赖项中的任何可导出组件我也许可以在这里使用。

我想写的代码:

@Component
public class HealthCounterMetric {
  private final Counter statusCounter;

  public HealthCounterMetric(MeterRegistry meterRegistry, SystemHealth systemHealth) {
    this.statusCounter = meterRegistry.counter("service.status");
  }

  @Scheduled(fixedRate = 30000L)
  public void reportHealth() {
    //do report health
  }
}

当然,SystemHealth 不是导出的 bean。 spring boot 执行器是否会导出我可以通过这种方式消费的 bean?

【问题讨论】:

    标签: spring-boot spring-boot-actuator


    【解决方案1】:

    参考文档describes how to do this by mapping the HealthEndpoint's response to a gauge

    @Configuration(proxyBeanMethods = false)
    public class MyHealthMetricsExportConfiguration {
    
        public MyHealthMetricsExportConfiguration(MeterRegistry registry, HealthEndpoint healthEndpoint) {
            // This example presumes common tags (such as the app) are applied elsewhere
            Gauge.builder("health", healthEndpoint, this::getStatusCode).strongReference(true).register(registry);
        }
    
        private int getStatusCode(HealthEndpoint health) {
            Status status = health.health().getStatus();
            if (Status.UP.equals(status)) {
                return 3;
            }
            if (Status.OUT_OF_SERVICE.equals(status)) {
                return 2;
            }
            if (Status.DOWN.equals(status)) {
                return 1;
            }
            return 0;
        }
    
    }
    

    【讨论】:

      猜你喜欢
      • 2015-07-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-08-24
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多