faucet-cli 1.0.0

Config-driven CLI runner for faucet-stream pipelines (YAML / JSON, Meltano-style)
Documentation
//! Liveness (`/healthz`), readiness (`/readyz`), and Prometheus (`/metrics`)
//! endpoints. All three are unauthenticated (probes / scrapers). Phase 1
//! `/readyz` is always-ready; it gains history-degraded / queue-full checks in
//! later phases.

use crate::serve::state::ServerState;
use axum::extract::State;
use axum::http::{StatusCode, header};
use axum::response::{IntoResponse, Response};

/// Liveness: a responding handler means the process is alive.
pub async fn healthz() -> impl IntoResponse {
    StatusCode::OK
}

/// Readiness: 503 if the history backend is degraded or the queue is full
/// (cannot accept new work), else 200.
pub async fn readyz(State(state): State<ServerState>) -> impl IntoResponse {
    let history_ok = !state.history().degraded();
    let queue_ok = !state.registry().is_full();
    if history_ok && queue_ok {
        StatusCode::OK
    } else {
        StatusCode::SERVICE_UNAVAILABLE
    }
}

/// Prometheus exposition rendered from serve's own recorder handle.
pub async fn metrics(State(state): State<ServerState>) -> Response {
    match state.render_metrics() {
        Some(body) => ([(header::CONTENT_TYPE, "text/plain; version=0.0.4")], body).into_response(),
        // 503 (not 200) so a Prometheus scraper marks the target down rather
        // than silently recording a successful scrape with zero metrics. This
        // only fires if the recorder failed to install (e.g. a second server in
        // one process); in normal operation `render_metrics()` is `Some`.
        None => (
            StatusCode::SERVICE_UNAVAILABLE,
            [(header::CONTENT_TYPE, "text/plain; version=0.0.4")],
            "# metrics recorder not installed in this process\n",
        )
            .into_response(),
    }
}