rpc: Update jsonrpsee v0.15.1 (#11939)

* Bump jsonrpsee to v0.15.1

Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io>

* Update cargo.lock

Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io>

* rpc-servers: Adjust RpcMiddleware to WS and HTTP traits

Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io>

* rpc/author: Use `SubscriptionSink`

Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io>

* rpc/chain: Use `SubscriptionSink`

Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io>

* rpc/state:  Use `SubscriptionSink`

Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io>

* rpc/finality-grandpa: Use `SubscriptionSink`

Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io>

* rpc/beefy: Use `SubscriptionSink`

Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io>

* client: Extract RPC string result from queries

Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io>

* Apply rust-fmt

Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io>

* Fix warnings

Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io>

* Fix testing

Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io>

* rpc/tests: Remove trailing comma

Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io>

* rpc: Use `SubscriptionResult` for implementations

Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io>

* rpc: Remove comment

Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io>

* rpc: Delegate middleware calls to `RpcMiddleware`

Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io>

* rpc: Remove comment

Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io>

* Revert Cargo.lock

Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io>

* Update Cargo.lock with minimal changes

Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io>

* rpc: Update imports for `SubscriptionResult`

Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io>

* Apply cargo fmt

Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io>

* rpc/tests: Submit raw json requests to validate DenyUnsafe

Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io>
This commit is contained in:
Alexandru Vasile
2022-08-02 20:00:18 +03:00
committed by GitHub
parent 103f770e75
commit 63f847c24f
32 changed files with 201 additions and 136 deletions
+64 -15
View File
@@ -18,11 +18,12 @@
//! RPC middlware to collect prometheus metrics on RPC calls.
use jsonrpsee::core::middleware::Middleware;
use jsonrpsee::core::middleware::{Headers, HttpMiddleware, MethodKind, Params, WsMiddleware};
use prometheus_endpoint::{
register, Counter, CounterVec, HistogramOpts, HistogramVec, Opts, PrometheusError, Registry,
U64,
};
use std::net::SocketAddr;
/// Metrics for RPC middleware storing information about the number of requests started/completed,
/// calls started/completed and their timings.
@@ -134,30 +135,33 @@ impl RpcMiddleware {
pub fn new(metrics: RpcMetrics, transport_label: &'static str) -> Self {
Self { metrics, transport_label }
}
}
impl Middleware for RpcMiddleware {
type Instant = std::time::Instant;
fn on_connect(&self) {
self.metrics.ws_sessions_opened.as_ref().map(|counter| counter.inc());
}
fn on_request(&self) -> Self::Instant {
/// Called when a new JSON-RPC request comes to the server.
fn on_request(&self) -> std::time::Instant {
let now = std::time::Instant::now();
self.metrics.requests_started.with_label_values(&[self.transport_label]).inc();
now
}
fn on_call(&self, name: &str) {
log::trace!(target: "rpc_metrics", "[{}] on_call name={}", self.transport_label, name);
/// Called on each JSON-RPC method call, batch requests will trigger `on_call` multiple times.
fn on_call(&self, name: &str, params: Params, kind: MethodKind) {
log::trace!(
target: "rpc_metrics",
"[{}] on_call name={} params={:?} kind={}",
self.transport_label,
name,
params,
kind,
);
self.metrics
.calls_started
.with_label_values(&[self.transport_label, name])
.inc();
}
fn on_result(&self, name: &str, success: bool, started_at: Self::Instant) {
/// Called on each JSON-RPC method completion, batch requests will trigger `on_result` multiple
/// times.
fn on_result(&self, name: &str, success: bool, started_at: std::time::Instant) {
let micros = started_at.elapsed().as_micros();
log::debug!(
target: "rpc_metrics",
@@ -183,12 +187,57 @@ impl Middleware for RpcMiddleware {
.inc();
}
fn on_response(&self, started_at: Self::Instant) {
/// Called once the JSON-RPC request is finished and response is sent to the output buffer.
fn on_response(&self, _result: &str, started_at: std::time::Instant) {
log::trace!(target: "rpc_metrics", "[{}] on_response started_at={:?}", self.transport_label, started_at);
self.metrics.requests_finished.with_label_values(&[self.transport_label]).inc();
}
}
fn on_disconnect(&self) {
impl WsMiddleware for RpcMiddleware {
type Instant = std::time::Instant;
fn on_connect(&self, _remote_addr: SocketAddr, _headers: &Headers) {
self.metrics.ws_sessions_opened.as_ref().map(|counter| counter.inc());
}
fn on_request(&self) -> Self::Instant {
self.on_request()
}
fn on_call(&self, name: &str, params: Params, kind: MethodKind) {
self.on_call(name, params, kind)
}
fn on_result(&self, name: &str, success: bool, started_at: Self::Instant) {
self.on_result(name, success, started_at)
}
fn on_response(&self, _result: &str, started_at: Self::Instant) {
self.on_response(_result, started_at)
}
fn on_disconnect(&self, _remote_addr: SocketAddr) {
self.metrics.ws_sessions_closed.as_ref().map(|counter| counter.inc());
}
}
impl HttpMiddleware for RpcMiddleware {
type Instant = std::time::Instant;
fn on_request(&self, _remote_addr: SocketAddr, _headers: &Headers) -> Self::Instant {
self.on_request()
}
fn on_call(&self, name: &str, params: Params, kind: MethodKind) {
self.on_call(name, params, kind)
}
fn on_result(&self, name: &str, success: bool, started_at: Self::Instant) {
self.on_result(name, success, started_at)
}
fn on_response(&self, _result: &str, started_at: Self::Instant) {
self.on_response(_result, started_at)
}
}