5个版本
0.2.0 | 2022年5月13日 |
---|---|
0.1.3 | 2020年11月9日 |
0.1.2 | 2020年11月7日 |
0.1.1 | 2020年11月6日 |
0.1.0 | 2020年11月6日 |
#357 in HTTP服务器
每月170次下载
34KB
584 行
一个辅助报告系统健康状况的库。
使用方法
为了与该库集成,需要实现一个模块来执行Checkable
特质。
消费者可以直接实现Checkable
特质或提供一个可以执行健康检查的函数。此函数可以是同步的或异步的。
同步检查器
同步检查器函数具有以下形式 Fn() -> Result<(), Error>
,可以使用check_fn()
来创建。
# use std::fmt::Error;
use health::Checkable;
fn all_is_well() -> Result<(), Error> { Ok(()) }
fn everything_is_fire() -> Result<(), Error> { Err(Error {}) }
let always_ok = health::check_fn("ok", all_is_well);
let always_bad = health::check_fn("bad", everything_is_fire);
异步检查器
异步检查器函数具有以下形式 async Fn() -> Result<(), Error>
,可以使用check_future()
来创建。
# use std::fmt::Error;
use health::Checkable;
async fn all_is_well() -> Result<(), Error> { Ok(()) }
async fn everything_is_fire() -> Result<(), Error> { Err(Error {}) }
let always_ok = health::check_future("ok", all_is_well);
let always_bad = health::check_future("bad", everything_is_fire);
周期性后台健康检查
一旦创建了Checkable
,就可以将其传递给实现Reporter
特质的PeriodicChecker<C>
,该检查器可以配置以定义报告健康状态的参数。
健康检查在后台周期性执行,而不是在当前健康状态请求中内联执行。这确保了当前健康状态信息总是可用的,不会延迟,这对于健康检查Web服务器的端点(如常见的/healthz
和/health
端点)尤其重要。
示例
# use std::{fmt::Error, future::Future};
use std::time::Duration;
use health::Reporter;
async fn all_is_well() -> Result<(), Error> { Ok(()) }
let always_ok = health::check_future("ok", all_is_well);
let reporter = health::PeriodicChecker::new(always_ok, health::Config {
check_interval: Duration::from_secs(10),
leeway: Duration::from_secs(30),
min_successes: 2,
min_failures: 6,
});
// Spawn the reporter on your executor to perform
// periodic checks in the background
# fn spawn<T>(t: T)
# where
# T: Future + Send + 'static,
# T::Output: Send + 'static,
# {}
spawn(reporter.clone().run());
assert_eq!(health::Status::Healthy, reporter.raw_status());
assert_eq!(Some(health::Status::Healthy), reporter.status());
assert_eq!(health::Check::Pass, reporter.last_check());
跟踪
该库使用 tracing
库,通过 health
目标报告资源健康状况。在每次检查完成后,PeriodicChecker<C>
使用以下事件级别来报告健康状况:
- 当健康状况不佳且最近一次检查失败时,显示
ERROR
。 - 当健康状况良好但最近一次检查失败时,显示
WARN
。 - 当健康状况不佳但最近一次检查通过时,显示
INFO
。 - 当不健康的资源变得健康时,显示
INFO
报告。 - 当健康状况良好且最近一次检查通过时,显示
DEBUG
。
特性
tracing
(默认):使用tracing
库报告周期性健康检查的结果。
依赖项
~3–9.5MB
~80K SLoC