Skip to content
This repository has been archived by the owner on Nov 15, 2023. It is now read-only.

Commit

Permalink
Added collectors label
Browse files Browse the repository at this point in the history
  • Loading branch information
grbIzl committed Sep 16, 2020
1 parent 8e1cff9 commit decbef6
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 15 deletions.
29 changes: 18 additions & 11 deletions client/rpc-servers/src/middleware.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,26 +22,30 @@ use jsonrpc_core::{
Middleware as RequestMiddleware, Metadata,
Request, Response, FutureResponse, FutureOutput
};
use prometheus_endpoint::{Registry, Counter, PrometheusError, register, U64};
use prometheus_endpoint::{
Registry, CounterVec, PrometheusError,
Opts, register, U64
};

use futures::{future::Either, Future};

use log::warn;

/// Metrics for RPC middleware
#[derive(Debug, Clone)]
pub struct RpcMetrics {
rpc_calls: Counter<U64>,
rpc_calls: CounterVec<U64>,
}

impl RpcMetrics {
/// Create an instance of metrics
pub fn new(metrics_registry: Option<&Registry>) -> Result<Self, PrometheusError> {
metrics_registry.and_then(|r| {
Some(RpcMetrics {
rpc_calls: register(Counter::new(
"rpc_calls_total",
"Number of rpc calls received",
rpc_calls: register(CounterVec::new(
Opts::new(
"rpc_calls_total",
"Number of rpc calls received",
),
&["protocol"]
).ok()?, r).ok()?,
})
}).ok_or(PrometheusError::Msg("Cannot register metric".to_string()))
Expand All @@ -51,13 +55,16 @@ impl RpcMetrics {
/// Middleware for RPC calls
pub struct RpcMiddleware {
metrics: Option<RpcMetrics>,
transport_label: String,
}

impl RpcMiddleware {
/// Create an instance of middleware
pub fn new(metrics: Option<&RpcMetrics>) -> Self {
/// Create an instance of middleware with provided metrics
/// transport_label is used as a label for Prometheus collector
pub fn new(metrics: Option<&RpcMetrics>, transport_label: &str) -> Self {
RpcMiddleware {
metrics: metrics.cloned()
metrics: metrics.cloned(),
transport_label: String::from(transport_label),
}
}
}
Expand All @@ -72,7 +79,7 @@ impl<M: Metadata> RequestMiddleware<M> for RpcMiddleware {
X: Future<Item = Option<Response>, Error = ()> + Send + 'static,
{
if let Some(ref metrics) = self.metrics {
metrics.rpc_calls.inc();
metrics.rpc_calls.with_label_values(&[self.transport_label.as_str()]).inc();
}

Either::B(next(request, meta))
Expand Down
2 changes: 1 addition & 1 deletion client/service/src/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -559,7 +559,7 @@ pub fn spawn_tasks<TBl, TBackend, TExPool, TRpc, TCl>(
// This is used internally, so don't restrict access to unsafe RPC
let rpc_handlers = RpcHandlers(Arc::new(gen_handler(
sc_rpc::DenyUnsafe::No,
sc_rpc_server::RpcMiddleware::new(rpc_metrics.as_ref())
sc_rpc_server::RpcMiddleware::new(rpc_metrics.as_ref(), "inbrowser")
).into()));

// Telemetry
Expand Down
6 changes: 3 additions & 3 deletions client/service/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -422,7 +422,7 @@ fn start_rpc_servers<
config.rpc_ipc.as_ref().map(|path| sc_rpc_server::start_ipc(
&*path, gen_handler(
sc_rpc::DenyUnsafe::No,
sc_rpc_server::RpcMiddleware::new(rpc_metrics)
sc_rpc_server::RpcMiddleware::new(rpc_metrics, "ipc")
)
)),
maybe_start_server(
Expand All @@ -432,7 +432,7 @@ fn start_rpc_servers<
config.rpc_cors.as_ref(),
gen_handler(
deny_unsafe(&address, &config.rpc_methods),
sc_rpc_server::RpcMiddleware::new(rpc_metrics)
sc_rpc_server::RpcMiddleware::new(rpc_metrics, "http")
),
),
)?.map(|s| waiting::HttpServer(Some(s))),
Expand All @@ -444,7 +444,7 @@ fn start_rpc_servers<
config.rpc_cors.as_ref(),
gen_handler(
deny_unsafe(&address, &config.rpc_methods),
sc_rpc_server::RpcMiddleware::new(rpc_metrics)
sc_rpc_server::RpcMiddleware::new(rpc_metrics, "ws")
),
),
)?.map(|s| waiting::WsServer(Some(s))),
Expand Down

0 comments on commit decbef6

Please sign in to comment.