mirror of
https://github.com/romanz/electrs.git
synced 2025-02-23 06:45:26 +01:00
Add gauge metric to export latest block height
This commit is contained in:
parent
94de4345b4
commit
a10c3244da
2 changed files with 36 additions and 3 deletions
|
@ -6,7 +6,7 @@ use crate::{
|
|||
chain::Chain,
|
||||
daemon::Daemon,
|
||||
db::{DBStore, Row, WriteBatch},
|
||||
metrics::{Histogram, Metrics},
|
||||
metrics::{Gauge, Histogram, Metrics},
|
||||
signals::ExitFlag,
|
||||
types::{HeaderRow, ScriptHash, ScriptHashRow, SpendingPrefixRow, TxidRow},
|
||||
};
|
||||
|
@ -15,6 +15,7 @@ use crate::{
|
|||
struct Stats {
|
||||
update_duration: Histogram,
|
||||
update_size: Histogram,
|
||||
height: Gauge,
|
||||
}
|
||||
|
||||
impl Stats {
|
||||
|
@ -30,6 +31,7 @@ impl Stats {
|
|||
"Index update size (in bytes)",
|
||||
"step",
|
||||
),
|
||||
height: metrics.gauge("index_height", "Latest indexed block height"),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -187,6 +189,7 @@ impl Index {
|
|||
self.observe_duration("block", || {
|
||||
index_single_block(block, height).extend(&mut batch)
|
||||
});
|
||||
self.stats.height.set(height);
|
||||
})?;
|
||||
let heights: Vec<_> = heights.collect();
|
||||
assert!(
|
||||
|
|
|
@ -52,6 +52,25 @@ mod metrics_impl {
|
|||
.expect("failed to register Histogram");
|
||||
Histogram { hist }
|
||||
}
|
||||
|
||||
pub fn gauge(&self, name: &str, desc: &str) -> Gauge {
|
||||
let gauge = prometheus::Gauge::new(name, desc).unwrap();
|
||||
self.reg
|
||||
.register(Box::new(gauge.clone()))
|
||||
.expect("failed to register Gauge");
|
||||
Gauge { gauge }
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone)]
|
||||
pub struct Gauge {
|
||||
gauge: prometheus::Gauge,
|
||||
}
|
||||
|
||||
impl Gauge {
|
||||
pub fn set(&self, value: usize) {
|
||||
self.gauge.set(value as f64)
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone)]
|
||||
|
@ -76,7 +95,7 @@ mod metrics_impl {
|
|||
}
|
||||
|
||||
#[cfg(feature = "metrics")]
|
||||
pub use metrics_impl::{Histogram, Metrics};
|
||||
pub use metrics_impl::{Gauge, Histogram, Metrics};
|
||||
|
||||
#[cfg(not(feature = "metrics"))]
|
||||
mod metrics_fake {
|
||||
|
@ -95,6 +114,17 @@ mod metrics_fake {
|
|||
pub fn histogram_vec(&self, _name: &str, _desc: &str, _label: &str) -> Histogram {
|
||||
Histogram {}
|
||||
}
|
||||
|
||||
pub fn gauge(&self, _name: &str, _desc: &str) -> Gauge {
|
||||
Gauge {}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone)]
|
||||
pub struct Gauge {}
|
||||
|
||||
impl Gauge {
|
||||
pub fn set(&self, _value: usize) {}
|
||||
}
|
||||
|
||||
#[derive(Clone)]
|
||||
|
@ -113,4 +143,4 @@ mod metrics_fake {
|
|||
}
|
||||
|
||||
#[cfg(not(feature = "metrics"))]
|
||||
pub use metrics_fake::{Histogram, Metrics};
|
||||
pub use metrics_fake::{Gauge, Histogram, Metrics};
|
||||
|
|
Loading…
Add table
Reference in a new issue