[英]get notified when spring boot healtcheck status changes
Is there a way to get notified when the status of the registered health check indicators changes? 当注册的健康检查指标的状态发生变化时,有没有办法得到通知? For example, when the healthcheck indicator of database becomes down, I would like to take some actions. 例如,当数据库的运行状况检查指示灯熄灭时,我想采取一些措施。
Actually, my final goal is to export healthcheck status to Prometheus' metrics. 实际上,我的最终目标是将健康检查状态导出为Prometheus的指标。 So, when there is status change, I want to update health metrics. 因此,当状态发生变化时,我想更新运行状况指标。
I assume your question refers to Micrometer issue 416 and Micrometer-Docs issue 39 . 我假设您的问题涉及Micrometer 问题416和Micrometer-Docs 问题39 。
As per documentation, you can register the custom HealthMetricsConfiguration
. 根据文档,您可以注册自定义HealthMetricsConfiguration
。 The value of the gauge is determined by the status the ComposeHealthIndicator
returns and is actually changing depending on the state of the single HealthIndicator
s. 量规的值由ComposeHealthIndicator
返回的状态确定,并且实际上根据单个HealthIndicator
的状态而变化。
I am using afformentioned HealthMetricsConfiguration
(just with different status value mappings as discussed in issue 416). 我使用的是提到的HealthMetricsConfiguration
(就像问题416中讨论的那样,只是具有不同的状态值映射)。
Wen't ahead and implemented a custom alternating health indicator: Wen并没有实施自定义交替健康指标:
@Component
public class AlternatingHealthIndicator extends AbstractHealthIndicator {
@Override
protected void doHealthCheck(Builder builder) throws Exception {
int minute = LocalDateTime.now().getMinute();
boolean minuteIsEven = minute % 2 == 0;
builder.status(minuteIsEven ? Status.UP : Status.DOWN);
builder.withDetail("description", "UP when current minute is even; DOWN when current minute is odd");
builder.withDetail("currentMinute", minute);
builder.withDetail("minuteIsEven", minuteIsEven);
}
}
The health
gauge exported on the Prometheus endpoint is minutely changing from 1=UP
to -2=DOWN
. 在Prometheus端点上导出的health
指标正在从1=UP
更改为-2=DOWN
。 Here's a visualization: 这是一个可视化:
Regarding alerting, you can use Grafana alerting or look into Prometheus' Alertmanager. 关于警报,您可以使用Grafana警报或查看Prometheus的Alertmanager。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.