From 38c733723488bb63472d95ddbae939d8fdd9b112 Mon Sep 17 00:00:00 2001 From: Andy Date: Fri, 27 Sep 2024 15:21:33 -0400 Subject: [PATCH] fix(middleware): change mod visibility to pass build. (#815) --- 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 | 126 +++++++++++++----------- crates/types/src/task/mod.rs | 4 + crates/types/src/task/traits.rs | 2 +- 9 files changed, 96 insertions(+), 70 deletions(-) diff --git a/crates/pool/src/server/remote/server.rs b/crates/pool/src/server/remote/server.rs index ac452786f..5f4f4d1b8 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 2408e6653..a0a444cdd 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 085428818..a4015a606 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 0ff51d596..bdf2df382 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 762786fef..f166384f4 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 8f9a557da..9c43a71a4 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 d492745eb..490f2a1b7 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, + protocol: String, + _request_extractor: PhantomData, + _request_type: PhantomData, } impl MetricsLayer @@ -37,12 +37,26 @@ where T: RequestExtractor, { /// Initialize a network layer wrappers the metric middleware. - pub fn new(service_name: String, protocal: String) -> Self { + pub fn new(service_name: String, protocol: String) -> Self { MetricsLayer { service_name, - protocal, - _request_extractor_: PhantomData, - _request_type_: PhantomData, + protocol, + _request_extractor: PhantomData, + _request_type: 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, } } } @@ -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.protocol.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,76 +127,62 @@ 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 protocol = 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, + &protocol, 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, &protocol); 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, &protocol); } rsp } .boxed() } } - -#[derive(Clone)] struct MethodMetrics {} impl MethodMetrics { - fn increment_num_requests(method_name: &str, service_name: &str, protocal: &str) { - metrics::counter!("num_requests", "method_name" => method_name.to_string(), "service_name" => service_name.to_string(), "protocal" => protocal.to_string()).increment(1) + 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, protocal: &str) { - metrics::gauge!("open_requests", "method_name" => method_name.to_string(), "service_name" => service_name.to_string(), "protocal" => protocal.to_string()).increment(1_f64) + 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, 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 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, 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 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 record_request_latency( method_name: &str, service_name: &str, - protocal: &str, + protocol: &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) + metrics::histogram!("request_latency", "method_name" => method_name.to_string(), "service_name" => service_name.to_string(), "protocol" => protocol.to_string()).record(latency) } } diff --git a/crates/types/src/task/mod.rs b/crates/types/src/task/mod.rs index ae3f9d3a0..28e4ab747 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 95ba2a639..2ab3923c2 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; }