From d98886db14be80eed1d805423ebfcc87c8feb18f Mon Sep 17 00:00:00 2001 From: Bryan Stitt Date: Sat, 10 Sep 2022 02:59:07 +0000 Subject: [PATCH] simple status json and prometheus --- web3_proxy/src/app.rs | 7 +++--- web3_proxy/src/frontend/http.rs | 40 ++++++++++++++++++++------------- web3_proxy/src/frontend/mod.rs | 3 ++- web3_proxy/src/metrics.rs | 2 +- 4 files changed, 30 insertions(+), 22 deletions(-) diff --git a/web3_proxy/src/app.rs b/web3_proxy/src/app.rs index 68a7c317..15b61d13 100644 --- a/web3_proxy/src/app.rs +++ b/web3_proxy/src/app.rs @@ -340,7 +340,7 @@ impl Web3ProxyApp { Ok((app, handle)) } - pub fn prometheus_metrics(&self) -> anyhow::Result { + pub fn prometheus_metrics(&self) -> String { let globals = HashMap::new(); // TODO: what globals? should this be the hostname or what? // globals.insert("service", "web3_proxy"); @@ -356,9 +356,8 @@ impl Web3ProxyApp { backend_rpc: &self.open_request_handle_metrics, }; - let serialized = serde_prometheus::to_string(&metrics, Some("web3_proxy"), globals)?; - - Ok(serialized) + serde_prometheus::to_string(&metrics, Some("web3_proxy"), globals) + .expect("prometheus metrics should always serialize") } #[instrument(skip_all)] diff --git a/web3_proxy/src/frontend/http.rs b/web3_proxy/src/frontend/http.rs index b2d305e5..4cc7df64 100644 --- a/web3_proxy/src/frontend/http.rs +++ b/web3_proxy/src/frontend/http.rs @@ -1,5 +1,7 @@ use crate::app::Web3ProxyApp; -use axum::{http::StatusCode, response::IntoResponse, Extension}; +use axum::{http::StatusCode, response::IntoResponse, Extension, Json}; +use moka::future::ConcurrentCacheExt; +use serde_json::json; use std::sync::Arc; use tracing::instrument; @@ -13,23 +15,29 @@ pub async fn health(Extension(app): Extension>) -> impl IntoRe } } +/// Prometheus metrics +/// TODO: when done debugging, remove this and only allow access on a different port +#[instrument(skip_all)] +pub async fn prometheus(Extension(app): Extension>) -> impl IntoResponse { + app.prometheus_metrics() +} + /// Very basic status page /// TODO: replace this with proper stats and monitoring #[instrument(skip_all)] pub async fn status(Extension(app): Extension>) -> impl IntoResponse { - // // TODO: what else should we include? uptime? - // app.pending_transactions.sync(); - // app.user_cache.sync(); - // let body = json!({ - // "total_queries": app.total_queries.load(atomic::Ordering::Acquire), - // "pending_transactions_count": app.pending_transactions.entry_count(), - // "pending_transactions_size": app.pending_transactions.weighted_size(), - // "user_cache_count": app.user_cache.entry_count(), - // "user_cache_size": app.user_cache.weighted_size(), - // "balanced_rpcs": app.balanced_rpcs, - // "private_rpcs": app.private_rpcs, - // }); - // Json(body) - // TODO: only expose this on a different port - app.prometheus_metrics().unwrap() + // TODO: what else should we include? uptime? + app.pending_transactions.sync(); + app.user_cache.sync(); + + let body = json!({ + "pending_transactions_count": app.pending_transactions.entry_count(), + "pending_transactions_size": app.pending_transactions.weighted_size(), + "user_cache_count": app.user_cache.entry_count(), + "user_cache_size": app.user_cache.weighted_size(), + "balanced_rpcs": app.balanced_rpcs, + "private_rpcs": app.private_rpcs, + }); + + Json(body) } diff --git a/web3_proxy/src/frontend/mod.rs b/web3_proxy/src/frontend/mod.rs index 34074aab..f37a5eb3 100644 --- a/web3_proxy/src/frontend/mod.rs +++ b/web3_proxy/src/frontend/mod.rs @@ -50,8 +50,9 @@ pub async fn serve(port: u16, proxy_app: Arc) -> anyhow::Result<() .route("/u/:user_key", post(rpc_proxy_http::user_proxy_web3_rpc)) .route("/u/:user_key", get(rpc_proxy_ws::user_websocket_handler)) .route("/health", get(http::health)) - // TODO: we probably want to remove /status in favor of the separate prometheus thread .route("/status", get(http::status)) + // TODO: make this optional or remove it since it is available on another port + .route("/prometheus", get(http::prometheus)) .route("/login/:user_address", get(users::get_login)) .route("/login/:user_address/:message_eip", get(users::get_login)) .route("/login", post(users::post_login)) diff --git a/web3_proxy/src/metrics.rs b/web3_proxy/src/metrics.rs index ca2c0dab..fbf01786 100644 --- a/web3_proxy/src/metrics.rs +++ b/web3_proxy/src/metrics.rs @@ -44,7 +44,7 @@ pub async fn serve(app: Arc, port: u16) -> anyhow::Result<()> { #[instrument(skip_all)] async fn root(Extension(app): Extension>) -> Response { - let serialized = app.prometheus_metrics().unwrap(); + let serialized = app.prometheus_metrics(); let mut r = serialized.into_response();