A library to assist in reporting on the health of a system.

Usage

In order to integrate with this library, a module will need to implement the Checkable trait.

Consumers can implement the Checkable trait directly or provide a function that can perform the health check. Such a function can be either synchronous or asynchronous.

Synchronous checker

Synchronous checker functions have the form Fn() -> Result<(), Error> and can be created with check_fn().

```

use std::fmt::Error;

use health::Checkable;

fn alliswell() -> Result<(), Error> { Ok(()) } fn everythingisfire() -> Result<(), Error> { Err(Error {}) }

let alwaysok = health::checkfn("ok", alliswell); let alwaysbad = health::checkfn("bad", everythingisfire); ```

Asynchronous checker

Asynchronous checker functions have the form async Fn() -> Result<(), Error> and can be created with check_future().

```

use std::fmt::Error;

use health::Checkable;

async fn alliswell() -> Result<(), Error> { Ok(()) } async fn everythingisfire() -> Result<(), Error> { Err(Error {}) }

let alwaysok = health::checkfuture("ok", alliswell); let alwaysbad = health::checkfuture("bad", everythingisfire); ```

Periodic background health checks

Once a Checkable is created, that can be passed to a PeriodicChecker<C>, which implements the Reporter trait. The periodic checker can be configured to define the parameters for reporting a health status.

Health checks are performed periodically in the background and not inline to requests for the current health status. This ensures that information about the current health status is always available without delay, which can be particularly important for health checking endpoings on web servers (such as the oft-seen /healthz and /health endpoints).

Example

```

use std::{fmt::Error, future::Future};

use std::time::Duration; use health::Reporter;

async fn alliswell() -> Result<(), Error> { Ok(()) } let alwaysok = health::checkfuture("ok", alliswell);

let reporter = health::PeriodicChecker::new(alwaysok, health::Config { checkinterval: Duration::fromsecs(10), leeway: Duration::fromsecs(30), minsuccesses: 2, minfailures: 6, });

// Spawn the reporter on your executor to perform // periodic checks in the background

fn spawn(t: T)

where

T: Future + Send + 'static,

T::Output: Send + 'static,

{}

spawn(reporter.clone().run());

asserteq!(health::Status::Healthy, reporter.rawstatus()); asserteq!(Some(health::Status::Healthy), reporter.status()); asserteq!(health::Check::Pass, reporter.last_check()); ```

Tracing

This library makes use of the tracing library to report on the health status of resources using the health target. The PeriodicChecker<C> uses the following event levels when reporting the health status after each check is complete:

Features