Skip to content
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

refactor: Tracing #9544

Merged
merged 1 commit into from
Sep 20, 2023
Merged
Show file tree
Hide file tree
Changes from all 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
77 changes: 77 additions & 0 deletions core/o11y/src/env_filter.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
use std::borrow::Cow;
use tracing_subscriber::filter::ParseError;
use tracing_subscriber::EnvFilter;

/// The default value for the `RUST_LOG` environment variable if one isn't specified otherwise.
const DEFAULT_RUST_LOG: &str = "tokio_reactor=info,\
config=info,\
near=info,\
recompress=info,\
stats=info,\
telemetry=info,\
db=info,\
delay_detector=info,\
near-performance-metrics=info,\
warn";

#[non_exhaustive]
#[derive(thiserror::Error, Debug)]
pub enum BuildEnvFilterError {
#[error("could not create a log filter for {1}")]
CreateEnvFilter(#[source] ParseError, String),
}

#[derive(Debug)]
pub struct EnvFilterBuilder<'a> {
rust_log: Cow<'a, str>,
verbose: Option<&'a str>,
}

impl<'a> EnvFilterBuilder<'a> {
/// Create the `EnvFilter` from the environment variable or the [`DEFAULT_RUST_LOG`] value if
/// the environment is not set.
pub fn from_env() -> Self {
Self::new(
std::env::var("RUST_LOG").map(Cow::Owned).unwrap_or(Cow::Borrowed(DEFAULT_RUST_LOG)),
)
}

/// Specify an exact `RUST_LOG` value to use.
///
/// This method will not inspect the environment variable.
pub fn new<S: Into<Cow<'a, str>>>(rust_log: S) -> Self {
Self { rust_log: rust_log.into(), verbose: None }
}

/// Make the produced [`EnvFilter`] verbose.
///
/// If the `module` string is empty, all targets will log debug output. Otherwise only the
/// specified target will log the debug output.
pub fn verbose(mut self, target: Option<&'a str>) -> Self {
self.verbose = target;
self
}

/// Construct an [`EnvFilter`] as configured.
pub fn finish(self) -> Result<EnvFilter, BuildEnvFilterError> {
let mut env_filter = EnvFilter::try_new(self.rust_log.clone())
.map_err(|err| BuildEnvFilterError::CreateEnvFilter(err, self.rust_log.to_string()))?;
if let Some(module) = self.verbose {
env_filter = env_filter
.add_directive("cranelift_codegen=warn".parse().expect("parse directive"))
.add_directive("h2=warn".parse().expect("parse directive"))
.add_directive("tower=warn".parse().expect("parse directive"))
.add_directive("trust_dns_resolver=warn".parse().expect("parse directive"))
.add_directive("trust_dns_proto=warn".parse().expect("parse directive"));
env_filter = if module.is_empty() {
env_filter.add_directive(tracing::Level::DEBUG.into())
} else {
let directive = format!("{}=debug", module).parse().map_err(|err| {
BuildEnvFilterError::CreateEnvFilter(err, format!("{}=debug", module))
})?;
env_filter.add_directive(directive)
};
}
Ok(env_filter)
}
}
Loading