首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >报告spring boot执行器的健康状态作为指标

报告spring boot执行器的健康状态作为指标
EN

Stack Overflow用户
提问于 2021-11-08 16:49:11
回答 1查看 52关注 0票数 1

我想要报告应用程序的健康状态作为量规,并且我希望使用与spring-boot-actuator相同的健康指示器,但是,我没有看到任何可以在这里使用的来自spring-boot-actuator依赖项的可导出组件。

我想写的代码是:

代码语言:javascript
复制
@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吗?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-11-08 17:16:39

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

代码语言:javascript
复制
@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;
    }

}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/69887048

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档