繁体   English   中英

将 Spring 的健康指标映射到指标

[英]Mapping Spring's health indicators to metrics

我想将每个春天的健康指标映射到普罗米修斯指标。 类似于http://micrometer.io/docs/guide/healthAsGauge ,但针对每个健康指标。 我使用弹簧靴 2 + 千分尺 + 普罗米修斯。 有没有什么优雅的方法来做到这一点? ID

解决方案可以是:

@Configuration
public class HealthMetricsConfiguration {

    @Bean
    public MeterRegistryCustomizer prometheusHealthCheck(HealthEndpoint healthEndpoint) {
        return registry -> registry.gauge("health", healthEndpoint, HealthMetricsConfiguration::healthToCode);
    }

    public static int healthToCode(HealthEndpoint ep) {
        Status status = ep.health().getStatus();

        return status.equals(Status.UP) ? 1 : 0;
    }
}

您可以在注册表中绑定所有健康指标。

这是另一种解决方案。 它为每个 HealthIndicer 创建一个指标,将健康指标的名称作为属性值。 度量值 1 表示指示符健康,否则就会出现问题。

    @Bean
public MeterRegistryCustomizer<MeterRegistry> healthRegistryCustomizer(HealthContributorRegistry healthRegistry) {
    return registry -> healthRegistry.stream().forEach(namedContributor -> registry.gauge("health", Tags.of("name", namedContributor.getName()), healthRegistry, health -> {
        var status = ((HealthIndicator) health.getContributor(namedContributor.getName())).getHealth(false).getStatus();
        return healthToCode(status);
    }));
}

public static int healthToCode(Status status) {
    return status.equals(Status.UP) ? 1 : 0;
}

这是对以前解决方案的清理。

@Bean
MeterRegistryCustomizer<MeterRegistry> metricsHealthGauge(HealthEndpoint healthEndpoint) {
    log.info("Registering application_health metric gauge");
    return registry ->
        Gauge.builder("application_health", healthEndpoint, e -> e.health().getStatus().equals(Status.UP) ? 1 : 0).register(registry);
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM