Skip to content

Commit dc14551

Browse files
committed
feat(metrics): export initial sync height as Prometheus gauge
Add initial_sync_height and initial_sync_progress_pct gauges, updated after every batch so sync progress is visible in Prometheus without waiting for the full update_blocks() call to complete.
1 parent 67db88f commit dc14551

2 files changed

Lines changed: 23 additions & 0 deletions

File tree

src/metrics.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,12 @@ impl Metrics {
4848
g
4949
}
5050

51+
pub fn float_gauge(&self, opts: prometheus::Opts) -> prometheus::Gauge {
52+
let g = prometheus::Gauge::with_opts(opts).unwrap();
53+
self.reg.register(Box::new(g.clone())).unwrap();
54+
g
55+
}
56+
5157
pub fn gauge_vec(&self, opts: prometheus::Opts, labels: &[&str]) -> GaugeVec {
5258
let g = GaugeVec::new(opts, labels).unwrap();
5359
self.reg.register(Box::new(g.clone())).unwrap();

src/new_index/schema.rs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -198,6 +198,8 @@ pub struct Indexer {
198198
iconfig: IndexerConfig,
199199
duration: HistogramVec,
200200
tip_metric: Gauge,
201+
sync_height: Gauge,
202+
sync_progress: prometheus::Gauge,
201203
}
202204

203205
struct IndexerConfig {
@@ -245,6 +247,14 @@ impl Indexer {
245247
&["step"],
246248
),
247249
tip_metric: metrics.gauge(MetricOpts::new("tip_height", "Current chain tip height")),
250+
sync_height: metrics.gauge(MetricOpts::new(
251+
"initial_sync_height",
252+
"Height of the last block batch completed during initial sync",
253+
)),
254+
sync_progress: metrics.float_gauge(MetricOpts::new(
255+
"initial_sync_progress_pct",
256+
"Initial sync progress as a percentage of the best known chain height",
257+
)),
248258
}
249259
}
250260

@@ -394,6 +404,13 @@ impl Indexer {
394404
self.index(&to_index);
395405
}
396406
}
407+
if let Some(last) = blocks.last() {
408+
let h = last.entry.height();
409+
self.sync_height.set(h as i64);
410+
if chain_tip_height > 0 {
411+
self.sync_progress.set(h as f64 / chain_tip_height as f64 * 100.0);
412+
}
413+
}
397414
});
398415

399416
// Compact after all add+index work is done, not between passes.

0 commit comments

Comments
 (0)