From 9e97977bbfcb910e65560a74974c97e5785141ba Mon Sep 17 00:00:00 2001 From: "Pengfei(Andy) Zhang" Date: Thu, 26 Sep 2024 13:17:49 -0400 Subject: [PATCH] fix(middleware): change mod visibility to pass build. fix(middleware): customize clone implemention. fix: cleanup unsed Clone and Copy trait. --- Cargo.lock | 1 + crates/pool/Cargo.toml | 1 + crates/pool/src/server/remote/server.rs | 8 +- crates/provider/src/traits/metrics.rs | 2 +- crates/rpc/src/rpc_metrics.rs | 5 +- crates/rpc/src/task.rs | 13 +-- crates/task/src/grpc/grpc_metrics.rs | 5 +- crates/task/src/grpc/mod.rs | 1 + crates/task/src/metrics.rs | 100 ++++++++++++++---------- crates/types/src/task/mod.rs | 4 + crates/types/src/task/traits.rs | 2 +- 11 files changed, 85 insertions(+), 57 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 9b6dd5b6..9f387d26 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -5166,6 +5166,7 @@ dependencies = [ "tonic-build", "tonic-health", "tonic-reflection", + "tower 0.4.13", "tracing", "url", ] diff --git a/crates/pool/Cargo.toml b/crates/pool/Cargo.toml index 62954593..14bcadb9 100644 --- a/crates/pool/Cargo.toml +++ b/crates/pool/Cargo.toml @@ -36,6 +36,7 @@ tokio-util.workspace = true tonic.workspace = true tonic-health.workspace = true tonic-reflection.workspace = true +tower.workspace = true tracing.workspace = true url.workspace = true diff --git a/crates/pool/src/server/remote/server.rs b/crates/pool/src/server/remote/server.rs index ac452786..5f4f4d1b 100644 --- a/crates/pool/src/server/remote/server.rs +++ b/crates/pool/src/server/remote/server.rs @@ -24,7 +24,7 @@ use async_trait::async_trait; use futures_util::StreamExt; use rundler_task::{ grpc::{grpc_metrics::HttpMethodExtractor, protos::from_bytes}, - metrics::{MetricsLayer, RequestMethodNameInfo}, + metrics::MetricsLayer, }; use rundler_types::{ chain::ChainSpec, @@ -81,7 +81,11 @@ pub(crate) async fn spawn_remote_mempool_server( .set_serving::>() .await; - let metrics_layer = MetricsLayer::::new("op_pool_service".to_string(), "http-grpc".to_string()); + let metrics_layer = MetricsLayer::::new( + "op_pool_service".to_string(), + "http-grpc".to_string(), + ); + let handle = tokio::spawn(async move { Server::builder() .layer(metrics_layer) diff --git a/crates/provider/src/traits/metrics.rs b/crates/provider/src/traits/metrics.rs index 2408e665..a0a444cd 100644 --- a/crates/provider/src/traits/metrics.rs +++ b/crates/provider/src/traits/metrics.rs @@ -16,7 +16,7 @@ use rundler_types::task::traits::RequestExtractor; use alloy_json_rpc::RequestPacket; #[derive(Clone, Copy)] -struct AlloyMethodExtractor; +pub struct AlloyMethodExtractor; impl RequestExtractor for RPCMethodExtractor { fn get_method_name(req: &RequestPacket) -> String { diff --git a/crates/rpc/src/rpc_metrics.rs b/crates/rpc/src/rpc_metrics.rs index 08542881..a4015a60 100644 --- a/crates/rpc/src/rpc_metrics.rs +++ b/crates/rpc/src/rpc_metrics.rs @@ -14,11 +14,10 @@ use jsonrpsee::types::Request; use rundler_types::task::traits::RequestExtractor; -#[derive(Copy, Clone)] -struct RPCMethodExtractor; +pub struct RPCMethodExtractor; impl RequestExtractor> for RPCMethodExtractor { - fn get_method_name(req: & Request<'static>) -> String { + fn get_method_name(req: &Request<'static>) -> String { req.method_name().to_string() } } diff --git a/crates/rpc/src/task.rs b/crates/rpc/src/task.rs index 0ff51d59..bdf2df38 100644 --- a/crates/rpc/src/task.rs +++ b/crates/rpc/src/task.rs @@ -16,7 +16,9 @@ use std::{net::SocketAddr, sync::Arc, time::Duration}; use anyhow::{bail, Context}; use async_trait::async_trait; use jsonrpsee::{ - server::{middleware::http::ProxyGetRequestLayer, RpcServiceBuilder, ServerBuilder}, types::Request, RpcModule + server::{middleware::http::ProxyGetRequestLayer, RpcServiceBuilder, ServerBuilder}, + types::Request, + RpcModule, }; use rundler_provider::{EntryPointProvider, Provider}; use rundler_sim::{ @@ -42,10 +44,9 @@ use crate::{ EthApiSettings, UserOperationEventProviderV0_6, UserOperationEventProviderV0_7, }, health::{HealthChecker, SystemApiServer}, - rpc_metrics, + rpc_metrics::RPCMethodExtractor, rundler::{RundlerApi, RundlerApiServer, Settings as RundlerApiSettings}, types::ApiNamespace, - rpc_metrics::RPCMethodExtractor, }; /// RPC server arguments. @@ -187,8 +188,10 @@ where .layer(ProxyGetRequestLayer::new("/health", "system_health")?) .timeout(self.args.rpc_timeout); - let rpc_metric_middleware = - MetricsLayer::::new("rundler-eth-service".to_string(), "rpc".to_string()); + let rpc_metric_middleware = MetricsLayer::>::new( + "rundler-eth-service".to_string(), + "rpc".to_string(), + ); let server = ServerBuilder::default() .set_http_middleware(http_middleware) diff --git a/crates/task/src/grpc/grpc_metrics.rs b/crates/task/src/grpc/grpc_metrics.rs index 762786fe..f166384f 100644 --- a/crates/task/src/grpc/grpc_metrics.rs +++ b/crates/task/src/grpc/grpc_metrics.rs @@ -14,9 +14,8 @@ use rundler_types::task::traits::RequestExtractor; use tonic::codegen::http; -/// http request method extractor. -#[derive(Copy, Clone)] -struct HttpMethodExtractor; +/// http request method extractor. +pub struct HttpMethodExtractor; impl RequestExtractor> for HttpMethodExtractor { fn get_method_name(req: &http::Request) -> String { diff --git a/crates/task/src/grpc/mod.rs b/crates/task/src/grpc/mod.rs index 8f9a557d..9c43a71a 100644 --- a/crates/task/src/grpc/mod.rs +++ b/crates/task/src/grpc/mod.rs @@ -13,6 +13,7 @@ //! Utilities for working with gRPC +/// grpc method extractor implmentation. pub mod grpc_metrics; #[allow(non_snake_case)] pub mod protos; diff --git a/crates/task/src/metrics.rs b/crates/task/src/metrics.rs index d492745e..181b82bc 100644 --- a/crates/task/src/metrics.rs +++ b/crates/task/src/metrics.rs @@ -24,12 +24,12 @@ use rundler_types::task::traits::RequestExtractor; use tower::{Layer, Service}; /// tower network layer: https://github.com/tower-rs/tower/blob/master/guides/building-a-middleware-from-scratch.md -#[derive(Debug, Clone)] +#[derive(Debug)] pub struct MetricsLayer { service_name: String, protocal: String, - _request_extractor_: PhantomData, - _request_type_: PhantomData, + _request_extractor: PhantomData, + _request_type: PhantomData, } impl MetricsLayer @@ -41,8 +41,22 @@ where MetricsLayer { service_name, protocal, - _request_extractor_: PhantomData, - _request_type_: PhantomData, + _request_extractor: PhantomData, + _request_type: PhantomData, + } + } +} + +impl Clone for MetricsLayer +where + T: RequestExtractor, +{ + fn clone(&self) -> Self { + Self { + service_name: self.service_name.clone(), + protocal: self.protocal.clone(), + _request_extractor: PhantomData, + _request_type: PhantomData, } } } @@ -52,8 +66,9 @@ where T: RequestExtractor, { type Service = MetricsMiddleware; + fn layer(&self, service: S) -> Self::Service { - MetricsMiddleware::::new(service, self.service_name.clone(), self.protocal.clone()) + Self::Service::new(service, self.service_name.clone(), self.protocal.clone()) } } @@ -61,9 +76,24 @@ where pub struct MetricsMiddleware { inner: S, service_name: String, - protocal: String, - _request_extractor_: PhantomData, - _request_type_: PhantomData, + protocol: String, + _request_extractor: PhantomData, + _request_type: 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, + } + } } impl MetricsMiddleware @@ -71,23 +101,23 @@ where T: RequestExtractor, { /// Initialize a middleware. - pub fn new(inner: S, service_name: String, protocal: String) -> Self { + pub fn new(inner: S, service_name: String, protocol: String) -> Self { Self { - inner: inner, + inner, service_name: service_name.clone(), - protocal: protocal, - _request_extractor_: PhantomData, - _request_type_: PhantomData, + protocol, + _request_extractor: PhantomData, + _request_type: PhantomData, } } } -impl Service for MetricsMiddleware +impl Service for MetricsMiddleware where - S: Service + Send + Sync + Clone + 'static, - S::Future: Send + Sync + 'static, - T: RequestExtractor + 'static, - Request: Send + Sync + 'static, + S: Service + Send + Clone + 'static, + S::Future: Send + 'static, + T: RequestExtractor + 'static, + R: Send + 'static, { type Response = S::Response; type Error = S::Error; @@ -97,51 +127,37 @@ where self.inner.poll_ready(cx) } - fn call(&mut self, request: Request) -> Self::Future { + fn call(&mut self, request: R) -> Self::Future { let method_name = T::get_method_name(&request); - MethodMetrics::increment_num_requests( - self.service_name.as_str(), - method_name.as_str(), - self.protocal.as_str(), - ); + 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.protocal.as_str(), + self.protocol.as_str(), ); let start = Instant::now(); let mut svc = self.inner.clone(); let service_name = self.service_name.clone(); - let protocal = self.protocal.clone(); + let protocal = self.protocol.clone(); async move { let rsp = svc.call(request).await; MethodMetrics::record_request_latency( - method_name.as_str(), - service_name.as_str(), - protocal.as_str(), + &method_name, + &service_name, + &protocal, start.elapsed(), ); - MethodMetrics::decrement_open_requests( - method_name.as_str(), - service_name.as_str(), - protocal.as_str(), - ); + MethodMetrics::decrement_open_requests(&method_name, &service_name, &protocal); 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, &service_name, &protocal); } rsp } .boxed() } } - -#[derive(Clone)] struct MethodMetrics {} impl MethodMetrics { diff --git a/crates/types/src/task/mod.rs b/crates/types/src/task/mod.rs index ae3f9d3a..28e4ab74 100644 --- a/crates/types/src/task/mod.rs +++ b/crates/types/src/task/mod.rs @@ -12,4 +12,8 @@ // If not, see https://www.gnu.org/licenses/. //! Rundler task traits. +//! +//! This module contains traits related to Rundler tasks. + +/// method extractor trait. pub mod traits; diff --git a/crates/types/src/task/traits.rs b/crates/types/src/task/traits.rs index 95ba2a63..2ab3923c 100644 --- a/crates/types/src/task/traits.rs +++ b/crates/types/src/task/traits.rs @@ -13,7 +13,7 @@ /// Trait to expose request method name. -pub trait RequestExtractor: Copy + Sync + Send { +pub trait RequestExtractor: Sync + Send { /// Get method name. fn get_method_name(request: &R) -> String; }