diff --git a/Cargo.lock b/Cargo.lock index f884311..714294b 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -4620,7 +4620,7 @@ dependencies = [ [[package]] name = "metrics" version = "0.1.0" -source = "git+https://github.com/Conflux-Chain/conflux-rust.git?rev=3ee498ce659e11fd0030bd4a264b7442705ade2b#3ee498ce659e11fd0030bd4a264b7442705ade2b" +source = "git+https://github.com/Conflux-Chain/conflux-rust.git?rev=992ebc5483d937c8f6b883e266f8ed2a67a7fa9a#992ebc5483d937c8f6b883e266f8ed2a67a7fa9a" dependencies = [ "chrono", "futures", @@ -6512,6 +6512,7 @@ dependencies = [ "jsonrpsee", "merkle_light", "merkle_tree", + "metrics", "miner", "network", "serde", diff --git a/Cargo.toml b/Cargo.toml index c5eb364..270c70d 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -28,7 +28,7 @@ members = [ resolver = "2" [workspace.dependencies] -metrics = { git = "https://github.com/Conflux-Chain/conflux-rust.git", rev = "3ee498ce659e11fd0030bd4a264b7442705ade2b" } +metrics = { git = "https://github.com/Conflux-Chain/conflux-rust.git", rev = "992ebc5483d937c8f6b883e266f8ed2a67a7fa9a" } [patch.crates-io] discv5 = { path = "version-meld/discv5" } diff --git a/node/rpc/Cargo.toml b/node/rpc/Cargo.toml index 5f25060..287fcc6 100644 --- a/node/rpc/Cargo.toml +++ b/node/rpc/Cargo.toml @@ -26,3 +26,4 @@ storage-async = { path = "../storage-async" } merkle_light = { path = "../../common/merkle_light" } merkle_tree = { path = "../../common/merkle_tree"} futures-channel = "^0.3" +metrics = { workspace = true } diff --git a/node/rpc/src/admin/api.rs b/node/rpc/src/admin/api.rs index 2ea96c4..1fed305 100644 --- a/node/rpc/src/admin/api.rs +++ b/node/rpc/src/admin/api.rs @@ -1,7 +1,7 @@ use crate::types::{LocationInfo, NetworkInfo, PeerInfo}; use jsonrpsee::core::RpcResult; use jsonrpsee::proc_macros::rpc; -use std::collections::HashMap; +use std::collections::{BTreeMap, HashMap}; use sync::{FileSyncInfo, SyncServiceState}; #[rpc(server, client, namespace = "admin")] @@ -48,4 +48,10 @@ pub trait Rpc { tx_seq: u64, all_shards: bool, ) -> RpcResult>>; + + #[method(name = "getMetrics")] + async fn get_metrics( + &self, + maybe_prefix: Option, + ) -> RpcResult>; } diff --git a/node/rpc/src/admin/impl.rs b/node/rpc/src/admin/impl.rs index cd62779..301adf4 100644 --- a/node/rpc/src/admin/impl.rs +++ b/node/rpc/src/admin/impl.rs @@ -4,8 +4,9 @@ use crate::{error, Context}; use futures::prelude::*; use jsonrpsee::core::async_trait; use jsonrpsee::core::RpcResult; +use metrics::DEFAULT_REGISTRY; use network::{multiaddr::Protocol, Multiaddr}; -use std::collections::HashMap; +use std::collections::{BTreeMap, HashMap}; use std::net::IpAddr; use storage::config::all_shards_available; use sync::{FileSyncInfo, SyncRequest, SyncResponse, SyncServiceState}; @@ -246,4 +247,22 @@ impl RpcServer for RpcServerImpl { Ok(None) } } + + async fn get_metrics( + &self, + maybe_prefix: Option, + ) -> RpcResult> { + let mut result = BTreeMap::new(); + + for (name, metric) in DEFAULT_REGISTRY.read().get_all() { + match &maybe_prefix { + Some(prefix) if !name.starts_with(prefix) => {} + _ => { + result.insert(name.clone(), metric.get_value()); + } + } + } + + Ok(result) + } }