diff --git a/Cargo.toml b/Cargo.toml index 83f469c..d44b75d 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -17,7 +17,7 @@ homepage.workspace = true [workspace.package] license = "GPL-3.0-only" authors = ["571nky", "57r37ch", "f4750"] -version = "0.7.208" +version = "0.7.209" edition = "2021" homepage = "https://ghostchain.io" repository = "https://git.ghostchain.io/ghostchain/ghost-node" diff --git a/metrics/src/lib.rs b/metrics/src/lib.rs index dd6219c..118ec3c 100644 --- a/metrics/src/lib.rs +++ b/metrics/src/lib.rs @@ -14,7 +14,7 @@ pub fn logger_hook() -> impl FnOnce(&mut sc_cli::LoggerBuilder, &sc_service::Con |_logger_builder, _config| {} } -/// This module exports Prometheus types an:d defines +/// This module exports Prometheus types an:d defines /// the [`Metrics`](metrics::Metrics) trait. pub mod metrics { pub use prometheus_endpoint as prometheus; diff --git a/metrics/src/metronome.rs b/metrics/src/metronome.rs index dbe5e17..19751f1 100644 --- a/metrics/src/metronome.rs +++ b/metrics/src/metronome.rs @@ -22,7 +22,11 @@ impl Metronome { /// Create a new metronome source with a defined cycle duration. pub fn new(cycle: Duration) -> Self { let period = cycle.into(); - Self { period, delay: Delay::new(period), state: MetronomeState::Snooze } + Self { + period, + delay: Delay::new(period), + state: MetronomeState::Snooze, + } } } @@ -36,14 +40,14 @@ impl futures::Stream for Metronome { let val = self.period; self.delay.reset(val); self.state = MetronomeState::Snooze; - }, + } MetronomeState::Snooze => { if !Pin::new(&mut self.delay).poll(cx).is_ready() { - break + break; } self.state = MetronomeState::SetAlarm; - return Poll::Ready(Some(())) - }, + return Poll::Ready(Some(())); + } } } Poll::Pending diff --git a/metrics/src/runtime/mod.rs b/metrics/src/runtime/mod.rs index 0a7ac19..ecf4942 100644 --- a/metrics/src/runtime/mod.rs +++ b/metrics/src/runtime/mod.rs @@ -2,19 +2,16 @@ use codec::Decode; use core_primitives::{ - metrics_definitions::{ - CounterDefinition, CounterVecDefinition, HistogramDefinition, - }, + metrics_definitions::{CounterDefinition, CounterVecDefinition, HistogramDefinition}, RuntimeMetricLabelValues, RuntimeMetricOp, RuntimeMetricUpdate, }; use prometheus_endpoint::{ - register, Counter, CounterVec, Histogram, HistogramOpts, Opts, Registry, - PrometheusError, U64, + register, Counter, CounterVec, Histogram, HistogramOpts, Opts, PrometheusError, Registry, U64, }; use std::{ collections::hash_map::HashMap, sync::{Arc, Mutex, MutexGuard}, -} +}; #[derive(Clone, Debug)] pub struct Metrics { @@ -32,13 +29,15 @@ impl RuntimeMetricsProvider { pub fn register_countervec(&self, countervec: CounterVecDefinition) { self.with_counter_vecs_lock_held(|mut hashmap| { - hashmap.entry(countervec.name.to_owned()).or_insert(register( - CounterVec::new( - Opts::new(countervec.name, countervec.description), - countervec.labels, - )?, - &self.0, - )?); + hashmap + .entry(countervec.name.to_owned()) + .or_insert(register( + CounterVec::new( + Opts::new(countervec.name, countervec.description), + countervec.labels, + )?, + &self.0, + )?); Ok(()) }) } @@ -46,10 +45,7 @@ impl RuntimeMetricsProvider { pub fn register_counter(&self, counter: CounterDefinition) { self.with_counters_lock_held(|mut hashmap| { hashmap.entry(counter.name.to_owned()).or_insert(register( - Counter::new( - counter.name, - counter.description, - )?, + Counter::new(counter.name, counter.description)?, &self.0, )?); Ok(()) @@ -60,8 +56,7 @@ impl RuntimeMetricsProvider { self.with_histograms_lock_held(|mut hashmap| { hashmap.entry(hist.name.to_owned()).or_insert(register( Histogram::with_opts( - HistogramOpts::new(hist.name, hist.description) - .buckets(hist.buckets.to_vec()), + HistogramOpts::new(hist.name, hist.description).buckets(hist.buckets.to_vec()), )?, &self.0, )?); @@ -69,23 +64,21 @@ impl RuntimeMetricsProvider { }) } - pub fn inc_counter_vec_by( - &self, - name: &str, - value: u64, - labels: &RuntimeMetricsProvider, - ) { + pub fn inc_counter_vec_by(&self, name: &str, value: u64, labels: &RuntimeMetricsProvider) { self.with_counter_vecs_lock_held(|mut hashmap| { hashmap.entry(name.to_owned()).and_modify(|counter_vec| { - counter_vec.with_label_values(&labels.as_str_vec()).inc_by(value) + counter_vec + .with_label_values(&labels.as_str_vec()) + .inc_by(value) }); Ok(()) }) } - + pub fn inc_counter_by(&self, name: &str, value: u64) { self.with_counters_lock_held(|mut hashmap| { - hashmap.entry(name.to_owned()) + hashmap + .entry(name.to_owned()) .and_modify(|counter_vec| counter_vec.inc_by(value)); Ok(()) }) @@ -93,8 +86,9 @@ impl RuntimeMetricsProvider { pub fn observe_histogram(&self, name: &str, value: u128) { self.with_histograms_lock_held(|mut hashmap| { - hashmap.entry(name.to_owned()) - and_modify(|histogram| histogram.observe(value as f64 / 1_000_000_000.0)); + hashmap + .entry(name.to_owned()) + .and_modify(|histogram| histogram.observe(value as f64 / 1_000_000_000.0)); Ok(()) }) } @@ -103,21 +97,36 @@ impl RuntimeMetricsProvider { where F: FnOnce(MutexGuard<'_, HashMap>>) -> Result<(), PrometheusError>, { - let _ = self.1.counters.lock().map(do_something).or_else(|error| Err(error)); + let _ = self + .1 + .counters + .lock() + .map(do_something) + .or_else(|error| Err(error)); } fn with_counter_vecs_lock_held(&self, do_something: F) where F: FnOnce(MutexGuard<'_, HashMap>>) -> Result<(), PrometheusError>, { - let _ = self.1.counter_vecs.lock().map(do_something).or_else(|error| Err(error)); + let _ = self + .1 + .counter_vecs + .lock() + .map(do_something) + .or_else(|error| Err(error)); } fn with_histograms_lock_held(&self, do_something: F) where F: FnOnce(MutexGuard<'_, HashMap>) -> Result<(), PrometheusError>, { - let _ = self.1.histograms.lock().map(do_something).or_else(|error| Err(error)); + let _ = self + .1 + .histograms + .lock() + .map(do_something) + .or_else(|error| Err(error)); } } @@ -131,7 +140,7 @@ impl sc_tracing::TraceHandler for RuntimeMetricsProvider { .unwrap_or(&String::default()) .ne("metrics") { - return + return; } if let Some(update_op_bs58) = event.values.string_values.get("params") { @@ -141,7 +150,7 @@ impl sc_tracing::TraceHandler for RuntimeMetricsProvider { .as_slice(), ) { Ok(update_op) => self.parse_metric_update(update_op), - Err(_) => {}, + Err(_) => {} } } } @@ -150,12 +159,15 @@ impl sc_tracing::TraceHandler for RuntimeMetricsProvider { impl RuntimeMetricsProvider { fn parse_metric_update(&self, update: RuntimeMetricUpdate) { match update.op { - RuntimeMetricOp::IncrementCounterVec(value, ref labels) => - self.inc_counter_vec_by(update.metric_name(), value, labels), - RuntimeMetricOp::IncrementCounter(value) => - self.inc_counter_by(update.metric_name(), value), - RuntimeMetricOp::ObserveHistogram(value) => - self.observe_histogram(update.metric_name(), value), + RuntimeMetricOp::IncrementCounterVec(value, ref labels) => { + self.inc_counter_vec_by(update.metric_name(), value, labels) + } + RuntimeMetricOp::IncrementCounter(value) => { + self.inc_counter_by(update.metric_name(), value) + } + RuntimeMetricOp::ObserveHistogram(value) => { + self.observe_histogram(update.metric_name(), value) + } } } @@ -178,7 +190,7 @@ impl RuntimeMetricsProvider { pub fn logger_hook() -> impl FnOnce(&mut sc_cli::LoggerBuilder, &sc_service::Configuration) -> () { |logger_builder, config| { if config.prometheus_registry().is_none() { - return + return; } let registry = config.prometheus_registry().cloned().unwrap(); diff --git a/metrics/src/tests.rs b/metrics/src/tests.rs index 8c6e78e..db17d84 100644 --- a/metrics/src/tests.rs +++ b/metrics/src/tests.rs @@ -1,5 +1,5 @@ -use hyper::{Client, Uri}; use ghost_test_service::{node_config, run_validator_node, test_prometheus_config}; +use hyper::{Client, Uri}; use keyring::AccountKeyring::*; use std::collections::HashMap; @@ -7,8 +7,13 @@ const DEFAULT_PROMETHEUS_PORT: u16 = 9616; #[tokio::test(flavor = "multi_thread")] async fn runtime_can_publish_metrics() { - let mut alice_config = - node_config(|| {}, tokio::runtime::Handle::current(), Alice, Vec::new(), true); + let mut alice_config = node_config( + || {}, + tokio::runtime::Handle::current(), + Alice, + Vec::new(), + true, + ); // Enable prometheus metrics for Alice. alice_config.prometheus_config = Some(test_prometheus_config(DEFAULT_PROMETHEUS_PORT)); @@ -26,8 +31,13 @@ async fn runtime_can_publish_metrics() { // Start validator Alice let alice = run_validator_node(alice_config, None); - let bob_config = - node_config(|| {}, tokio::runtime::Handle::current(), Bob, vec![alice.addr.clone()], true); + let bob_config = node_config( + || {}, + tokio::runtime::Handle::current(), + Bob, + vec![alice.addr.clone()], + true, + ); // Start validator Bob let _bob = run_validator_node(bob_config, None); @@ -48,19 +58,25 @@ async fn scrape_prometheus_metrics(metrics_uri: &str) -> HashMap { .expect("GET request failed"); let body = String::from_utf8( - hyper::body::to_bytes(res).await.expect("can't get body as bytes").to_vec(), - ).expect("body is not an UTF8 string"); + hyper::body::to_bytes(res) + .await + .expect("can't get body as bytes") + .to_vec(), + ) + .expect("body is not an UTF8 string"); let lines: Vec<_> = body.lines().map(|s| Ok(s.to_owned())).collect(); prometheus_parse::Scrape::parse(lines.into_iter()) .expect("Scraper failed to parse Prometheus metrics") .samples .into_iter() - .filter_map(|prometheus_parse::Sample { metric, value, .. }| match value { - prometheus_parse::Value::Counter(value) => Some((metric, value as u64)), - prometheus_parse::Value::Gauge(value) => Some((metric, value as u64)), - prometheus_parse::Value::Untyped(value) => Some((metric, value as u64)), - _ => None, - }) + .filter_map( + |prometheus_parse::Sample { metric, value, .. }| match value { + prometheus_parse::Value::Counter(value) => Some((metric, value as u64)), + prometheus_parse::Value::Gauge(value) => Some((metric, value as u64)), + prometheus_parse::Value::Untyped(value) => Some((metric, value as u64)), + _ => None, + }, + ) .collect() }