web3-proxy/web3_proxy/src/frontend/status.rs

84 lines
2.6 KiB
Rust
Raw Normal View History

2022-10-18 00:47:58 +03:00
//! Used by admins for health checks and inspecting global statistics.
//!
//! For ease of development, users can currently access these endponts.
//! They will eventually move to another port.
use super::{FrontendHealthCache, FrontendJsonResponseCache, FrontendResponseCaches};
2023-01-18 07:18:18 +03:00
use crate::app::{Web3ProxyApp, APP_USER_AGENT};
2022-09-10 05:59:07 +03:00
use axum::{http::StatusCode, response::IntoResponse, Extension, Json};
2022-10-27 03:12:42 +03:00
use axum_macros::debug_handler;
2022-09-10 05:59:07 +03:00
use serde_json::json;
2022-09-09 06:53:16 +03:00
use std::sync::Arc;
2022-06-05 22:58:47 +03:00
2022-10-18 00:47:58 +03:00
/// Health check page for load balancers to use.
2022-10-20 01:26:33 +03:00
#[debug_handler]
pub async fn health(
Extension(app): Extension<Arc<Web3ProxyApp>>,
Extension(health_cache): Extension<FrontendHealthCache>,
) -> impl IntoResponse {
let synced = health_cache
.get_with((), async { app.balanced_rpcs.synced() })
.await;
if synced {
2022-06-29 21:22:53 +03:00
(StatusCode::OK, "OK")
} else {
(StatusCode::SERVICE_UNAVAILABLE, ":(")
}
}
/// Easy alerting if backup servers are in use.
pub async fn backups_needed(Extension(app): Extension<Arc<Web3ProxyApp>>) -> impl IntoResponse {
let code = {
let consensus_rpcs = app
.balanced_rpcs
.watch_consensus_rpcs_sender
.borrow()
.clone();
if let Some(ref consensus_rpcs) = consensus_rpcs {
if consensus_rpcs.backups_needed {
StatusCode::INTERNAL_SERVER_ERROR
} else {
StatusCode::OK
}
} else {
// if no consensus, we still "need backups". we just don't have any. which is worse
StatusCode::INTERNAL_SERVER_ERROR
}
};
if matches!(code, StatusCode::OK) {
(code, "no backups needed. :)")
} else {
(code, "backups needed! :(")
}
}
2022-10-18 00:47:58 +03:00
/// Very basic status page.
///
2022-08-05 22:22:23 +03:00
/// TODO: replace this with proper stats and monitoring
2022-10-20 01:26:33 +03:00
#[debug_handler]
2022-11-16 23:17:33 +03:00
pub async fn status(
Extension(app): Extension<Arc<Web3ProxyApp>>,
Extension(response_cache): Extension<FrontendJsonResponseCache>,
2022-11-16 23:17:33 +03:00
) -> impl IntoResponse {
let body = response_cache
.get_with(FrontendResponseCaches::Status, async {
// TODO: what else should we include? uptime, cache hit rates, cpu load, memory used
2023-03-23 01:18:54 +03:00
// TODO: the hostname is probably not going to change. only get once at the start?
2022-11-16 23:17:33 +03:00
let body = json!({
2023-01-18 07:18:18 +03:00
"version": APP_USER_AGENT,
"chain_id": app.config.chain_id,
2022-11-16 23:17:33 +03:00
"balanced_rpcs": app.balanced_rpcs,
"private_rpcs": app.private_rpcs,
2023-03-23 01:18:54 +03:00
"hostname": app.hostname,
2022-11-16 23:17:33 +03:00
});
Arc::new(body)
})
.await;
2022-09-10 05:59:07 +03:00
Json(body)
2022-06-05 22:58:47 +03:00
}