Skip to content

Tracing cleanup #7168

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

Merged
merged 11 commits into from
Apr 1, 2025
Merged
Show file tree
Hide file tree
Changes from 6 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
30 changes: 27 additions & 3 deletions Cargo.lock

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

4 changes: 4 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ members = [
"common/eth2_wallet_manager",
"common/filesystem",
"common/health_metrics",
"common/lighthouse_macros",
"common/lighthouse_version",
"common/lockfile",
"common/logging",
Expand Down Expand Up @@ -120,6 +121,7 @@ bincode = "1"
bitvec = "1"
byteorder = "1"
bytes = "1"
cargo_metadata = "0.19"
clap = { version = "4.5.4", features = ["derive", "cargo", "wrap_help"] }
# Turn off c-kzg's default features which include `blst/portable`. We can turn on blst's portable
# feature ourselves when desired.
Expand Down Expand Up @@ -159,6 +161,7 @@ mockito = "1.5.0"
num_cpus = "1"
parking_lot = "0.12"
paste = "1"
proc-macro2 = "1"
prometheus = { version = "0.13", default-features = false }
quickcheck = "1"
quickcheck_macros = "1"
Expand Down Expand Up @@ -246,6 +249,7 @@ kzg = { path = "crypto/kzg" }
metrics = { path = "common/metrics" }
lighthouse_network = { path = "beacon_node/lighthouse_network" }
lighthouse_version = { path = "common/lighthouse_version" }
lighthouse_macros = { path = "common/lighthouse_macros" }
lockfile = { path = "common/lockfile" }
logging = { path = "common/logging" }
lru_cache = { path = "common/lru_cache" }
Expand Down
12 changes: 12 additions & 0 deletions common/lighthouse_macros/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
[package]
name = "lighthouse_macros"
version = "0.1.0"
edition = { workspace = true }

[lib]
proc-macro = true

[dependencies]
cargo_metadata = { workspace = true }
proc-macro2 = { workspace = true }
quote = { workspace = true }
39 changes: 39 additions & 0 deletions common/lighthouse_macros/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
use cargo_metadata::MetadataCommand;
use proc_macro::TokenStream;
use quote::quote;
use std::error::Error;

fn get_workspace_crates() -> Result<Vec<String>, Box<dyn Error>> {
let metadata = MetadataCommand::new().no_deps().exec()?;

Ok(metadata
.workspace_members
.iter()
.filter_map(|member_id| {
metadata
.packages
.iter()
.find(|package| &package.id == member_id)
.map(|package| package.name.clone())
})
.collect())
}

#[proc_macro]
pub fn workspace_crates(_input: TokenStream) -> TokenStream {
match get_workspace_crates() {
Ok(crate_names) => {
let crate_strs = crate_names.iter().map(|s| s.as_str());
quote! {
Ok(&[#(#crate_strs),*])
}
}
Err(e) => {
let error_msg = e.to_string();
quote! {
Err(#error_msg)
}
}
}
.into()
}
3 changes: 2 additions & 1 deletion common/logging/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,15 @@ test_logger = [] # Print log output to stderr when running tests instead of drop

[dependencies]
chrono = { version = "0.4", default-features = false, features = ["clock", "std"] }
lighthouse_macros = { workspace = true }
logroller = { workspace = true }
metrics = { workspace = true }
once_cell = "1.17.1"
parking_lot = { workspace = true }
serde = { workspace = true }
serde_json = { workspace = true }
tokio = { workspace = true, features = [ "time" ] }
tracing = "0.1"
tracing = { workspace = true }
tracing-appender = { workspace = true }
tracing-core = { workspace = true }
tracing-log = { workspace = true }
Expand Down
142 changes: 8 additions & 134 deletions common/logging/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,24 +1,24 @@
use chrono::Local;
use logroller::{Compression, LogRollerBuilder, Rotation, RotationSize};
use metrics::{try_create_int_counter, IntCounter, Result as MetricsResult};
use std::io::Write;
use std::path::PathBuf;
use std::sync::LazyLock;
use std::time::{Duration, Instant};
use tracing::Subscriber;
use tracing_appender::non_blocking::{NonBlocking, WorkerGuard};
use tracing_subscriber::layer::Context;
use tracing_subscriber::{EnvFilter, Layer};
use tracing_subscriber::EnvFilter;

pub const MAX_MESSAGE_WIDTH: usize = 40;

pub mod macros;
mod sse_logging_components;
mod tracing_libp2p_discv5_logging_layer;
pub mod tracing_logging_layer;
mod tracing_metrics_layer;
pub mod utils;

pub use sse_logging_components::SSELoggingComponents;
pub use tracing_libp2p_discv5_logging_layer::{
create_libp2p_discv5_tracing_layer, Libp2pDiscv5TracingLayer,
};
pub use tracing_logging_layer::LoggingLayer;
pub use tracing_metrics_layer::MetricsLayer;
pub use utils::build_workspace_filter;

/// The minimum interval between log messages indicating that a queue is full.
const LOG_DEBOUNCE_INTERVAL: Duration = Duration::from_secs(30);
Expand Down Expand Up @@ -51,132 +51,6 @@ impl TimeLatch {
}
}

pub struct Libp2pDiscv5TracingLayer {
pub libp2p_non_blocking_writer: NonBlocking,
pub _libp2p_guard: WorkerGuard,
pub discv5_non_blocking_writer: NonBlocking,
pub _discv5_guard: WorkerGuard,
}

impl<S> Layer<S> for Libp2pDiscv5TracingLayer
where
S: Subscriber,
{
fn on_event(&self, event: &tracing::Event<'_>, _ctx: Context<S>) {
let meta = event.metadata();
let log_level = meta.level();
let timestamp = Local::now().format("%Y-%m-%d %H:%M:%S").to_string();

let target = match meta.target().split_once("::") {
Some((crate_name, _)) => crate_name,
None => "unknown",
};

let mut writer = match target {
"gossipsub" => self.libp2p_non_blocking_writer.clone(),
"discv5" => self.discv5_non_blocking_writer.clone(),
_ => return,
};

let mut visitor = LogMessageExtractor {
message: String::default(),
};

event.record(&mut visitor);
let message = format!("{} {} {}\n", timestamp, log_level, visitor.message);

if let Err(e) = writer.write_all(message.as_bytes()) {
eprintln!("Failed to write log: {}", e);
}
}
}

struct LogMessageExtractor {
message: String,
}

impl tracing_core::field::Visit for LogMessageExtractor {
fn record_debug(&mut self, _: &tracing_core::Field, value: &dyn std::fmt::Debug) {
self.message = format!("{} {:?}", self.message, value);
}
}

pub fn create_libp2p_discv5_tracing_layer(
base_tracing_log_path: Option<PathBuf>,
max_log_size: u64,
compression: bool,
max_log_number: usize,
) -> Libp2pDiscv5TracingLayer {
if let Some(mut tracing_log_path) = base_tracing_log_path {
// Ensure that `tracing_log_path` only contains directories.
for p in tracing_log_path.clone().iter() {
tracing_log_path = tracing_log_path.join(p);
if let Ok(metadata) = tracing_log_path.metadata() {
if !metadata.is_dir() {
tracing_log_path.pop();
break;
}
}
}

let mut libp2p_writer =
LogRollerBuilder::new(tracing_log_path.clone(), PathBuf::from("libp2p.log"))
.rotation(Rotation::SizeBased(RotationSize::MB(max_log_size)))
.max_keep_files(max_log_number.try_into().unwrap_or_else(|e| {
eprintln!("Failed to convert max_log_number to u64: {}", e);
10
}));

let mut discv5_writer =
LogRollerBuilder::new(tracing_log_path.clone(), PathBuf::from("discv5.log"))
.rotation(Rotation::SizeBased(RotationSize::MB(max_log_size)))
.max_keep_files(max_log_number.try_into().unwrap_or_else(|e| {
eprintln!("Failed to convert max_log_number to u64: {}", e);
10
}));

if compression {
libp2p_writer = libp2p_writer.compression(Compression::Gzip);
discv5_writer = discv5_writer.compression(Compression::Gzip);
}

let libp2p_writer = match libp2p_writer.build() {
Ok(writer) => writer,
Err(e) => {
eprintln!("Failed to initialize libp2p rolling file appender: {e}");
std::process::exit(1);
}
};

let discv5_writer = match discv5_writer.build() {
Ok(writer) => writer,
Err(e) => {
eprintln!("Failed to initialize discv5 rolling file appender: {e}");
std::process::exit(1);
}
};

let (libp2p_non_blocking_writer, _libp2p_guard) = NonBlocking::new(libp2p_writer);
let (discv5_non_blocking_writer, _discv5_guard) = NonBlocking::new(discv5_writer);

Libp2pDiscv5TracingLayer {
libp2p_non_blocking_writer,
_libp2p_guard,
discv5_non_blocking_writer,
_discv5_guard,
}
} else {
let (libp2p_non_blocking_writer, _libp2p_guard) = NonBlocking::new(std::io::sink());
let (discv5_non_blocking_writer, _discv5_guard) = NonBlocking::new(std::io::sink());
Libp2pDiscv5TracingLayer {
libp2p_non_blocking_writer,
_libp2p_guard,
discv5_non_blocking_writer,
_discv5_guard,
}
}
}

/// Return a tracing subscriber suitable for test usage.
///
/// By default no logs will be printed, but they can be enabled via
Expand Down
Loading
Loading