From 9e74380d7c5e16edd2653488285b87313b431a8e Mon Sep 17 00:00:00 2001 From: Tiago Castro Date: Tue, 24 Oct 2023 19:11:17 +0100 Subject: [PATCH] feat: add common rust log filter with silenced modules Signed-off-by: Tiago Castro --- scripts/rust/linter.sh | 4 +++- tracing-filter/Cargo.toml | 9 +++++++++ tracing-filter/src/lib.rs | 37 +++++++++++++++++++++++++++++++++++++ 3 files changed, 49 insertions(+), 1 deletion(-) create mode 100644 tracing-filter/Cargo.toml create mode 100644 tracing-filter/src/lib.rs diff --git a/scripts/rust/linter.sh b/scripts/rust/linter.sh index d5be369..75f02c6 100755 --- a/scripts/rust/linter.sh +++ b/scripts/rust/linter.sh @@ -2,8 +2,10 @@ set -euo pipefail +FMT_OPTS=${FMT_OPTS:-"--check"} + for d in `find -maxdepth 2 -name Cargo.toml -printf '%h\n' | grep -v "^./h2" | grep -v "git-version-macro"`; do pushd $d - cargo-fmt --all -- --check + cargo-fmt --all -- $FMT_OPTS popd done diff --git a/tracing-filter/Cargo.toml b/tracing-filter/Cargo.toml new file mode 100644 index 0000000..a73502c --- /dev/null +++ b/tracing-filter/Cargo.toml @@ -0,0 +1,9 @@ +[package] +name = "tracing-filter" +version = "0.1.0" +edition = "2021" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] +tracing-subscriber = { version = "0.3.17", features = [ "env-filter" ] } diff --git a/tracing-filter/src/lib.rs b/tracing-filter/src/lib.rs new file mode 100644 index 0000000..061d4d9 --- /dev/null +++ b/tracing-filter/src/lib.rs @@ -0,0 +1,37 @@ +/// The default quiet filters in addition to `RUST_LOG`. +pub const RUST_LOG_SILENCE_DEFAULTS: &str = + "actix_web=info,actix_server=info,async_nats=info,h2=info,hyper=info,tower_buffer=info,tower=info,rustls=info,reqwest=info,tokio_util=info,tokio_tungstenite=info,tungstenite=info,async_io=info,polling=info,tonic=info,want=info,mio=info"; + +/// Mix the `RUST_LOG` `EnvFilter` with `RUST_LOG_SILENCE`. +/// This is useful when we want to bulk-silence certain crates by default. +pub fn rust_log_add_quiet_defaults( + current: tracing_subscriber::EnvFilter, +) -> tracing_subscriber::EnvFilter { + let rust_log_silence = std::env::var("RUST_LOG_SILENCE"); + let silence = match &rust_log_silence { + Ok(quiets) => quiets.as_str(), + Err(_) => RUST_LOG_SILENCE_DEFAULTS, + }; + + tracing_subscriber::EnvFilter::try_new(match silence.is_empty() { + true => current.to_string(), + false => format!("{current},{silence}"), + }) + .unwrap() +} + +/// Get the `tracing_subscriber::EnvFilter`, taken by mixing RUST_LOG +/// and RUST_LOG_SILENCE which silences unwanted traces. +/// If no default is set from env, use the provided filter. +pub fn rust_log_filter_ext(level: &str) -> tracing_subscriber::EnvFilter { + rust_log_add_quiet_defaults( + tracing_subscriber::EnvFilter::try_from_default_env() + .unwrap_or_else(|_| tracing_subscriber::EnvFilter::new(level)), + ) +} + +/// Get the `tracing_subscriber::EnvFilter`, taken by mixing RUST_LOG +/// and RUST_LOG_SILENCE which silences unwanted traces. +pub fn rust_log_filter() -> tracing_subscriber::EnvFilter { + rust_log_filter_ext("info") +}