首页 > 解决方案 > Spring Boot 2 迁移后未导出 Health Endpoint Metrics

问题描述

我的团队将我们的微服务从 Spring Boot 1 迁移到了版本 2,并且由于执行器发生了变化,我们通过 prometheus jmx 导出器导出的 Health Endpoint Metrics 不再起作用。

通常的 /actuator/health 按预期工作,但prometheus-jmx-exporter不会拿起它,尽管尝试了几件事:

所以现在我的想法已经用完了,如果oyu能给我任何提示,我将不胜感激

旧的 prometheus-jmx-exporter exporter-config.yaml:

---
lowercaseOutputName: true
lowercaseOutputLabelNames: true
whitelistObjectNames: ["org.springframework.boot:type=Endpoint,name=healthEndpoint"]
rules:
- pattern: 'org.springframework.boot<type=Endpoint, name=healthEndpoint><(.*, )?(.*)>(.*):'
  name: health_endpoint_$1$3
  attrNameSnakeCase: true  

新的 prometheus-jmx-exporter exporter-config.yaml:

---
lowercaseOutputName: true
lowercaseOutputLabelNames: true
whitelistObjectNames: ["org.springframework.boot:type=Endpoint,name=Health"]
rules:
- pattern: 'org.springframework.boot<type=Endpoint, name=Health>'
  name: health_endpoint_$1$3
  attrNameSnakeCase: true

当前关于执行器端点的应用程序属性:

management.endpoints.web.exposure.include=info, health, refresh, metrics, prometheus
management.endpoints.jmx.exposure.include=health, metrics, prometheus

在带有旧 exporter-config.yaml 的 Spring Boot 1 中,我得到如下结果:

# HELP health_endpoint_hystrix_status Invoke the underlying endpoint (org.springframework.boot<type=Endpoint, name=healthEndpoint><hystrix, status>status)
# TYPE health_endpoint_hystrix_status untyped
health_endpoint_hystrix_status 1.0
# HELP health_endpoint_status Invoke the underlying endpoint (org.springframework.boot<type=Endpoint, name=healthEndpoint><status>status)
# TYPE health_endpoint_status untyped
health_endpoint_status 1.0

但是随着所有的变化和 Spring Boot 2 中的变化,我什么也没得到。

标签: springjmxprometheusactuator

解决方案


您可以配置自己的健康值并将其添加到 Prometheus Metrics 端点:

@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;
    }
}

推荐阅读