tc_rpc_server/
middleware.rs

1// This file is part of Tetcore.
2
3// Copyright (C) 2020-2021 Parity Technologies (UK) Ltd.
4// SPDX-License-Identifier: GPL-3.0-or-later WITH Classpath-exception-2.0
5
6// This program is free software: you can redistribute it and/or modify
7// it under the terms of the GNU General Public License as published by
8// the Free Software Foundation, either version 3 of the License, or
9// (at your option) any later version.
10
11// This program is distributed in the hope that it will be useful,
12// but WITHOUT ANY WARRANTY; without even the implied warranty of
13// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14// GNU General Public License for more details.
15
16// You should have received a copy of the GNU General Public License
17// along with this program. If not, see <https://www.gnu.org/licenses/>.
18
19//! Middleware for RPC requests.
20
21use tetsy_jsonrpc_core::{
22	Middleware as RequestMiddleware, Metadata,
23	Request, Response, FutureResponse, FutureOutput
24};
25use prometheus_endpoint::{
26	Registry, CounterVec, PrometheusError,
27	Opts, register, U64
28};
29
30use futures::{future::Either, Future};
31
32/// Metrics for RPC middleware
33#[derive(Debug, Clone)]
34pub struct RpcMetrics {
35	rpc_calls: Option<CounterVec<U64>>,
36}
37
38impl RpcMetrics {
39	/// Create an instance of metrics
40	pub fn new(metrics_registry: Option<&Registry>) -> Result<Self, PrometheusError> {
41		Ok(Self {
42			rpc_calls: metrics_registry.map(|r|
43				register(
44					CounterVec::new(
45						Opts::new(
46							"rpc_calls_total",
47							"Number of rpc calls received",
48						),
49						&["protocol"]
50					)?,
51					r,
52				)
53			).transpose()?,
54		})
55	}
56}
57
58/// Middleware for RPC calls
59pub struct RpcMiddleware {
60	metrics: RpcMetrics,
61	transport_label: String,
62}
63
64impl RpcMiddleware {
65	/// Create an instance of middleware.
66	///
67	/// - `metrics`: Will be used to report statistics.
68	/// - `transport_label`: The label that is used when reporting the statistics.
69	pub fn new(metrics: RpcMetrics, transport_label: &str) -> Self {
70		RpcMiddleware {
71			metrics,
72			transport_label: String::from(transport_label),
73		}
74	}
75}
76
77impl<M: Metadata> RequestMiddleware<M> for RpcMiddleware {
78	type Future = FutureResponse;
79	type CallFuture = FutureOutput;
80
81	fn on_request<F, X>(&self, request: Request, meta: M, next: F) -> Either<FutureResponse, X>
82	where
83		F: Fn(Request, M) -> X + Send + Sync,
84		X: Future<Item = Option<Response>, Error = ()> + Send + 'static,
85	{
86		if let Some(ref rpc_calls) = self.metrics.rpc_calls {
87			rpc_calls.with_label_values(&[self.transport_label.as_str()]).inc();
88		}
89
90		Either::B(next(request, meta))
91	}
92}