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

1
Cargo.lock generated
View File

@@ -3162,6 +3162,7 @@ dependencies = [
"beacon_chain",
"ethereum_ssz",
"ethereum_ssz_derive",
"lighthouse_metrics",
"proto_array",
"slog",
"state_processing",

View File

@@ -2192,8 +2192,6 @@ impl<T: BeaconChainTypes> BeaconChain<T> {
&self,
verified: &impl VerifiedAttestation<T>,
) -> Result<(), Error> {
let _timer = metrics::start_timer(&metrics::FORK_CHOICE_PROCESS_ATTESTATION_TIMES);
self.canonical_head
.fork_choice_write_lock()
.on_attestation(
@@ -3634,8 +3632,6 @@ impl<T: BeaconChainTypes> BeaconChain<T> {
// Register the new block with the fork choice service.
{
let _fork_choice_block_timer =
metrics::start_timer(&metrics::FORK_CHOICE_PROCESS_BLOCK_TIMES);
let block_delay = self
.slot_clock
.seconds_from_current_slot_start()

View File

@@ -1666,9 +1666,6 @@ impl<T: BeaconChainTypes> ExecutionPendingBlock<T> {
// Register each attestation in the block with fork choice.
for (i, attestation) in block.message().body().attestations().enumerate() {
let _fork_choice_attestation_timer =
metrics::start_timer(&metrics::FORK_CHOICE_PROCESS_ATTESTATION_TIMES);
let indexed_attestation = consensus_context
.get_indexed_attestation(&state, attestation)
.map_err(|e| BlockError::PerBlockProcessingError(e.into_with_index(i)))?;

View File

@@ -569,19 +569,6 @@ pub static FORK_CHOICE_AFTER_FINALIZATION_TIMES: LazyLock<Result<Histogram>> =
exponential_buckets(1e-3, 2.0, 10),
)
});
pub static FORK_CHOICE_PROCESS_BLOCK_TIMES: LazyLock<Result<Histogram>> = LazyLock::new(|| {
try_create_histogram(
"beacon_fork_choice_process_block_seconds",
"Time taken to add a block and all attestations to fork choice",
)
});
pub static FORK_CHOICE_PROCESS_ATTESTATION_TIMES: LazyLock<Result<Histogram>> =
LazyLock::new(|| {
try_create_histogram(
"beacon_fork_choice_process_attestation_seconds",
"Time taken to add an attestation to fork choice",
)
});
pub static FORK_CHOICE_SET_HEAD_LAG_TIMES: LazyLock<Result<Histogram>> = LazyLock::new(|| {
try_create_histogram(
"beacon_fork_choice_set_head_lag_times",
@@ -1955,6 +1942,11 @@ pub fn scrape_for_metrics<T: BeaconChainTypes>(beacon_chain: &BeaconChain<T>) {
.validator_monitor
.read()
.scrape_metrics(&beacon_chain.slot_clock, &beacon_chain.spec);
beacon_chain
.canonical_head
.fork_choice_read_lock()
.scrape_for_metrics();
}
/// Scrape the given `state` assuming it's the head state, updating the `DEFAULT_REGISTRY`.

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,
);
}