Skip to content

Commit bf723c1

Browse files
authored
Log Count metrics (#297)
1 parent 9115b94 commit bf723c1

File tree

7 files changed

+110
-2
lines changed

7 files changed

+110
-2
lines changed

Cargo.lock

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

anchor/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ futures = { workspace = true }
1515
keygen = { workspace = true }
1616
keysplit = { workspace = true }
1717
logging = { workspace = true }
18+
metrics = { workspace = true }
1819
serde = { workspace = true }
1920
strum = { workspace = true }
2021
task_executor = { workspace = true }

anchor/logging/Cargo.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,12 @@ chrono = { version = "0.4", default-features = false, features = [
1111
] }
1212
clap = { workspace = true }
1313
logroller = "0.1.8"
14+
metrics = { workspace = true }
1415
serde = { workspace = true }
1516
strum = { workspace = true }
1617
tracing = { workspace = true }
1718
tracing-appender = "0.2"
1819
tracing-core = "0.1"
20+
tracing-log = "0.2.0"
1921
tracing-subscriber = { workspace = true }
2022
workspace_members = { workspace = true }

anchor/logging/src/count_layer.rs

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
use std::sync::LazyLock;
2+
3+
use tracing_log::NormalizeEvent;
4+
5+
// Global metrics counters
6+
pub static INFOS_TOTAL: LazyLock<metrics::Result<metrics::IntCounter>> = LazyLock::new(|| {
7+
metrics::try_create_int_counter(
8+
"info_total",
9+
"Total count of info logs across all dependencies",
10+
)
11+
});
12+
13+
pub static WARNS_TOTAL: LazyLock<metrics::Result<metrics::IntCounter>> = LazyLock::new(|| {
14+
metrics::try_create_int_counter(
15+
"warn_total",
16+
"Total count of warn logs across all dependencies",
17+
)
18+
});
19+
20+
pub static ERRORS_TOTAL: LazyLock<metrics::Result<metrics::IntCounter>> = LazyLock::new(|| {
21+
metrics::try_create_int_counter(
22+
"error_total",
23+
"Total count of error logs across all dependencies",
24+
)
25+
});
26+
27+
// Dependency-specific metrics
28+
pub static DEP_INFOS_TOTAL: LazyLock<metrics::Result<metrics::IntCounterVec>> =
29+
LazyLock::new(|| {
30+
metrics::try_create_int_counter_vec(
31+
"dep_info_total",
32+
"Count of infos logged per enabled dependency",
33+
&["target"],
34+
)
35+
});
36+
37+
pub static DEP_WARNS_TOTAL: LazyLock<metrics::Result<metrics::IntCounterVec>> =
38+
LazyLock::new(|| {
39+
metrics::try_create_int_counter_vec(
40+
"dep_warn_total",
41+
"Count of warns logged per enabled dependency",
42+
&["target"],
43+
)
44+
});
45+
46+
pub static DEP_ERRORS_TOTAL: LazyLock<metrics::Result<metrics::IntCounterVec>> =
47+
LazyLock::new(|| {
48+
metrics::try_create_int_counter_vec(
49+
"dep_error_total",
50+
"Count of errors logged per enabled dependency",
51+
&["target"],
52+
)
53+
});
54+
55+
// Count layer implementation
56+
pub struct CountLayer;
57+
impl<S: tracing_core::Subscriber> tracing_subscriber::layer::Layer<S> for CountLayer {
58+
fn on_event(
59+
&self,
60+
event: &tracing_core::Event<'_>,
61+
_ctx: tracing_subscriber::layer::Context<'_, S>,
62+
) {
63+
// get the event's normalized metadata
64+
let normalized_meta = event.normalized_metadata();
65+
let meta = normalized_meta.as_ref().unwrap_or_else(|| event.metadata());
66+
if !meta.is_event() {
67+
// ignore tracing span events
68+
return;
69+
}
70+
71+
// First, increment global counters regardless of target
72+
match *meta.level() {
73+
tracing_core::Level::INFO => metrics::inc_counter(&INFOS_TOTAL),
74+
tracing_core::Level::WARN => metrics::inc_counter(&WARNS_TOTAL),
75+
tracing_core::Level::ERROR => metrics::inc_counter(&ERRORS_TOTAL),
76+
_ => {}
77+
}
78+
79+
// Then handle dependency-specific counters
80+
let full_target = meta.module_path().unwrap_or_else(|| meta.target());
81+
let target = full_target
82+
.split_once("::")
83+
.map(|(name, _rest)| name)
84+
.unwrap_or(full_target);
85+
let target = &[target];
86+
match *meta.level() {
87+
tracing_core::Level::INFO => metrics::inc_counter_vec(&DEP_INFOS_TOTAL, target),
88+
tracing_core::Level::WARN => metrics::inc_counter_vec(&DEP_WARNS_TOTAL, target),
89+
tracing_core::Level::ERROR => metrics::inc_counter_vec(&DEP_ERRORS_TOTAL, target),
90+
_ => {}
91+
}
92+
}
93+
}

anchor/logging/src/lib.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1+
mod count_layer;
12
mod logging;
23
mod tracing_libp2p_discv5_layer;
34
pub mod utils;
5+
pub use count_layer::CountLayer;
46
pub use logging::*;

anchor/message_validator/src/consensus_message.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -927,7 +927,11 @@ mod tests {
927927
signed_ssv_message: &signed_msg,
928928
committee_info: &committee_info,
929929
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).
930-
received_at: now.checked_add(Duration::from_secs(1 + LATE_SLOT_ALLOWANCE + 2)).unwrap().checked_add(LATE_MESSAGE_MARGIN).unwrap(),
930+
received_at: now
931+
.checked_add(Duration::from_secs(1 + LATE_SLOT_ALLOWANCE + 2))
932+
.unwrap()
933+
.checked_add(LATE_MESSAGE_MARGIN)
934+
.unwrap(),
931935
operators_pk: &[],
932936
slots_per_epoch: 32,
933937
epochs_per_sync_committee_period: 256,

anchor/src/main.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ use keygen::Keygen;
1313
use keysplit::Keysplit;
1414
use logging::{
1515
create_libp2p_discv5_tracing_layer, init_file_logging, utils::build_workspace_filter,
16-
Libp2pDiscv5TracingLayer, LoggerConfig, LoggingLayer,
16+
CountLayer, Libp2pDiscv5TracingLayer, LoggerConfig, LoggingLayer,
1717
};
1818
use task_executor::ShutdownReason;
1919
use tracing::Level;
@@ -244,6 +244,9 @@ fn enable_logging(anchor_config: &Node) -> (Option<WorkerGuard>, Option<Libp2pDi
244244
);
245245
}
246246

247+
// Add the CountLayer
248+
logging_layers.push(CountLayer.boxed());
249+
247250
let logging_result = tracing_subscriber::registry()
248251
.with(logging_layers)
249252
.try_init();

0 commit comments

Comments
 (0)