Add metrics inside fork-choice crate (#6205)

* Add metrics inside fork-choice crate
This commit is contained in:
Lion - dapplion
2024-08-19 09:56:31 +02:00
committed by GitHub
parent 6566705505
commit 9bc5643319
8 changed files with 87 additions and 20 deletions

View File

@@ -12,6 +12,7 @@ state_processing = { workspace = true }
proto_array = { workspace = true }
ethereum_ssz = { workspace = true }
ethereum_ssz_derive = { workspace = true }
lighthouse_metrics = { workspace = true }
slog = { workspace = true }
[dev-dependencies]

View File

@@ -1,3 +1,4 @@
use crate::metrics::{self, scrape_for_metrics};
use crate::{ForkChoiceStore, InvalidationOperation};
use proto_array::{
Block as ProtoBlock, DisallowedReOrgOffsets, ExecutionStatus, ProposerHeadError,
@@ -262,6 +263,11 @@ fn dequeue_attestations(
.unwrap_or(queued_attestations.len()),
);
metrics::inc_counter_by(
&metrics::FORK_CHOICE_DEQUEUED_ATTESTATIONS,
queued_attestations.len() as u64,
);
std::mem::replace(queued_attestations, remaining)
}
@@ -649,6 +655,8 @@ where
payload_verification_status: PayloadVerificationStatus,
spec: &ChainSpec,
) -> Result<(), Error<T::Error>> {
let _timer = metrics::start_timer(&metrics::FORK_CHOICE_ON_BLOCK_TIMES);
// If this block has already been processed we do not need to reprocess it.
// We check this immediately in case re-processing the block mutates some property of the
// global fork choice store, e.g. the justified checkpoints or the proposer boost root.
@@ -1040,6 +1048,8 @@ where
attestation: IndexedAttestationRef<E>,
is_from_block: AttestationFromBlock,
) -> Result<(), Error<T::Error>> {
let _timer = metrics::start_timer(&metrics::FORK_CHOICE_ON_ATTESTATION_TIMES);
self.update_time(system_time_current_slot)?;
// Ignore any attestations to the zero hash.
@@ -1087,6 +1097,8 @@ where
///
/// We assume that the attester slashing provided to this function has already been verified.
pub fn on_attester_slashing(&mut self, slashing: AttesterSlashingRef<'_, E>) {
let _timer = metrics::start_timer(&metrics::FORK_CHOICE_ON_ATTESTER_SLASHING_TIMES);
let attesting_indices_set = |att: IndexedAttestationRef<'_, E>| {
att.attesting_indices_iter()
.copied()
@@ -1502,6 +1514,11 @@ where
queued_attestations: self.queued_attestations().to_vec(),
}
}
/// Update the global metrics `DEFAULT_REGISTRY` with info from the fork choice
pub fn scrape_for_metrics(&self) {
scrape_for_metrics(self);
}
}
/// Helper struct that is used to encode/decode the state of the `ForkChoice` as SSZ bytes.

View File

@@ -1,5 +1,6 @@
mod fork_choice;
mod fork_choice_store;
mod metrics;
pub use crate::fork_choice::{
AttestationFromBlock, Error, ForkChoice, ForkChoiceView, ForkchoiceUpdateParameters,

View File

@@ -0,0 +1,62 @@
pub use lighthouse_metrics::*;
use std::sync::LazyLock;
use types::EthSpec;
use crate::{ForkChoice, ForkChoiceStore};
pub static FORK_CHOICE_QUEUED_ATTESTATIONS: LazyLock<Result<IntGauge>> = LazyLock::new(|| {
try_create_int_gauge(
"fork_choice_queued_attestations",
"Current count of queued attestations",
)
});
pub static FORK_CHOICE_NODES: LazyLock<Result<IntGauge>> = LazyLock::new(|| {
try_create_int_gauge("fork_choice_nodes", "Current count of proto array nodes")
});
pub static FORK_CHOICE_INDICES: LazyLock<Result<IntGauge>> = LazyLock::new(|| {
try_create_int_gauge(
"fork_choice_indices",
"Current count of proto array indices",
)
});
pub static FORK_CHOICE_DEQUEUED_ATTESTATIONS: LazyLock<Result<IntCounter>> = LazyLock::new(|| {
try_create_int_counter(
"fork_choice_dequeued_attestations_total",
"Total count of dequeued attestations",
)
});
pub static FORK_CHOICE_ON_BLOCK_TIMES: LazyLock<Result<Histogram>> = LazyLock::new(|| {
try_create_histogram(
"beacon_fork_choice_process_block_seconds",
"The duration in seconds of on_block runs",
)
});
pub static FORK_CHOICE_ON_ATTESTATION_TIMES: LazyLock<Result<Histogram>> = LazyLock::new(|| {
try_create_histogram(
"beacon_fork_choice_process_attestation_seconds",
"The duration in seconds of on_attestation runs",
)
});
pub static FORK_CHOICE_ON_ATTESTER_SLASHING_TIMES: LazyLock<Result<Histogram>> =
LazyLock::new(|| {
try_create_histogram(
"beacon_fork_choice_on_attester_slashing_seconds",
"The duration in seconds on on_attester_slashing runs",
)
});
/// Update the global metrics `DEFAULT_REGISTRY` with info from the fork choice.
pub fn scrape_for_metrics<T: ForkChoiceStore<E>, E: EthSpec>(fork_choice: &ForkChoice<T, E>) {
set_gauge(
&FORK_CHOICE_QUEUED_ATTESTATIONS,
fork_choice.queued_attestations().len() as i64,
);
set_gauge(
&FORK_CHOICE_NODES,
fork_choice.proto_array().core_proto_array().nodes.len() as i64,
);
set_gauge(
&FORK_CHOICE_INDICES,
fork_choice.proto_array().core_proto_array().indices.len() as i64,
);
}