Skip to content

Commit

Permalink
feat(middleware): fix typo.
Browse files Browse the repository at this point in the history
  • Loading branch information
andysim3d committed Sep 24, 2024
1 parent 149a740 commit 0480067
Show file tree
Hide file tree
Showing 6 changed files with 55 additions and 29 deletions.
6 changes: 4 additions & 2 deletions crates/pool/src/server/remote/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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},
Expand Down
6 changes: 3 additions & 3 deletions crates/rpc/src/rpc_metrics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}
}
}
4 changes: 3 additions & 1 deletion crates/rpc/src/task.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
8 changes: 4 additions & 4 deletions crates/task/src/grpc/grpc_metrics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Body> RequestMethoedNameInfo for http::Request<Body>{
use crate::metrics::RequestMethodNameInfo;

impl<Body> RequestMethodNameInfo for http::Request<Body> {
fn get_method_name(&self) -> String {
let method_name = self.uri().path().split('/').last().unwrap_or("unknown");
method_name.to_string()
}
}
}
2 changes: 1 addition & 1 deletion crates/task/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::*;
58 changes: 40 additions & 18 deletions crates/task/src/metrics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand All @@ -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 {
Expand All @@ -45,8 +44,7 @@ impl MetricsLayer {
}
}

impl<S> Layer<S> for MetricsLayer
{
impl<S> Layer<S> for MetricsLayer {
type Service = MetricsMiddleware<S>;
fn layer(&self, service: S) -> Self::Service {
MetricsMiddleware::new(service, self.service_name.clone(), self.protocal.clone())
Expand All @@ -60,8 +58,7 @@ pub struct MetricsMiddleware<S> {
protocal: String,
}

impl<S> MetricsMiddleware<S>
{
impl<S> MetricsMiddleware<S> {
/// Initialize a middleware.
pub fn new(inner: S, service_name: String, protocal: String) -> Self {
Self {
Expand All @@ -75,8 +72,8 @@ impl<S> MetricsMiddleware<S>
impl<S, Request> Service<Request> for MetricsMiddleware<S>
where
S: Service<Request> + 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;
Expand All @@ -86,27 +83,47 @@ 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();
let service_name = self.service_name.clone();
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)]
Expand All @@ -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)
}
}

0 comments on commit 0480067

Please sign in to comment.