MeterRegistry HealthIndicator 如何使用 Gauge 测量时报告数据

问题描述 投票:0回答:0

我正在使用 statsd 指标,现在我正在尝试添加指标 healthIndicator

public class xxxHealthIndicator implements HealthIndicator {
 private final xxx xxx;

    private final MeterRegistry meterRegistry;
    @Autowired
    public xxxHealthIndicator(xxx xxx, @Nullable MeterRegistry meterRegistry) {
        this.xxx = xxx;
        this.meterRegistry = meterRegistry;
    }

    @Override
    public Health health() {
        return A.getHealth(xxx.getHealth());
    }
}

我的想法是在 health() 中添加代码:

 @Override
    public Health health() {
        if(meterRegistry!=null){
         Gauge gauge = Gauge
// I am confusing about report data when observed, does is means I need to add status change ? 
                .builder("xxxhealth", ????, ????) 

                .tags("status",xxx.getHealth.get("status"))
                .register(meterRegistry);
         }
        return A.getHealth(xxx.getHealth());
    }

以及 https://docs.spring.io/spring-boot/docs/2.5.6/reference/html/howto.html#howto.actuator.map-health-indicators-to-metrics,HealthEndpoint I不需要添加。

 Gauge gauge = Gauge 
                .builder("xxxhealth", ????, this::getStatusCode ) 
                .tags("status",xxx.getHealth().get("status"))
                .register(meterRegistry);
         }
//getHealth() is return a Map
private int getStatusCode(xxx health) {
       String status = health.getHealth().get("status");
        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;
    }

spring-boot spring-boot-actuator micrometer gauge statsd
© www.soinside.com 2019 - 2024. All rights reserved.