Skip to content

Commit

Permalink
feat: address PR commnets
Browse files Browse the repository at this point in the history
  • Loading branch information
karlem committed Jul 19, 2024
1 parent 7431138 commit 02f68ef
Show file tree
Hide file tree
Showing 11 changed files with 71 additions and 51 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 7 additions & 3 deletions fendermint/app/src/cmd/run.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ use fendermint_crypto::SecretKey;
use fendermint_rocksdb::{blockstore::NamespaceBlockstore, namespaces, RocksDb, RocksDbConfig};
use fendermint_vm_actor_interface::eam::EthAddress;
use fendermint_vm_interpreter::chain::ChainEnv;
use fendermint_vm_interpreter::fvm::observe::register_metrics as register_interpreter_metrics;
use fendermint_vm_interpreter::fvm::upgrades::UpgradeScheduler;
use fendermint_vm_interpreter::{
bytes::{BytesMessageInterpreter, ProposalPrepareMode},
Expand All @@ -27,6 +28,7 @@ use fendermint_vm_topdown::voting::{publish_vote_loop, Error as VoteError, VoteT
use fendermint_vm_topdown::{CachedFinalityProvider, IPCParentFinality, Toggle};
use fvm_shared::address::{current_network, Address, Network};
use ipc_ipld_resolver::{Event as ResolverEvent, VoteRecord};
use ipc_observability::observe::register_metrics as register_default_metrics;
use ipc_provider::config::subnet::{EVMSubnet, SubnetConfig};
use ipc_provider::IpcProvider;
use libp2p::identity::secp256k1;
Expand All @@ -38,6 +40,7 @@ use tracing::info;

use crate::cmd::key::read_secret_key;
use crate::{cmd, options::run::RunArgs, settings::Settings};
use fendermint_app::observe::register_metrics as register_consensus_metrics;

cmd! {
RunArgs(self, settings) {
Expand Down Expand Up @@ -70,10 +73,11 @@ async fn run(settings: Settings) -> anyhow::Result<()> {
let metrics_registry = if settings.metrics.enabled {
let registry = prometheus::Registry::new();

register_default_metrics(&registry).context("failed to register default metrics")?;
register_topdown_metrics(&registry).context("failed to register topdown metrics")?;

fendermint_app::metrics::register_app_metrics(&registry)
.context("failed to register metrics")?;
register_interpreter_metrics(&registry)
.context("failed to register interpreter metrics")?;
register_consensus_metrics(&registry).context("failed to register consensus metrics")?;

Some(registry)
} else {
Expand Down
1 change: 0 additions & 1 deletion fendermint/app/src/metrics/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,4 @@

mod prometheus;

pub use prometheus::app::register_metrics as register_app_metrics;
pub use prometheus::eth::register_metrics as register_eth_metrics;
41 changes: 1 addition & 40 deletions fendermint/app/src/metrics/prometheus.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,42 +2,9 @@
// SPDX-License-Identifier: Apache-2.0, MIT
//! Prometheus metrics
macro_rules! metrics {
($($name:ident : $type:ty = $desc:literal);* $(;)?) => {
$(
paste! {
lazy_static! {
pub static ref $name: $type = $type::new(stringify!([< $name:lower >]), $desc).unwrap();
}
}
)*

pub fn register_metrics(registry: &Registry) -> anyhow::Result<()> {
$(registry.register(Box::new($name.clone()))?;)*
Ok(())
}
};
}

/// Metrics emitted by endermint.
pub mod app {
use lazy_static::lazy_static;
use paste::paste;
use prometheus::{IntCounter, IntGauge, Registry};

metrics! {
BOTTOMUP_CKPT_BLOCK_HEIGHT: IntGauge = "Highest bottom-up checkpoint created";
BOTTOMUP_CKPT_CONFIG_NUM: IntGauge = "Highest configuration number checkpointed";
BOTTOMUP_CKPT_NUM_MSGS: IntCounter = "Number of bottom-up messages observed since start";

// This metrics is available in CometBFT as well, but it's something that should increase even without subnets,
// which can be a useful way to check if metrics work at all.
ABCI_COMMITTED_BLOCK_HEIGHT: IntGauge = "Highest committed block";
}
}

/// Metrics emitted by the Ethereum API facade.
pub mod eth {
// TODO - migrate these metrics to new observability architecture
use fendermint_eth_api::apis::RPC_METHOD_CALL_LATENCY_SECONDS;

pub fn register_metrics(registry: &prometheus::Registry) -> anyhow::Result<()> {
Expand All @@ -48,12 +15,6 @@ pub mod eth {

#[cfg(test)]
mod tests {
#[test]
fn can_register_app_metrics() {
let r = prometheus::Registry::new();
super::app::register_metrics(&r).unwrap();
}

#[test]
fn can_register_eth_metrics() {
let r = prometheus::Registry::new();
Expand Down
5 changes: 4 additions & 1 deletion fendermint/vm/interpreter/src/fvm/checkpoint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,9 @@ use ipc_actors_abis::gateway_getter_facet as getter;
use ipc_api::staking::ConfigurationNumber;
use ipc_observability::{emit, serde::HexEncodableBlockHash};

use super::observe::{CheckpointCreated, CheckpointFinalized, CheckpointSigned};
use super::observe::{
CheckpointCreated, CheckpointFinalized, CheckpointSigned, CheckpointSignedRole,
};
use super::state::ipc::tokens_to_burn;
use super::{
broadcast::Broadcaster,
Expand Down Expand Up @@ -256,6 +258,7 @@ where
.context("failed to broadcast checkpoint signature")?;

emit(CheckpointSigned {
role: CheckpointSignedRole::Own,
height: height.value(),
hash: HexEncodableBlockHash(cp.block_hash.to_vec()),
validator: validator_ctx.public_key,
Expand Down
12 changes: 9 additions & 3 deletions fendermint/vm/interpreter/src/fvm/exec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,14 @@ use fendermint_vm_actor_interface::{chainmetadata, cron, system};
use fvm::executor::ApplyRet;
use fvm_ipld_blockstore::Blockstore;
use fvm_shared::{address::Address, ActorID, MethodNum, BLOCK_GAS_LIMIT};
use ipc_observability::{emit, measure_time};
use ipc_observability::{emit, measure_time, observe::TracingError, Traceable};
use tendermint_rpc::Client;

use crate::ExecInterpreter;

use super::{
checkpoint::{self, PowerUpdates},
observe::{MsgExec, MsgExecPurpose},
observe::{CheckpointFinalized, MsgExec, MsgExecPurpose},
state::FvmExecState,
FvmMessage, FvmMessageInterpreter,
};
Expand Down Expand Up @@ -186,7 +186,13 @@ where
}

async fn end(&self, mut state: Self::State) -> anyhow::Result<(Self::State, Self::EndOutput)> {
checkpoint::emit_trace_if_check_checkpoint_finalized(&self.gateway, &mut state)?;
let _ = checkpoint::emit_trace_if_check_checkpoint_finalized(&self.gateway, &mut state)
.inspect_err(|e| {
emit(TracingError {
affected_event: CheckpointFinalized::name(),
reason: e.to_string(),
});
});

let updates = if let Some((checkpoint, updates)) =
checkpoint::maybe_create_checkpoint(&self.gateway, &mut state)
Expand Down
11 changes: 8 additions & 3 deletions fendermint/vm/interpreter/src/fvm/observe.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,9 +82,6 @@ impl_traceables!(
CheckpointFinalized
);

/// Hex encoded hash.
pub type HashHex<'a> = &'a str;

#[derive(Debug)]
pub struct CheckpointCreated {
pub height: u64,
Expand All @@ -102,8 +99,15 @@ impl Recordable for CheckpointCreated {
}
}

#[derive(Debug)]
pub enum CheckpointSignedRole {
Own,
Peer,
}

#[derive(Debug)]
pub struct CheckpointSigned {
pub role: CheckpointSignedRole,
pub height: u64,
pub hash: HexEncodableBlockHash,
pub validator: PublicKey,
Expand Down Expand Up @@ -181,6 +185,7 @@ mod tests {
let secret_key = SecretKey::random(&mut r);

emit(CheckpointSigned {
role: CheckpointSignedRole::Own,
height: 1,
hash: HexEncodableBlockHash(hash.clone()),
validator: secret_key.public_key(),
Expand Down
1 change: 1 addition & 0 deletions ipc/observability/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,4 @@ tracing = { workspace = true }
tracing-subscriber = { workspace = true }
tracing-appender = { workspace = true }
hex = { workspace = true }
anyhow = { workspace = true }
2 changes: 2 additions & 0 deletions ipc/observability/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ pub mod macros;
pub mod traces;
mod tracing_layers;
pub use lazy_static::lazy_static;
pub mod observe;
pub mod serde;

use std::fmt::Debug;
Expand All @@ -19,6 +20,7 @@ pub trait Recordable {
pub trait Traceable {
fn trace_level(&self) -> TraceLevel;
fn domain(&self) -> &'static str;
fn name() -> &'static str;
}

pub enum TraceLevel {
Expand Down
8 changes: 8 additions & 0 deletions ipc/observability/src/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@ macro_rules! register_metrics {
macro_rules! impl_traceable {
($struct_name:ident<$lifetime:tt>, $trace_level:expr, $domain:expr) => {
impl<$lifetime> Traceable for $struct_name<$lifetime> {
fn name() -> &'static str {
stringify!($struct_name)
}

fn trace_level(&self) -> TraceLevel {
$trace_level
}
Expand All @@ -33,6 +37,10 @@ macro_rules! impl_traceable {
};
($struct_name:ident, $trace_level:expr, $domain:expr) => {
impl Traceable for $struct_name {
fn name() -> &'static str {
stringify!($struct_name)
}

fn trace_level(&self) -> TraceLevel {
$trace_level
}
Expand Down
30 changes: 30 additions & 0 deletions ipc/observability/src/observe.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
// Copyright 2022-2024 Protocol Labs
// SPDX-License-Identifier: Apache-2.0, MIT

use crate::{
impl_traceable, impl_traceables, lazy_static, register_metrics, Recordable, TraceLevel,
Traceable,
};
use anyhow;
use prometheus::{register_int_counter_vec, IntCounterVec, Registry};

register_metrics! {
TRACING_ERRORS: IntCounterVec
= register_int_counter_vec!("tracing_errors", "Number of tracing errors", &["event"]);
}

impl_traceables!(TraceLevel::Error, "TracingError", TracingError<'a>);

#[derive(Debug)]
pub struct TracingError<'a> {
pub affected_event: &'a str,
pub reason: String,
}

impl Recordable for TracingError<'_> {
fn record_metrics(&self) {
TRACING_ERRORS
.with_label_values(&[self.affected_event])
.inc();
}
}

0 comments on commit 02f68ef

Please sign in to comment.