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/// Metrics for the RPC server.
17///
18/// Metrics are divided into two categories:
19/// - Connection metrics: metrics for the connection (e.g. number of connections opened, relevant
20///   for WS and IPC)
21/// - Request metrics: metrics for each RPC method (e.g. number of calls started, time taken to
22///   process a call)
23#[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    /// Creates a new instance of the metrics layer for HTTP.
44    pub(crate) fn http(module: &RpcModule<()>) -> Self {
45        Self::new(module, RpcTransport::Http)
46    }
47
48    /// Creates a new instance of the metrics layer for same port.
49    ///
50    /// Note: currently it's not possible to track transport specific metrics for a server that runs http and ws on the same port: <https://github.com/paritytech/jsonrpsee/issues/1345> until we have this feature we will use the http metrics for this case.
51    pub(crate) fn same_port(module: &RpcModule<()>) -> Self {
52        Self::http(module)
53    }
54
55    /// Creates a new instance of the metrics layer for Ws.
56    pub(crate) fn ws(module: &RpcModule<()>) -> Self {
57        Self::new(module, RpcTransport::WebSocket)
58    }
59
60    /// Creates a new instance of the metrics layer for Ws.
61    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/// Metrics for the RPC server
75#[derive(Default, Clone, Debug)]
76struct RpcServerMetricsInner {
77    /// Connection metrics per transport type
78    connection_metrics: RpcServerConnectionMetrics,
79    /// Call metrics per RPC method
80    call_metrics: HashMap<&'static str, RpcServerCallMetrics>,
81}
82
83/// A [`RpcServiceT`] middleware that captures RPC metrics for the server.
84///
85/// This is created per connection and captures metrics for each request.
86#[derive(Clone, Debug)]
87pub struct RpcRequestMetricsService<S> {
88    /// The metrics collector for RPC requests
89    metrics: RpcRequestMetrics,
90    /// The inner service being wrapped
91    inner: S,
92}
93
94impl<S> RpcRequestMetricsService<S> {
95    pub(crate) fn new(service: S, metrics: RpcRequestMetrics) -> Self {
96        // this instance is kept alive for the duration of the connection
97        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        // update connection metrics, connection closed
126        self.metrics.inner.connection_metrics.connections_closed_total.increment(1);
127    }
128}
129
130/// Response future to update the metrics for a single request/response pair.
131#[pin_project::pin_project]
132pub struct MeteredRequestFuture<F> {
133    #[pin]
134    fut: F,
135    /// time when the request started
136    started_at: Instant,
137    /// metrics for the method call
138    metrics: RpcRequestMetrics,
139    /// the method name if known
140    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            // update transport metrics
160            this.metrics.inner.connection_metrics.requests_finished_total.increment(1);
161            this.metrics.inner.connection_metrics.request_time_seconds.record(elapsed);
162
163            // update call metrics
164            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/// The transport protocol used for the RPC connection.
180#[derive(Debug, Clone, Copy, Eq, PartialEq)]
181pub(crate) enum RpcTransport {
182    Http,
183    WebSocket,
184    Ipc,
185}
186
187impl RpcTransport {
188    /// Returns the string representation of the transport protocol.
189    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    /// Returns the connection metrics for the transport protocol.
198    fn connection_metrics(&self) -> RpcServerConnectionMetrics {
199        RpcServerConnectionMetrics::new_with_labels(&[("transport", self.as_str())])
200    }
201}
202
203/// Metrics for the RPC connections
204#[derive(Metrics, Clone)]
205#[metrics(scope = "rpc_server.connections")]
206struct RpcServerConnectionMetrics {
207    /// The number of connections opened
208    connections_opened_total: Counter,
209    /// The number of connections closed
210    connections_closed_total: Counter,
211    /// The number of requests started
212    requests_started_total: Counter,
213    /// The number of requests finished
214    requests_finished_total: Counter,
215    /// Response for a single request/response pair
216    request_time_seconds: Histogram,
217}
218
219/// Metrics for the RPC calls
220#[derive(Metrics, Clone)]
221#[metrics(scope = "rpc_server.calls")]
222struct RpcServerCallMetrics {
223    /// The number of calls started
224    started_total: Counter,
225    /// The number of successful calls
226    successful_total: Counter,
227    /// The number of failed calls
228    failed_total: Counter,
229    /// Response for a single call
230    time_seconds: Histogram,
231}