-
Notifications
You must be signed in to change notification settings - Fork 773
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
Critical dependency logging #4988
Merged
AgeManning
merged 36 commits into
sigp:unstable
from
eserilev:critical-dependency-logging
Jan 16, 2024
Merged
Changes from all commits
Commits
Show all changes
36 commits
Select commit
Hold shift + click to select a range
0087a0e
add metrics layer
divagant-martian 39b2533
add metrics
divagant-martian 57baf2d
simplify getting the target
divagant-martian 71ad6a0
make clippy happy
divagant-martian dc46e92
fix typos
divagant-martian b3e5ea0
unify deps under workspace
divagant-martian 94573c7
make import statement shorter, fix typos
divagant-martian a18c189
enable warn by default, mark flag as deprecated
divagant-martian 908ad21
do not exit on error when initializing logging fails
divagant-martian 9dd307d
revert exit on error
divagant-martian 9a6d862
adjust bootnode logging
divagant-martian 5b2395b
add logging layer
eserilev de2840e
non blocking file writer
eserilev d6ff17e
non blocking file writer
eserilev a158865
add tracing visitor
eserilev 1ffac2c
Merge branch 'unstable' of https://github.com/sigp/lighthouse into cr…
eserilev 85deec7
use target as is by default
divagant-martian 46e0ec9
make libp2p events register correctly
divagant-martian c2b7423
Merge branch 'unstable' into deps-logging-metrics
divagant-martian f6c4d27
adjust repilcated cli help
divagant-martian bedf55f
refactor tracing layer
eserilev 7e227a0
linting
eserilev 5bbf27f
filesize
eserilev 6ce1a4e
log gossipsub, dont filter by log level
eserilev c222b36
turn on debug logs by default, remove deprecation warning
divagant-martian e256953
Merge branch 'unstable' into deps-logging-metrics
divagant-martian 72ee781
resolve merge conflicts
eserilev 509a421
truncate file, add timestamp, add unit test
eserilev 0d73f76
suppress output (#5)
divagant-martian f25dcb6
use tracing appender
eserilev 5abb2c5
merge changes from 4979
eserilev 958bb7b
cleanup
eserilev ebf4b29
Merge latest unstable
AgeManning f7dea99
Add a task to remove old log files and upgrade to warn level
AgeManning 32bbbfd
Add the time feature for tokio
AgeManning b2f9d96
Udeps and fmt
AgeManning File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,115 @@ | ||
use chrono::{naive::Days, prelude::*}; | ||
use slog::{debug, warn}; | ||
use std::io::Write; | ||
use tracing::Subscriber; | ||
use tracing_appender::non_blocking::{NonBlocking, WorkerGuard}; | ||
use tracing_subscriber::layer::Context; | ||
use tracing_subscriber::Layer; | ||
|
||
pub struct LoggingLayer { | ||
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 LoggingLayer | ||
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 { | ||
"libp2p_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); | ||
} | ||
} | ||
|
||
/// Creates a long lived async task that routinely deletes old tracing log files | ||
pub async fn cleanup_logging_task(path: std::path::PathBuf, log: slog::Logger) { | ||
loop { | ||
// Delay for 1 day and then prune old logs | ||
tokio::time::sleep(std::time::Duration::from_secs(60 * 60 * 24)).await; | ||
|
||
let Some(yesterday_date) = chrono::prelude::Local::now() | ||
.naive_local() | ||
.checked_sub_days(Days::new(1)) | ||
else { | ||
warn!(log, "Could not calculate the current date"); | ||
return; | ||
}; | ||
|
||
// Search for old log files | ||
let dir = path.as_path(); | ||
|
||
if dir.is_dir() { | ||
let Ok(files) = std::fs::read_dir(dir) else { | ||
warn!(log, "Could not read log directory contents"; "path" => ?dir); | ||
break; | ||
}; | ||
|
||
for file in files { | ||
let Ok(dir_entry) = file else { | ||
warn!(log, "Could not read file"); | ||
continue; | ||
}; | ||
|
||
let Ok(file_name) = dir_entry.file_name().into_string() else { | ||
warn!(log, "Could not read file"; "file" => ?dir_entry); | ||
continue; | ||
}; | ||
|
||
if file_name.starts_with("libp2p.log") | file_name.starts_with("discv5.log") { | ||
let log_file_date = file_name.split('.').collect::<Vec<_>>(); | ||
if log_file_date.len() == 3 { | ||
let Ok(log_file_date_type) = | ||
NaiveDate::parse_from_str(log_file_date[2], "%Y-%m-%d") | ||
else { | ||
warn!(log, "Could not parse log file date"; "file" => file_name); | ||
continue; | ||
}; | ||
|
||
if log_file_date_type < yesterday_date.into() { | ||
// Delete the file, its too old | ||
debug!(log, "Removing old log file"; "file" => &file_name); | ||
if let Err(e) = std::fs::remove_file(dir_entry.path()) { | ||
warn!(log, "Failed to remove log file"; "file" => file_name, "error" => %e); | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe we use debug by default?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this one's for me, will change to debug
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done in c222b36