Skip to content

Log Count metrics #297

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 5 commits into
base: unstable
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions Cargo.lock

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

1 change: 1 addition & 0 deletions anchor/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ futures = { workspace = true }
keygen = { workspace = true }
keysplit = { workspace = true }
logging = { workspace = true }
metrics = { workspace = true }
serde = { workspace = true }
strum = { workspace = true }
task_executor = { workspace = true }
Expand Down
2 changes: 2 additions & 0 deletions anchor/logging/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,12 @@ chrono = { version = "0.4", default-features = false, features = [
] }
clap = { workspace = true }
logroller = "0.1.8"
metrics = { workspace = true }
serde = { workspace = true }
strum = { workspace = true }
tracing = { workspace = true }
tracing-appender = "0.2"
tracing-core = "0.1"
tracing-log = "0.2.0"
tracing-subscriber = { workspace = true }
workspace_members = { workspace = true }
93 changes: 93 additions & 0 deletions anchor/logging/src/count_layer.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
use std::sync::LazyLock;

use tracing_log::NormalizeEvent;

// Global metrics counters
pub static INFOS_TOTAL: LazyLock<metrics::Result<metrics::IntCounter>> = LazyLock::new(|| {
metrics::try_create_int_counter(
"info_total",
"Total count of info logs across all dependencies",
)
});

pub static WARNS_TOTAL: LazyLock<metrics::Result<metrics::IntCounter>> = LazyLock::new(|| {
metrics::try_create_int_counter(
"warn_total",
"Total count of warn logs across all dependencies",
)
});

pub static ERRORS_TOTAL: LazyLock<metrics::Result<metrics::IntCounter>> = LazyLock::new(|| {
metrics::try_create_int_counter(
"error_total",
"Total count of error logs across all dependencies",
)
});

// Dependency-specific metrics
pub static DEP_INFOS_TOTAL: LazyLock<metrics::Result<metrics::IntCounterVec>> =
LazyLock::new(|| {
metrics::try_create_int_counter_vec(
"dep_info_total",
"Count of infos logged per enabled dependency",
&["target"],
)
});

pub static DEP_WARNS_TOTAL: LazyLock<metrics::Result<metrics::IntCounterVec>> =
LazyLock::new(|| {
metrics::try_create_int_counter_vec(
"dep_warn_total",
"Count of warns logged per enabled dependency",
&["target"],
)
});

pub static DEP_ERRORS_TOTAL: LazyLock<metrics::Result<metrics::IntCounterVec>> =
LazyLock::new(|| {
metrics::try_create_int_counter_vec(
"dep_error_total",
"Count of errors logged per enabled dependency",
&["target"],
)
});

// Count layer implementation
pub struct CountLayer;
impl<S: tracing_core::Subscriber> tracing_subscriber::layer::Layer<S> for CountLayer {
fn on_event(
&self,
event: &tracing_core::Event<'_>,
_ctx: tracing_subscriber::layer::Context<'_, S>,
) {
// get the event's normalized metadata
let normalized_meta = event.normalized_metadata();
let meta = normalized_meta.as_ref().unwrap_or_else(|| event.metadata());
if !meta.is_event() {
// ignore tracing span events
return;
}

// First, increment global counters regardless of target
match *meta.level() {
tracing_core::Level::INFO => metrics::inc_counter(&INFOS_TOTAL),
tracing_core::Level::WARN => metrics::inc_counter(&WARNS_TOTAL),
tracing_core::Level::ERROR => metrics::inc_counter(&ERRORS_TOTAL),
_ => {}
}

// Then handle dependency-specific counters
let full_target = meta.module_path().unwrap_or_else(|| meta.target());
let target = full_target
.split_once("::")
.map(|(name, _rest)| name)
.unwrap_or(full_target);
let target = &[target];
match *meta.level() {
tracing_core::Level::INFO => metrics::inc_counter_vec(&DEP_INFOS_TOTAL, target),
tracing_core::Level::WARN => metrics::inc_counter_vec(&DEP_WARNS_TOTAL, target),
tracing_core::Level::ERROR => metrics::inc_counter_vec(&DEP_ERRORS_TOTAL, target),
_ => {}
}
}
}
2 changes: 2 additions & 0 deletions anchor/logging/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
mod count_layer;
mod logging;
mod tracing_libp2p_discv5_layer;
pub mod utils;
pub use count_layer::CountLayer;
pub use logging::*;
6 changes: 5 additions & 1 deletion anchor/message_validator/src/consensus_message.rs
Original file line number Diff line number Diff line change
Expand Up @@ -927,7 +927,11 @@ mod tests {
signed_ssv_message: &signed_msg,
committee_info: &committee_info,
role: Role::Proposer, // Proposer role has TTL = 1 + LATE_SLOT_ALLOWANCE + LATE_MESSAGE_MARGIN. To be late for slot 1, we need to add more 2 seconds (2 * slot duration).
received_at: now.checked_add(Duration::from_secs(1 + LATE_SLOT_ALLOWANCE + 2)).unwrap().checked_add(LATE_MESSAGE_MARGIN).unwrap(),
received_at: now
.checked_add(Duration::from_secs(1 + LATE_SLOT_ALLOWANCE + 2))
.unwrap()
.checked_add(LATE_MESSAGE_MARGIN)
.unwrap(),
operators_pk: &[],
slots_per_epoch: 32,
epochs_per_sync_committee_period: 256,
Expand Down
5 changes: 4 additions & 1 deletion anchor/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ use keygen::Keygen;
use keysplit::Keysplit;
use logging::{
create_libp2p_discv5_tracing_layer, init_file_logging, utils::build_workspace_filter,
Libp2pDiscv5TracingLayer, LoggerConfig, LoggingLayer,
CountLayer, Libp2pDiscv5TracingLayer, LoggerConfig, LoggingLayer,
};
use task_executor::ShutdownReason;
use tracing::Level;
Expand Down Expand Up @@ -244,6 +244,9 @@ fn enable_logging(anchor_config: &Node) -> (Option<WorkerGuard>, Option<Libp2pDi
);
}

// Add the CountLayer
logging_layers.push(CountLayer.boxed());

let logging_result = tracing_subscriber::registry()
.with(logging_layers)
.try_init();
Expand Down