mirror of
https://git.ghostchain.io/proxmio/ghost-node.git
synced 2025-12-27 03:09:56 +00:00
rustfmt ghost metrics and fix typos
Signed-off-by: Uncle Stretch <uncle.stretch@ghostchain.io>
This commit is contained in:
@@ -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<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)
|
||||
where
|
||||
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)
|
||||
where
|
||||
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())
|
||||
.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();
|
||||
|
||||
Reference in New Issue
Block a user