reth_rpc_builder/
metrics.rs
1use jsonrpsee::{server::middleware::rpc::RpcServiceT, types::Request, MethodResponse, RpcModule};
2use reth_metrics::{
3 metrics::{Counter, Histogram},
4 Metrics,
5};
6use std::{
7 collections::HashMap,
8 future::Future,
9 pin::Pin,
10 sync::Arc,
11 task::{Context, Poll},
12 time::Instant,
13};
14use tower::Layer;
15
16#[derive(Default, Debug, Clone)]
24pub(crate) struct RpcRequestMetrics {
25 inner: Arc<RpcServerMetricsInner>,
26}
27
28impl RpcRequestMetrics {
29 pub(crate) fn new(module: &RpcModule<()>, transport: RpcTransport) -> Self {
30 Self {
31 inner: Arc::new(RpcServerMetricsInner {
32 connection_metrics: transport.connection_metrics(),
33 call_metrics: module
34 .method_names()
35 .map(|method| {
36 (method, RpcServerCallMetrics::new_with_labels(&[("method", method)]))
37 })
38 .collect(),
39 }),
40 }
41 }
42
43 pub(crate) fn http(module: &RpcModule<()>) -> Self {
45 Self::new(module, RpcTransport::Http)
46 }
47
48 pub(crate) fn same_port(module: &RpcModule<()>) -> Self {
52 Self::http(module)
53 }
54
55 pub(crate) fn ws(module: &RpcModule<()>) -> Self {
57 Self::new(module, RpcTransport::WebSocket)
58 }
59
60 pub(crate) fn ipc(module: &RpcModule<()>) -> Self {
62 Self::new(module, RpcTransport::Ipc)
63 }
64}
65
66impl<S> Layer<S> for RpcRequestMetrics {
67 type Service = RpcRequestMetricsService<S>;
68
69 fn layer(&self, inner: S) -> Self::Service {
70 RpcRequestMetricsService::new(inner, self.clone())
71 }
72}
73
74#[derive(Default, Clone, Debug)]
76struct RpcServerMetricsInner {
77 connection_metrics: RpcServerConnectionMetrics,
79 call_metrics: HashMap<&'static str, RpcServerCallMetrics>,
81}
82
83#[derive(Clone, Debug)]
87pub struct RpcRequestMetricsService<S> {
88 metrics: RpcRequestMetrics,
90 inner: S,
92}
93
94impl<S> RpcRequestMetricsService<S> {
95 pub(crate) fn new(service: S, metrics: RpcRequestMetrics) -> Self {
96 metrics.inner.connection_metrics.connections_opened_total.increment(1);
98 Self { inner: service, metrics }
99 }
100}
101
102impl<'a, S> RpcServiceT<'a> for RpcRequestMetricsService<S>
103where
104 S: RpcServiceT<'a> + Send + Sync + Clone + 'static,
105{
106 type Future = MeteredRequestFuture<S::Future>;
107
108 fn call(&self, req: Request<'a>) -> Self::Future {
109 self.metrics.inner.connection_metrics.requests_started_total.increment(1);
110 let call_metrics = self.metrics.inner.call_metrics.get_key_value(req.method.as_ref());
111 if let Some((_, call_metrics)) = &call_metrics {
112 call_metrics.started_total.increment(1);
113 }
114 MeteredRequestFuture {
115 fut: self.inner.call(req),
116 started_at: Instant::now(),
117 metrics: self.metrics.clone(),
118 method: call_metrics.map(|(method, _)| *method),
119 }
120 }
121}
122
123impl<S> Drop for RpcRequestMetricsService<S> {
124 fn drop(&mut self) {
125 self.metrics.inner.connection_metrics.connections_closed_total.increment(1);
127 }
128}
129
130#[pin_project::pin_project]
132pub struct MeteredRequestFuture<F> {
133 #[pin]
134 fut: F,
135 started_at: Instant,
137 metrics: RpcRequestMetrics,
139 method: Option<&'static str>,
141}
142
143impl<F> std::fmt::Debug for MeteredRequestFuture<F> {
144 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
145 f.write_str("MeteredRequestFuture")
146 }
147}
148
149impl<F: Future<Output = MethodResponse>> Future for MeteredRequestFuture<F> {
150 type Output = F::Output;
151
152 fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
153 let this = self.project();
154
155 let res = this.fut.poll(cx);
156 if let Poll::Ready(resp) = &res {
157 let elapsed = this.started_at.elapsed().as_secs_f64();
158
159 this.metrics.inner.connection_metrics.requests_finished_total.increment(1);
161 this.metrics.inner.connection_metrics.request_time_seconds.record(elapsed);
162
163 if let Some(call_metrics) =
165 this.method.and_then(|method| this.metrics.inner.call_metrics.get(method))
166 {
167 call_metrics.time_seconds.record(elapsed);
168 if resp.is_success() {
169 call_metrics.successful_total.increment(1);
170 } else {
171 call_metrics.failed_total.increment(1);
172 }
173 }
174 }
175 res
176 }
177}
178
179#[derive(Debug, Clone, Copy, Eq, PartialEq)]
181pub(crate) enum RpcTransport {
182 Http,
183 WebSocket,
184 Ipc,
185}
186
187impl RpcTransport {
188 pub(crate) const fn as_str(&self) -> &'static str {
190 match self {
191 Self::Http => "http",
192 Self::WebSocket => "ws",
193 Self::Ipc => "ipc",
194 }
195 }
196
197 fn connection_metrics(&self) -> RpcServerConnectionMetrics {
199 RpcServerConnectionMetrics::new_with_labels(&[("transport", self.as_str())])
200 }
201}
202
203#[derive(Metrics, Clone)]
205#[metrics(scope = "rpc_server.connections")]
206struct RpcServerConnectionMetrics {
207 connections_opened_total: Counter,
209 connections_closed_total: Counter,
211 requests_started_total: Counter,
213 requests_finished_total: Counter,
215 request_time_seconds: Histogram,
217}
218
219#[derive(Metrics, Clone)]
221#[metrics(scope = "rpc_server.calls")]
222struct RpcServerCallMetrics {
223 started_total: Counter,
225 successful_total: Counter,
227 failed_total: Counter,
229 time_seconds: Histogram,
231}