-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Co-authored-by: Matthias Seitz <[email protected]>
- Loading branch information
Showing
4 changed files
with
99 additions
and
4 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
use jsonrpsee::server::logger::{HttpRequest, Logger, MethodKind, Params, TransportProtocol}; | ||
use reth_metrics::{ | ||
metrics::{self, Counter, Histogram}, | ||
Metrics, | ||
}; | ||
use std::{net::SocketAddr, time::Instant}; | ||
|
||
/// Metrics for the rpc server | ||
#[derive(Metrics, Clone)] | ||
#[metrics(scope = "rpc_server")] | ||
pub(crate) struct RpcServerMetrics { | ||
/// The number of calls started | ||
calls_started: Counter, | ||
/// The number of successful calls | ||
successful_calls: Counter, | ||
/// The number of failed calls | ||
failed_calls: Counter, | ||
/// The number of requests started | ||
requests_started: Counter, | ||
/// The number of requests finished | ||
requests_finished: Counter, | ||
/// The number of ws sessions opened | ||
ws_session_opened: Counter, | ||
/// The number of ws sessions closed | ||
ws_session_closed: Counter, | ||
/// Latency for a single request/response pair | ||
request_latency: Histogram, | ||
/// Latency for a single call | ||
call_latency: Histogram, | ||
} | ||
|
||
impl Logger for RpcServerMetrics { | ||
type Instant = Instant; | ||
fn on_connect( | ||
&self, | ||
_remote_addr: SocketAddr, | ||
_request: &HttpRequest, | ||
transport: TransportProtocol, | ||
) { | ||
match transport { | ||
TransportProtocol::Http => {} | ||
TransportProtocol::WebSocket => self.ws_session_opened.increment(1), | ||
} | ||
} | ||
fn on_request(&self, _transport: TransportProtocol) -> Self::Instant { | ||
self.requests_started.increment(1); | ||
Instant::now() | ||
} | ||
fn on_call( | ||
&self, | ||
_method_name: &str, | ||
_params: Params<'_>, | ||
_kind: MethodKind, | ||
_transport: TransportProtocol, | ||
) { | ||
self.calls_started.increment(1); | ||
} | ||
fn on_result( | ||
&self, | ||
_method_name: &str, | ||
success: bool, | ||
started_at: Self::Instant, | ||
_transport: TransportProtocol, | ||
) { | ||
// capture call duration | ||
self.call_latency.record(started_at.elapsed().as_millis() as f64); | ||
if !success { | ||
self.failed_calls.increment(1); | ||
} else { | ||
self.successful_calls.increment(1); | ||
} | ||
} | ||
fn on_response(&self, _result: &str, started_at: Self::Instant, _transport: TransportProtocol) { | ||
// capture request latency for this request/response pair | ||
self.request_latency.record(started_at.elapsed().as_millis() as f64); | ||
self.requests_finished.increment(1); | ||
} | ||
fn on_disconnect(&self, _remote_addr: SocketAddr, transport: TransportProtocol) { | ||
match transport { | ||
TransportProtocol::Http => {} | ||
TransportProtocol::WebSocket => self.ws_session_closed.increment(1), | ||
} | ||
} | ||
} |