Skip to content

Commit

Permalink
json-rpc: register JsonRpcMetrics with provided Registry
Browse files Browse the repository at this point in the history
  • Loading branch information
bmwill committed Jun 22, 2022
1 parent 0e0b9d6 commit 195818d
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 18 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions crates/sui-json-rpc/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ jsonrpsee = { version = "0.13.1", features = ["full"] }
jsonrpsee-core = "0.13.1"
once_cell = "1.12.0"
prometheus_exporter = "0.8.4"
prometheus = "0.13.1"
anyhow = "1.0.57"
sui-open-rpc = { path = "../sui-open-rpc" }
tracing = "0.1.34"
Expand Down
33 changes: 17 additions & 16 deletions crates/sui-json-rpc/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ use jsonrpsee::http_server::{AccessControlBuilder, HttpServerBuilder, HttpServer
use jsonrpsee_core::middleware::Middleware;
use jsonrpsee_core::server::rpc_module::RpcModule;

use once_cell::sync::Lazy;
use prometheus_exporter::prometheus::{
register_histogram_vec, register_int_counter_vec, HistogramVec, IntCounterVec,
use prometheus::{
register_histogram_vec_with_registry, register_int_counter_vec_with_registry, HistogramVec,
IntCounterVec,
};
use std::env;
use std::net::SocketAddr;
Expand Down Expand Up @@ -39,7 +39,7 @@ pub fn sui_rpc_doc() -> Project {
}

impl JsonRpcServerBuilder {
pub fn new() -> anyhow::Result<Self> {
pub fn new(prometheus_registry: &prometheus::Registry) -> anyhow::Result<Self> {
let mut ac_builder = AccessControlBuilder::default();

if let Ok(value) = env::var("ACCESS_CONTROL_ALLOW_ORIGIN") {
Expand All @@ -53,7 +53,7 @@ impl JsonRpcServerBuilder {

let server_builder = HttpServerBuilder::default()
.set_access_control(acl)
.set_middleware(JsonRpcMetrics::new());
.set_middleware(JsonRpcMetrics::new(prometheus_registry));

let module = RpcModule::new(());

Expand Down Expand Up @@ -100,29 +100,30 @@ struct JsonRpcMetrics {
}

impl JsonRpcMetrics {
pub fn new() -> Self {
static METRICS: Lazy<JsonRpcMetrics> = Lazy::new(|| JsonRpcMetrics {
requests_by_route: register_int_counter_vec!(
pub fn new(registry: &prometheus::Registry) -> Self {
Self {
requests_by_route: register_int_counter_vec_with_registry!(
"rpc_requests_by_route",
"Number of requests by route",
&["route"]
&["route"],
registry,
)
.unwrap(),
req_latency_by_route: register_histogram_vec!(
req_latency_by_route: register_histogram_vec_with_registry!(
"req_latency_by_route",
"Latency of a request by route",
&["route"]
&["route"],
registry,
)
.unwrap(),
errors_by_route: register_int_counter_vec!(
errors_by_route: register_int_counter_vec_with_registry!(
"errors_by_route",
"Number of errors by route",
&["route"]
&["route"],
registry,
)
.unwrap(),
});

Lazy::force(&METRICS).clone()
}
}
}

Expand Down
2 changes: 1 addition & 1 deletion crates/sui-node/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ impl SuiNode {
let json_rpc_service = if config.consensus_config().is_some() {
None
} else {
let mut server = JsonRpcServerBuilder::new()?;
let mut server = JsonRpcServerBuilder::new(&prometheus_registry)?;
server.register_module(ReadApi::new(state.clone()))?;
server.register_module(FullNodeApi::new(state.clone()))?;
server.register_module(BcsApiImpl::new(state.clone()))?;
Expand Down
2 changes: 1 addition & 1 deletion crates/sui/src/bin/rpc-server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ async fn main() -> anyhow::Result<()> {
let client = create_client(&config_path)?;

let address = SocketAddr::new(IpAddr::V4(options.host), options.port);
let mut server = JsonRpcServerBuilder::new()?;
let mut server = JsonRpcServerBuilder::new(prometheus::default_registry())?;
server.register_module(RpcGatewayImpl::new(client.clone()))?;
server.register_module(GatewayReadApiImpl::new(client.clone()))?;
server.register_module(TransactionBuilderImpl::new(client.clone()))?;
Expand Down

0 comments on commit 195818d

Please sign in to comment.