Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 37 additions & 5 deletions crates/pathfinder/src/monitoring.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use std::sync::atomic::AtomicBool;
use metrics_exporter_prometheus::PrometheusHandle;
use warp::Filter;

/// Spawns a server which hosts a `/health` endpoint.
/// Spawns a server which hosts `/health`, `/ready` and `/metrics` endpoints.
pub async fn spawn_server(
addr: impl Into<std::net::SocketAddr> + 'static,
readiness: std::sync::Arc<AtomicBool>,
Expand All @@ -26,7 +26,15 @@ fn routes(

/// Always returns `Ok(200)` at `/health`.
fn health_route() -> impl Filter<Extract = (impl warp::Reply,), Error = warp::Rejection> + Clone {
warp::get().and(warp::path!("health")).map(warp::reply)
warp::get()
.and(warp::path!("health"))
.and_then(|| async move {
Ok::<_, std::convert::Infallible>(
warp::http::Response::builder()
.header(warp::http::header::CONTENT_TYPE, "application/json")
.body("{\"healthy\":\"HEALTHY\"}"),
)
})
}

/// Returns `Ok` if `readiness == true`, or `SERVICE_UNAVAILABLE` otherwise.
Expand All @@ -38,13 +46,22 @@ fn ready_route(
.map(move || -> std::sync::Arc<AtomicBool> { readiness.clone() })
.and_then(|readiness: std::sync::Arc<AtomicBool>| async move {
match readiness.load(std::sync::atomic::Ordering::Relaxed) {
true => Ok::<_, std::convert::Infallible>(warp::http::StatusCode::OK),
false => Ok(warp::http::StatusCode::SERVICE_UNAVAILABLE),
true => Ok::<_, std::convert::Infallible>(
warp::http::Response::builder()
.header(warp::http::header::CONTENT_TYPE, "application/json")
.body("{\"ready\":\"READY\"}"),
),
false => Ok::<_, std::convert::Infallible>(
warp::http::Response::builder()
.status(warp::http::StatusCode::SERVICE_UNAVAILABLE)
.header(warp::http::header::CONTENT_TYPE, "application/json")
.body("{\"ready\":\"NOT READY\"}"),
),
}
})
}

/// Returns Prometheus merics snapshot at `/metrics`.
/// Returns Prometheus metrics snapshot at `/metrics`.
fn metrics_route(
handle: PrometheusHandle,
) -> impl Filter<Extract = (impl warp::Reply,), Error = warp::Rejection> + Clone {
Expand All @@ -71,6 +88,11 @@ mod tests {
let response = warp::test::request().path("/health").reply(&filter).await;

assert_eq!(response.status(), http::StatusCode::OK);
assert_eq!(
response.headers().get(warp::http::header::CONTENT_TYPE).unwrap(),
"application/json"
);
assert_eq!(response.body(), "{\"healthy\":\"HEALTHY\"}");
}

#[tokio::test]
Expand All @@ -81,10 +103,20 @@ mod tests {
let filter = super::routes(readiness.clone(), handle);
let response = warp::test::request().path("/ready").reply(&filter).await;
assert_eq!(response.status(), http::StatusCode::SERVICE_UNAVAILABLE);
assert_eq!(
response.headers().get(warp::http::header::CONTENT_TYPE).unwrap(),
"application/json"
);
assert_eq!(response.body(), "{\"ready\":\"NOT READY\"}");

readiness.store(true, std::sync::atomic::Ordering::Relaxed);
let response = warp::test::request().path("/ready").reply(&filter).await;
assert_eq!(response.status(), http::StatusCode::OK);
assert_eq!(
response.headers().get(warp::http::header::CONTENT_TYPE).unwrap(),
"application/json"
);
assert_eq!(response.body(), "{\"ready\":\"READY\"}");
}

#[tokio::test]
Expand Down