Add metrics printer

This commit is contained in:
asonix 2022-11-19 17:45:01 -06:00
parent c322798ba3
commit f892a50f2c
6 changed files with 29 additions and 9 deletions

8
Cargo.lock generated
View file

@ -499,9 +499,9 @@ dependencies = [
[[package]] [[package]]
name = "background-jobs-actix" name = "background-jobs-actix"
version = "0.14.0" version = "0.14.1"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ea433afb3fbbb6dc2614bad9f6671517f6cffcd523981e568cdb595dc7fa6399" checksum = "3aba1bc1cdff87161a6e2e00bf9dc1712412ee926a065d96cc0a03dc851b5fd3"
dependencies = [ dependencies = [
"actix-rt", "actix-rt",
"anyhow", "anyhow",
@ -520,9 +520,9 @@ dependencies = [
[[package]] [[package]]
name = "background-jobs-core" name = "background-jobs-core"
version = "0.14.0" version = "0.14.1"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b3071bf7a5e46638085076957dc189b2b0b147cd279eb09510b7af54e95085ef" checksum = "1274e49ae8eff1fc6b4943660e59ce2f2e13e65a23a707924a50a40c7b94fc4d"
dependencies = [ dependencies = [
"actix-rt", "actix-rt",
"anyhow", "anyhow",

View file

@ -1,5 +1,6 @@
use crate::{ use crate::{
admin::{AllowedDomains, BlockedDomains, ConnectedActors, Domains}, admin::{AllowedDomains, BlockedDomains, ConnectedActors, Domains},
collector::Snapshot,
config::{AdminUrlKind, Config}, config::{AdminUrlKind, Config},
error::{Error, ErrorKind}, error::{Error, ErrorKind},
}; };
@ -50,6 +51,10 @@ pub(crate) async fn connected(client: &Client, config: &Config) -> Result<Connec
get_results(client, config, AdminUrlKind::Connected).await get_results(client, config, AdminUrlKind::Connected).await
} }
pub(crate) async fn stats(client: &Client, config: &Config) -> Result<Snapshot, Error> {
get_results(client, config, AdminUrlKind::Stats).await
}
async fn get_results<T: DeserializeOwned>( async fn get_results<T: DeserializeOwned>(
client: &Client, client: &Client,
config: &Config, config: &Config,

View file

@ -14,11 +14,14 @@ pub(crate) struct Args {
#[arg(short, long, help = "List allowed and blocked domains")] #[arg(short, long, help = "List allowed and blocked domains")]
list: bool, list: bool,
#[arg(short, long, help = "Get statistics from the server")]
stats: bool,
} }
impl Args { impl Args {
pub(crate) fn any(&self) -> bool { pub(crate) fn any(&self) -> bool {
!self.blocks.is_empty() || !self.allowed.is_empty() || self.list !self.blocks.is_empty() || !self.allowed.is_empty() || self.list || self.stats
} }
pub(crate) fn new() -> Self { pub(crate) fn new() -> Self {
@ -40,4 +43,8 @@ impl Args {
pub(crate) fn list(&self) -> bool { pub(crate) fn list(&self) -> bool {
self.list self.list
} }
pub(crate) fn stats(&self) -> bool {
self.stats
}
} }

View file

@ -29,25 +29,25 @@ struct Inner {
registry: Registry<Key, GenerationalStorage<AtomicStorage>>, registry: Registry<Key, GenerationalStorage<AtomicStorage>>,
} }
#[derive(serde::Serialize)] #[derive(Debug, serde::Deserialize, serde::Serialize)]
struct Counter { struct Counter {
labels: Vec<(String, String)>, labels: Vec<(String, String)>,
value: u64, value: u64,
} }
#[derive(serde::Serialize)] #[derive(Debug, serde::Deserialize, serde::Serialize)]
struct Gauge { struct Gauge {
labels: Vec<(String, String)>, labels: Vec<(String, String)>,
value: f64, value: f64,
} }
#[derive(serde::Serialize)] #[derive(Debug, serde::Deserialize, serde::Serialize)]
struct Histogram { struct Histogram {
labels: Vec<(String, String)>, labels: Vec<(String, String)>,
value: Vec<(f64, Option<f64>)>, value: Vec<(f64, Option<f64>)>,
} }
#[derive(serde::Serialize)] #[derive(Debug, serde::Deserialize, serde::Serialize)]
pub(crate) struct Snapshot { pub(crate) struct Snapshot {
counters: HashMap<String, Vec<Counter>>, counters: HashMap<String, Vec<Counter>>,
gauges: HashMap<String, Vec<Gauge>>, gauges: HashMap<String, Vec<Gauge>>,

View file

@ -77,6 +77,7 @@ pub enum AdminUrlKind {
Allowed, Allowed,
Blocked, Blocked,
Connected, Connected,
Stats,
} }
impl std::fmt::Debug for Config { impl std::fmt::Debug for Config {
@ -338,6 +339,8 @@ impl Config {
.try_resolve(IriRelativeStr::new("api/v1/admin/blocked")?.as_ref())?, .try_resolve(IriRelativeStr::new("api/v1/admin/blocked")?.as_ref())?,
AdminUrlKind::Connected => FixedBaseResolver::new(self.base_uri.as_ref()) AdminUrlKind::Connected => FixedBaseResolver::new(self.base_uri.as_ref())
.try_resolve(IriRelativeStr::new("api/v1/admin/connected")?.as_ref())?, .try_resolve(IriRelativeStr::new("api/v1/admin/connected")?.as_ref())?,
AdminUrlKind::Stats => FixedBaseResolver::new(self.base_uri.as_ref())
.try_resolve(IriRelativeStr::new("api/v1/admin/stats")?.as_ref())?,
}; };
Ok(iri) Ok(iri)

View file

@ -142,6 +142,11 @@ async fn main() -> Result<(), anyhow::Error> {
println!("{report}"); println!("{report}");
} }
if args.stats() {
let stats = admin::client::stats(&client, &config).await?;
println!("{:#?}", stats);
}
return Ok(()); return Ok(());
} }