Add admin rpc to exports metrics (#162)

This commit is contained in:
Bo QIU 2024-08-19 15:18:12 +08:00 committed by GitHub
parent f9120b1e4a
commit 908ee156ca
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
5 changed files with 31 additions and 4 deletions

3
Cargo.lock generated
View File

@ -4620,7 +4620,7 @@ dependencies = [
[[package]] [[package]]
name = "metrics" name = "metrics"
version = "0.1.0" 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 = [ dependencies = [
"chrono", "chrono",
"futures", "futures",
@ -6512,6 +6512,7 @@ dependencies = [
"jsonrpsee", "jsonrpsee",
"merkle_light", "merkle_light",
"merkle_tree", "merkle_tree",
"metrics",
"miner", "miner",
"network", "network",
"serde", "serde",

View File

@ -28,7 +28,7 @@ members = [
resolver = "2" resolver = "2"
[workspace.dependencies] [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] [patch.crates-io]
discv5 = { path = "version-meld/discv5" } discv5 = { path = "version-meld/discv5" }

View File

@ -26,3 +26,4 @@ storage-async = { path = "../storage-async" }
merkle_light = { path = "../../common/merkle_light" } merkle_light = { path = "../../common/merkle_light" }
merkle_tree = { path = "../../common/merkle_tree"} merkle_tree = { path = "../../common/merkle_tree"}
futures-channel = "^0.3" futures-channel = "^0.3"
metrics = { workspace = true }

View File

@ -1,7 +1,7 @@
use crate::types::{LocationInfo, NetworkInfo, PeerInfo}; use crate::types::{LocationInfo, NetworkInfo, PeerInfo};
use jsonrpsee::core::RpcResult; use jsonrpsee::core::RpcResult;
use jsonrpsee::proc_macros::rpc; use jsonrpsee::proc_macros::rpc;
use std::collections::HashMap; use std::collections::{BTreeMap, HashMap};
use sync::{FileSyncInfo, SyncServiceState}; use sync::{FileSyncInfo, SyncServiceState};
#[rpc(server, client, namespace = "admin")] #[rpc(server, client, namespace = "admin")]
@ -48,4 +48,10 @@ pub trait Rpc {
tx_seq: u64, tx_seq: u64,
all_shards: bool, all_shards: bool,
) -> RpcResult<Option<Vec<LocationInfo>>>; ) -> RpcResult<Option<Vec<LocationInfo>>>;
#[method(name = "getMetrics")]
async fn get_metrics(
&self,
maybe_prefix: Option<String>,
) -> RpcResult<BTreeMap<String, String>>;
} }

View File

@ -4,8 +4,9 @@ use crate::{error, Context};
use futures::prelude::*; use futures::prelude::*;
use jsonrpsee::core::async_trait; use jsonrpsee::core::async_trait;
use jsonrpsee::core::RpcResult; use jsonrpsee::core::RpcResult;
use metrics::DEFAULT_REGISTRY;
use network::{multiaddr::Protocol, Multiaddr}; use network::{multiaddr::Protocol, Multiaddr};
use std::collections::HashMap; use std::collections::{BTreeMap, HashMap};
use std::net::IpAddr; use std::net::IpAddr;
use storage::config::all_shards_available; use storage::config::all_shards_available;
use sync::{FileSyncInfo, SyncRequest, SyncResponse, SyncServiceState}; use sync::{FileSyncInfo, SyncRequest, SyncResponse, SyncServiceState};
@ -246,4 +247,22 @@ impl RpcServer for RpcServerImpl {
Ok(None) Ok(None)
} }
} }
async fn get_metrics(
&self,
maybe_prefix: Option<String>,
) -> RpcResult<BTreeMap<String, String>> {
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)
}
} }