Skip to content

Commit

Permalink
Expose epoch schedule metrics in Prometheus
Browse files Browse the repository at this point in the history
With the epoch start slots and the number of slots in the epoch (and the
current slot, which we already had), we can infer/estimate:

 * Epoch progress percentage
 * Slots left until the next epoch
 * Time left until the next epoch (from slot height increase)

These are useful metrics to have about the network.
  • Loading branch information
ruuda authored and enriquefynn committed Aug 23, 2022
1 parent 3b7a516 commit fd2769a
Showing 1 changed file with 42 additions and 0 deletions.
42 changes: 42 additions & 0 deletions prometheus/src/bank_metrics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ use crate::{
utils::{write_metric, Metric, MetricFamily},
};
use std::io;
use solana_sdk::sysvar;
use solana_sdk::sysvar::epoch_schedule::EpochSchedule;

pub fn write_bank_metrics<W: io::Write>(
banks_with_commitments: &BanksWithCommitments,
Expand All @@ -28,6 +30,46 @@ pub fn write_bank_metrics<W: io::Write>(
.for_each_commitment(|bank| Some(Metric::new(bank.clock().epoch))),
},
)?;
write_metric(
out,
&MetricFamily {
name: "solana_block_epoch_start_slot",
help: "The first slot in the current epoch",
type_: "gauge",
metrics: banks_with_commitments
.for_each_commitment(|bank| {
// Note, the bank actually has a field that holds the EpochSchedule,
// but it is not public, so we can't easily access it here. We could
// make it public, but to make our patches less invasive, load the
// epoch schedule from the sysvar instead. It should always exist.
let epoch_schedule: EpochSchedule = bank
.get_account(&sysvar::epoch_schedule::id())?
.deserialize_data().ok()?;
let clock = bank.clock();
Some(Metric::new(epoch_schedule.get_first_slot_in_epoch(clock.epoch)))
}),
},
)?;
write_metric(
out,
&MetricFamily {
name: "solana_block_epoch_slots_total",
help: "The duration of the current epoch, in slots.",
type_: "gauge",
metrics: banks_with_commitments
.for_each_commitment(|bank| {
// Note, the bank actually has a field that holds the EpochSchedule,
// but it is not public, so we can't easily access it here. We could
// make it public, but to make our patches less invasive, load the
// epoch schedule from the sysvar instead. It should always exist.
let epoch_schedule: EpochSchedule = bank
.get_account(&sysvar::epoch_schedule::id())?
.deserialize_data().ok()?;
let clock = bank.clock();
Some(Metric::new(epoch_schedule.get_slots_in_epoch(clock.epoch)))
}),
},
)?;
write_metric(
out,
&MetricFamily {
Expand Down

0 comments on commit fd2769a

Please sign in to comment.