Health
When your API runs under Kubernetes, behind a load balancer, or under
any supervisor, that supervisor needs to ask two questions. Is the
process alive, or should it be restarted? And is it ready to serve, or
should requests go elsewhere for now? The health plugin answers both,
with two routes:
GET /status, the liveness probe: it answers as long as the process answers;GET /ready, the readiness probe: it checks the servers the application depends on (its databases, Redis, the storage…) and answers200only when all of them answer.
Coming from Django: Django has no built-in health check. This is what packages such as
django-health-checkprovide, with its checks for the database, the cache and the storage. In rind, the plugins declare their own checks, so you do not list them.
Turning it on
Turn on the health feature of rind in your Cargo.toml:
rind = { version = "…", features = ["health"] }
Then install the plugin in app():
#![allow(unused)]
fn main() {
// sample/src/lib.rs
.with(rind::health::Health::new())
}
It reads no environment variable.
Asking if the process is alive
GET /status
{"status": "OK"}
/status runs no check and touches no database. It answers 200 as
long as the process can answer at all. A supervisor that gets no answer
restarts the process.
Asking if the application is ready
/ready runs every check at the same time, each with a timeout of
2 seconds. It answers 200 when every check passes, and 503 when any
check fails. In both cases, the body says how each check went, and how
long it took in milliseconds:
GET /ready
{
"status": "FAIL",
"checks": {
"database:default": { "ok": true, "ms": 0.4, "detail": null },
"database:default:replica1": { "ok": true, "ms": 0.6, "detail": "lag 0.2 s" },
"cache": { "ok": false, "ms": 2000.1, "error": "no answer within 2000 ms" },
"storage": { "ok": true, "ms": 0.1, "detail": null }
}
}
Here the cache’s Redis did not answer within the timeout, so the whole
answer is 503, with "status": "FAIL". When everything passes,
status is "OK". The answer carries Cache-Control: no-store, so
that no proxy keeps it.
You do not have to declare the checks of rind’s servers. /ready runs:
- each database’s servers. The primary must answer
SELECT 1. Each replica must answer, and it tells its lag (how far behind the primary it is). A late replica does not fail the check: reads already avoid a replica that may not have a recent write (see Databases). - the checks of the plugins installed, for the servers they depend
on:
cache(Redis answersPING; a cache in memory always passes),storage(the folder or the bucket answers),mail(the mail transport), andjobs(how many jobs are due, and how old the oldest one is). - the application’s own checks (next section).
Adding a check of your own
An application often depends on other servers: an upstream API, a search engine. Add a check for each, either on the plugin:
#![allow(unused)]
fn main() {
.with(rind::health::Health::new().check("upstream", |_state| async {
reach_upstream().await.map(|_| None).map_err(|e| e.to_string())
}))
}
or on the application, from anywhere it is put together, even in a crate that does not know about the health plugin:
#![allow(unused)]
fn main() {
.health("upstream", |_state| async { Ok(Some("v2".to_owned())) })
}
A check is an async function that receives the application’s state (its databases and services). It returns:
Ok(None)when it passes, with nothing to show;Ok(Some(detail))when it passes, with a detail worth showing (a version, a lag);Err(reason)when it fails, with why.
A plugin declares its own checks in the same way, with
Setup::health(name, check) (see Plugins).
What the routes do not do
Neither route needs credentials: a supervisor has no token. Neither
route is a resource, so neither goes through the plugins’ hooks, and
neither appears in the usage log or in the
metrics. The successes are not logged; a failed check is
logged as a warning, under the target rind::health (see
Logging).
Reference
| Route | Answers | For |
|---|---|---|
GET /status | 200 {"status": "OK"} as long as the process answers | a liveness probe |
GET /ready | 200 when every check passes, 503 when any fails, with each check’s result | a readiness probe |
Health:: | Meaning |
|---|---|
new() | the two routes, at /status and /ready, with a timeout of 2 s |
.paths(status, ready) | other paths for the two routes: .paths("/healthz", "/readyz") |
.timeout(duration) | how long a check may take before it fails |
.check(name, check) | a check of the application’s own |
The names of the checks rind declares are database:<name> for a
primary, database:<name>:replica<n> for a replica, cache, storage,
mail and jobs.