From 2d289da62134a3491bf1040d8341edd6b0534a7c Mon Sep 17 00:00:00 2001 From: "Pengfei(Andy) Zhang" Date: Wed, 2 Oct 2024 12:55:16 -0400 Subject: [PATCH] fix: meger with master. --- .gitignore | 1 - crates/rpc/src/lib.rs | 1 + crates/rpc/src/rpc_metrics.rs | 2 +- crates/rpc/src/task.rs | 2 +- crates/task/src/metrics.rs | 211 ---------------------------------- 5 files changed, 3 insertions(+), 214 deletions(-) delete mode 100644 crates/task/src/metrics.rs diff --git a/.gitignore b/.gitignore index e6721fe63..728fbc633 100644 --- a/.gitignore +++ b/.gitignore @@ -19,4 +19,3 @@ # Release artifacts dist/ -Cargo.lock diff --git a/crates/rpc/src/lib.rs b/crates/rpc/src/lib.rs index 4c983e400..e9ae7562a 100644 --- a/crates/rpc/src/lib.rs +++ b/crates/rpc/src/lib.rs @@ -40,3 +40,4 @@ pub use task::{Args as RpcTaskArgs, RpcTask}; mod types; mod utils; +mod rpc_metrics; \ No newline at end of file diff --git a/crates/rpc/src/rpc_metrics.rs b/crates/rpc/src/rpc_metrics.rs index 88c6e9472..a5af9073b 100644 --- a/crates/rpc/src/rpc_metrics.rs +++ b/crates/rpc/src/rpc_metrics.rs @@ -90,7 +90,7 @@ where ErrorCode::MethodNotFound => { (HttpCode::FourHundreds, RpcCode::MethodNotFound) } - ErrorCode::ServerIsBusy => (HttpCode::FiveHundreds, RpcCode::ServerIsBusy), + ErrorCode::ServerIsBusy => (HttpCode::FiveHundreds, RpcCode::ResourceExhausted), ErrorCode::InvalidParams => { (HttpCode::FourHundreds, RpcCode::InvalidParams) } diff --git a/crates/rpc/src/task.rs b/crates/rpc/src/task.rs index ce6e9c45d..52e6453f1 100644 --- a/crates/rpc/src/task.rs +++ b/crates/rpc/src/task.rs @@ -194,7 +194,7 @@ where RpcMetricsMiddlewareLayer::new("rundler-eth-service".to_string()); let server = ServerBuilder::default() - .set_rpc_middleware(metric_middleware) + .set_rpc_middleware(rpc_metric_middleware) .set_http_middleware(http_middleware) .max_connections(self.args.max_connections) // Set max request body size to 2x the max transaction size as none of our diff --git a/crates/task/src/metrics.rs b/crates/task/src/metrics.rs deleted file mode 100644 index f1bfa0d5f..000000000 --- a/crates/task/src/metrics.rs +++ /dev/null @@ -1,211 +0,0 @@ -// // This file is part of Rundler. -// // -// // Rundler is free software: you can redistribute it and/or modify it under the -// // terms of the GNU Lesser General Public License as published by the Free Software -// // Foundation, either version 3 of the License, or (at your option) any later version. -// // -// // Rundler is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; -// // without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. -// // See the GNU General Public License for more details. -// // -// // You should have received a copy of the GNU General Public License along with Rundler. -// // If not, see https://www.gnu.org/licenses/. - -// //! Middleware for recording metrics for requests. - -// use std::{ -// marker::PhantomData, -// task::{Context, Poll}, -// time::{Duration, Instant}, -// }; - -// use futures::{future::BoxFuture, FutureExt}; -// use tower::{Layer, Service}; - -// /// tower network layer: https://github.com/tower-rs/tower/blob/master/guides/building-a-middleware-from-scratch.md -// #[derive(Debug)] -// pub struct MetricsLayer { -// service_name: String, -// protocol: String, -// _request_extractor: PhantomData, -// _request_type: PhantomData, -// _response_extractor: PhantomData, -// } - -// impl MetricsLayer -// where -// T: RequestExtractor, -// { -// /// Initialize a network layer wrappers the metric middleware. -// pub fn new(service_name: String, protocol: String) -> Self { -// MetricsLayer { -// service_name: service_name, -// protocol: protocol, -// _request_extractor: PhantomData, -// _request_type: PhantomData, -// _response_extractor: PhantomData, -// } -// } -// } - -// impl Clone for MetricsLayer -// where -// T: RequestExtractor, -// { -// fn clone(&self) -> Self { -// Self { -// service_name: self.service_name.clone(), -// protocol: self.protocol.clone(), -// _request_extractor: PhantomData, -// _request_type: PhantomData, -// _response_extractor: PhantomData, -// } -// } -// } - -// impl Layer for MetricsLayer -// where -// T: RequestExtractor, -// { -// type Service = MetricsMiddleware; -// fn layer(&self, service: S) -> Self::Service { -// Self::Service::new(service, self.service_name.clone(), self.protocol.clone()) -// } -// } - -// /// Middleware implementation. -// pub struct MetricsMiddleware { -// inner: S, -// service_name: String, -// protocol: String, -// _request_extractor: PhantomData, -// _request_type: PhantomData, -// _response_extractor: PhantomData, -// } - -// impl Clone for MetricsMiddleware -// where -// S: Clone, -// { -// fn clone(&self) -> Self { -// Self { -// inner: self.inner.clone(), -// service_name: self.service_name.clone(), -// protocol: self.protocol.clone(), -// _request_extractor: PhantomData, -// _request_type: PhantomData, -// _response_extractor: PhantomData, -// } -// } -// } - -// impl MetricsMiddleware -// where -// T: RequestExtractor, -// { -// /// Initialize a middleware. -// pub fn new(inner: S, service_name: String, protocol: String) -> Self { -// Self { -// inner, -// service_name: service_name.clone(), -// protocol: protocol, -// _request_extractor: PhantomData, -// _request_type: PhantomData, -// _response_extractor: PhantomData, -// } -// } -// } - -// impl Service for MetricsMiddleware -// where -// S: Service + Send + Sync + Clone + 'static, -// S::Future: Send + Sync + 'static, -// T: RequestExtractor + 'static, -// R: Send + Sync + 'static, -// RE: ResponseExtractor> + Send + Sync + 'static, -// { -// type Response = S::Response; -// type Error = S::Error; -// type Future = BoxFuture<'static, Result>; - -// fn poll_ready(&mut self, cx: &mut Context<'_>) -> Poll> { -// self.inner.poll_ready(cx) -// } - -// fn call(&mut self, request: R) -> Self::Future { -// let method_name = T::get_method_name(&request); - -// MethodMetrics::increment_num_requests(&self.service_name, &method_name, &self.protocol); -// MethodMetrics::increment_open_requests( -// self.service_name.as_str(), -// method_name.as_str(), -// self.protocol.as_str(), -// ); - -// let start = Instant::now(); -// let mut svc = self.inner.clone(); -// let service_name = self.service_name.clone(); -// let protocol = self.protocol.clone(); -// async move { -// let rsp = svc.call(request).await; -// MethodMetrics::record_request_latency( -// &method_name, -// &service_name, -// &protocol, -// start.elapsed(), -// ); -// MethodMetrics::decrement_open_requests(&method_name, &service_name, &protocol); -// let http_status_code = RE::get_http_status_code(&rsp); -// let rpc_status_code = RE::get_rpc_status_code(&rsp); -// MethodMetrics::increment_http_response_code( -// &method_name, -// &service_name, -// &http_status_code, -// ); -// MethodMetrics::increment_rpc_response_code( -// &method_name, -// &service_name, -// &rpc_status_code, -// ); - -// rsp -// } -// .boxed() -// } -// } -// struct MethodMetrics {} - -// impl MethodMetrics { -// fn increment_num_requests(method_name: &str, service_name: &str, protocol: &str) { -// metrics::counter!("num_requests", "method_name" => method_name.to_string(), "service_name" => service_name.to_string(), "protocol" => protocol.to_string()).increment(1) -// } - -// fn increment_open_requests(method_name: &str, service_name: &str, protocol: &str) { -// metrics::gauge!("open_requests", "method_name" => method_name.to_string(), "service_name" => service_name.to_string(), "protocol" => protocol.to_string()).increment(1_f64) -// } - -// fn decrement_open_requests(method_name: &str, service_name: &str, protocol: &str) { -// metrics::gauge!("open_requests", "method_name" => method_name.to_string(), "service_name" => service_name.to_string(), "protocol" => protocol.to_string()).decrement(1_f64) -// } - -// fn increment_error_count(method_name: &str, service_name: &str, protocol: &str) { -// metrics::counter!("open_requests", "method_name" => method_name.to_string(), "service_name" => service_name.to_string(), "protocol" => protocol.to_string()).increment(1) -// } - -// fn increment_http_response_code(method_name: &str, service_name: &str, http_status_code: &str) { -// metrics::counter!("http_response_status", "method_name" => method_name.to_string(), "service_name" => service_name.to_string(), "protocol" => "http", "response_code" => http_status_code.to_string()).increment(1) -// } - -// fn increment_rpc_response_code(method_name: &str, service_name: &str, rpc_status_code: &str) { -// metrics::counter!("rpc_response_status", "method_name" => method_name.to_string(), "service_name" => service_name.to_string(), "protocol" => "rpc", "response_code" => rpc_status_code.to_string()).increment(1) -// } - -// fn record_request_latency( -// method_name: &str, -// service_name: &str, -// protocol: &str, -// latency: Duration, -// ) { -// metrics::histogram!("request_latency", "method_name" => method_name.to_string(), "service_name" => service_name.to_string(), "protocol" => protocol.to_string()).record(latency) -// } -// }