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