From cf72a6cdcc8577e0a0c0be3a5f8d183265209615 Mon Sep 17 00:00:00 2001 From: Paul Hauner Date: Fri, 17 Jan 2020 15:25:07 +1100 Subject: [PATCH] Add metrics for log levels --- Cargo.lock | 2 ++ eth2/utils/logging/Cargo.toml | 2 ++ eth2/utils/logging/src/lib.rs | 29 +++++++++++++++++++++++++++-- 3 files changed, 31 insertions(+), 2 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index db12b6e7c2..4106c27b7c 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2455,6 +2455,8 @@ dependencies = [ name = "logging" version = "0.1.0" dependencies = [ + "lazy_static 1.4.0 (registry+https://github.com/rust-lang/crates.io-index)", + "lighthouse_metrics 0.1.0", "slog 2.5.2 (registry+https://github.com/rust-lang/crates.io-index)", "slog-term 2.4.2 (registry+https://github.com/rust-lang/crates.io-index)", ] diff --git a/eth2/utils/logging/Cargo.toml b/eth2/utils/logging/Cargo.toml index 9d9405429e..0fe0566631 100644 --- a/eth2/utils/logging/Cargo.toml +++ b/eth2/utils/logging/Cargo.toml @@ -7,3 +7,5 @@ edition = "2018" [dependencies] slog = "2.5.2" slog-term = "2.4.2" +lighthouse_metrics = { path = "../lighthouse_metrics" } +lazy_static = "1.4.0" diff --git a/eth2/utils/logging/src/lib.rs b/eth2/utils/logging/src/lib.rs index cbd256f426..5a2a0757f2 100644 --- a/eth2/utils/logging/src/lib.rs +++ b/eth2/utils/logging/src/lib.rs @@ -1,7 +1,24 @@ +#[macro_use] +extern crate lazy_static; + +use lighthouse_metrics::{ + inc_counter, try_create_int_counter, IntCounter, Result as MetricsResult, +}; use std::io::{Result, Write}; pub const MAX_MESSAGE_WIDTH: usize = 40; +lazy_static! { + pub static ref INFOS_TOTAL: MetricsResult = + try_create_int_counter("info_total", "Count of infos logged"); + pub static ref WARNS_TOTAL: MetricsResult = + try_create_int_counter("warn_total", "Count of warns logged"); + pub static ref ERRORS_TOTAL: MetricsResult = + try_create_int_counter("error_total", "Count of errors logged"); + pub static ref CRITS_TOTAL: MetricsResult = + try_create_int_counter("crit_total", "Count of crits logged"); +} + pub struct AlignedTermDecorator { wrapped: slog_term::TermDecorator, message_width: usize, @@ -19,14 +36,22 @@ impl AlignedTermDecorator { impl slog_term::Decorator for AlignedTermDecorator { fn with_record( &self, - _record: &slog::Record, + record: &slog::Record, _logger_values: &slog::OwnedKVList, f: F, ) -> Result<()> where F: FnOnce(&mut dyn slog_term::RecordDecorator) -> std::io::Result<()>, { - self.wrapped.with_record(_record, _logger_values, |deco| { + match record.level() { + slog::Level::Info => inc_counter(&INFOS_TOTAL), + slog::Level::Warning => inc_counter(&WARNS_TOTAL), + slog::Level::Error => inc_counter(&ERRORS_TOTAL), + slog::Level::Critical => inc_counter(&CRITS_TOTAL), + _ => (), + } + + self.wrapped.with_record(record, _logger_values, |deco| { f(&mut AlignedRecordDecorator::new(deco, self.message_width)) }) }