diff --git a/crates/pool/src/server/remote/server.rs b/crates/pool/src/server/remote/server.rs index fe54e908..b5befa78 100644 --- a/crates/pool/src/server/remote/server.rs +++ b/crates/pool/src/server/remote/server.rs @@ -22,8 +22,10 @@ use std::{ use async_trait::async_trait; use ethers::types::{Address, H256}; use futures_util::StreamExt; -use rundler_task::{metrics::{MetricsLayer, RequestMethoedNameInfo}, grpc::{grpc_metrics::GrpcMetricsLayer, protos::from_bytes}}; - +use rundler_task::{ + grpc::{grpc_metrics::GrpcMetricsLayer, protos::from_bytes}, + metrics::{MetricsLayer, RequestMethodNameInfo}, +}; use rundler_types::{ chain::ChainSpec, pool::{Pool, Reputation}, diff --git a/crates/rpc/src/rpc_metrics.rs b/crates/rpc/src/rpc_metrics.rs index 4b51774d..56800211 100644 --- a/crates/rpc/src/rpc_metrics.rs +++ b/crates/rpc/src/rpc_metrics.rs @@ -11,11 +11,11 @@ // You should have received a copy of the GNU General Public License along with Rundler. // If not, see https://www.gnu.org/licenses/. -use rundler_task::metrics::RequestMethoedNameInfo; use jsonrpsee::types::Request; +use rundler_task::metrics::RequestMethodNameInfo; -impl RequestMethoedNameInfo for Request { +impl RequestMethodNameInfo for Request { fn get_method_name(&self) -> String { self.method_name().to_string() } -} \ No newline at end of file +} diff --git a/crates/rpc/src/task.rs b/crates/rpc/src/task.rs index d8434f52..bea67b0f 100644 --- a/crates/rpc/src/task.rs +++ b/crates/rpc/src/task.rs @@ -24,7 +24,9 @@ use rundler_sim::{ EstimationSettings, FeeEstimator, GasEstimatorV0_6, GasEstimatorV0_7, PrecheckSettings, }; use rundler_task::{ - metrics::MetricsLayer, server::{format_socket_addr, HealthCheck}, Task + metrics::MetricsLayer, + server::{format_socket_addr, HealthCheck}, + Task, }; use rundler_types::{ builder::Builder, chain::ChainSpec, pool::Pool, v0_6::UserOperation as UserOperationV0_6, diff --git a/crates/task/src/grpc/grpc_metrics.rs b/crates/task/src/grpc/grpc_metrics.rs index 2dc2543d..b9bf1888 100644 --- a/crates/task/src/grpc/grpc_metrics.rs +++ b/crates/task/src/grpc/grpc_metrics.rs @@ -11,13 +11,13 @@ // You should have received a copy of the GNU General Public License along with Rundler. // If not, see https://www.gnu.org/licenses/. - use tonic::codegen::http; -use crate::metrics::RequestMethoedNameInfo; -impl RequestMethoedNameInfo for http::Request{ +use crate::metrics::RequestMethodNameInfo; + +impl RequestMethodNameInfo for http::Request { fn get_method_name(&self) -> String { let method_name = self.uri().path().split('/').last().unwrap_or("unknown"); method_name.to_string() } -} \ No newline at end of file +} diff --git a/crates/task/src/lib.rs b/crates/task/src/lib.rs index 809805d8..5cb280f0 100644 --- a/crates/task/src/lib.rs +++ b/crates/task/src/lib.rs @@ -21,8 +21,8 @@ pub mod block_watcher; pub mod grpc; -pub mod server; pub mod metrics; +pub mod server; mod task; pub use task::*; diff --git a/crates/task/src/metrics.rs b/crates/task/src/metrics.rs index 7d9a4c2a..48aef98e 100644 --- a/crates/task/src/metrics.rs +++ b/crates/task/src/metrics.rs @@ -22,7 +22,7 @@ use futures::{future::BoxFuture, FutureExt}; use tower::{Layer, Service}; /// Trait to expose request method name. -pub trait RequestMethoedNameInfo { +pub trait RequestMethodNameInfo { /// Get method name. fn get_method_name(&self) -> String; } @@ -34,7 +34,6 @@ pub struct MetricsLayer { protocal: String, } - impl MetricsLayer { /// Initialize a network layer wrappers the metric middleware. pub fn new(service_name: String, protocal: String) -> Self { @@ -45,8 +44,7 @@ impl MetricsLayer { } } -impl Layer for MetricsLayer -{ +impl Layer for MetricsLayer { type Service = MetricsMiddleware; fn layer(&self, service: S) -> Self::Service { MetricsMiddleware::new(service, self.service_name.clone(), self.protocal.clone()) @@ -60,8 +58,7 @@ pub struct MetricsMiddleware { protocal: String, } -impl MetricsMiddleware -{ +impl MetricsMiddleware { /// Initialize a middleware. pub fn new(inner: S, service_name: String, protocal: String) -> Self { Self { @@ -75,8 +72,8 @@ impl MetricsMiddleware impl Service for MetricsMiddleware where S: Service + Send + Sync + Clone + 'static, - S::Future : Send + Sync + 'static, - Request: RequestMethoedNameInfo + Send + Sync + 'static, + S::Future: Send + Sync + 'static, + Request: RequestMethodNameInfo + Send + Sync + 'static, { type Response = S::Response; type Error = S::Error; @@ -86,10 +83,18 @@ where self.inner.poll_ready(cx) } - fn call(& mut self, request: Request) -> Self::Future { + fn call(&mut self, request: Request) -> Self::Future { let method_name = request.get_method_name(); - MethodMetrics::increment_num_requests(self.service_name.as_str(), method_name.as_str(), self.protocal.as_str()); - MethodMetrics::increment_open_requests(self.service_name.as_str(), method_name.as_str(), self.protocal.as_str()); + MethodMetrics::increment_num_requests( + self.service_name.as_str(), + method_name.as_str(), + self.protocal.as_str(), + ); + MethodMetrics::increment_open_requests( + self.service_name.as_str(), + method_name.as_str(), + self.protocal.as_str(), + ); let start = Instant::now(); let mut svc = self.inner.clone(); @@ -97,16 +102,28 @@ where let protocal = self.protocal.clone(); async move { let rsp = svc.call(request).await; - MethodMetrics::record_request_latency(method_name.as_str(), service_name.as_str(), protocal.as_str(), start.elapsed()); - MethodMetrics::decrement_open_requests(method_name.as_str(), service_name.as_str(), protocal.as_str()); + MethodMetrics::record_request_latency( + method_name.as_str(), + service_name.as_str(), + protocal.as_str(), + start.elapsed(), + ); + MethodMetrics::decrement_open_requests( + method_name.as_str(), + service_name.as_str(), + protocal.as_str(), + ); if rsp.is_err() { - MethodMetrics::increment_error_count(method_name.as_str(), service_name.as_str(), protocal.as_str()); + MethodMetrics::increment_error_count( + method_name.as_str(), + service_name.as_str(), + protocal.as_str(), + ); } rsp } .boxed() } - } #[derive(Clone)] @@ -121,15 +138,20 @@ impl MethodMetrics { metrics::gauge!("open_requests", "method_name" => method_name.to_string(), "service_name" => service_name.to_string(), "protocal" => protocal.to_string()).increment(1_f64) } - fn decrement_open_requests( method_name: &str, service_name: &str, protocal: &str) { + fn decrement_open_requests(method_name: &str, service_name: &str, protocal: &str) { metrics::gauge!("open_requests", "method_name" => method_name.to_string(), "service_name" => service_name.to_string(), "protocal" => protocal.to_string()).decrement(1_f64) } - fn increment_error_count( method_name: &str, service_name: &str, protocal: &str) { + fn increment_error_count(method_name: &str, service_name: &str, protocal: &str) { metrics::counter!("open_requests", "method_name" => method_name.to_string(), "service_name" => service_name.to_string(), "protocal" => protocal.to_string()).increment(1) } - fn record_request_latency( method_name: &str, service_name: &str, protocal: &str, latency: Duration) { + fn record_request_latency( + method_name: &str, + service_name: &str, + protocal: &str, + latency: Duration, + ) { metrics::histogram!("request_latency", "method_name" => method_name.to_string(), "service_name" => service_name.to_string(), "protocal" => protocal.to_string()).record(latency) } }