//! A module providing the `JsonRpcErrorCount` metric. use ethers::providers::ProviderError; use serde::Serialize; use std::ops::Deref; /// A metric counting how many times an expression typed std `Result` as /// returned an `Err` variant. /// /// This is a light-weight metric. /// /// By default, `ErrorCount` uses a lock-free `u64` `Counter`, which makes sense /// in multithread scenarios. Non-threaded applications can gain performance by /// using a `std::cell:Cell` instead. #[derive(Clone, Default, Debug, Serialize)] pub struct JsonRpcErrorCount>(pub C); impl Metric> for JsonRpcErrorCount {} impl Enter for JsonRpcErrorCount { type E = (); fn enter(&self) {} } impl OnResult> for JsonRpcErrorCount { /// Unlike the default ErrorCount, this one does not increment for internal jsonrpc errors /// TODO: count errors like this on another helper fn on_result(&self, _: (), r: &Result) -> Advice { match r { Ok(_) => {} Err(ProviderError::JsonRpcClientError(_)) => { self.0.incr(); } Err(_) => { // TODO: count jsonrpc errors } } Advice::Return } } impl Clear for JsonRpcErrorCount { fn clear(&self) { self.0.clear() } } impl Deref for JsonRpcErrorCount { type Target = C; fn deref(&self) -> &Self::Target { &self.0 } }