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

feat: allow to provide log's "directives" via init_subscribers_and_loglevel #215

Merged
merged 1 commit into from
Feb 24, 2025
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
4 changes: 4 additions & 0 deletions init-tracing-opentelemetry/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,8 @@ pub enum Error {

#[error(transparent)]
TraceError(#[from] opentelemetry::trace::TraceError),

#[cfg(feature = "tracing_subscriber_ext")]
#[error(transparent)]
FilterParseError(#[from] tracing_subscriber::filter::ParseError),
}
61 changes: 43 additions & 18 deletions init-tracing-opentelemetry/src/tracing_subscriber_ext.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use opentelemetry::trace::{TraceError, TracerProvider};
use opentelemetry_sdk::trace::{SdkTracerProvider, Tracer};
use tracing::{info, Subscriber};
use tracing::{info, level_filters::LevelFilter, Subscriber};
use tracing_opentelemetry::OpenTelemetryLayer;
use tracing_subscriber::{filter::EnvFilter, layer::SubscriberExt, registry::LookupSpan, Layer};

Expand Down Expand Up @@ -43,21 +43,41 @@ where
}

#[must_use]
pub fn build_loglevel_filter_layer() -> tracing_subscriber::filter::EnvFilter {
// filter what is output on log (fmt)
// std::env::set_var("RUST_LOG", "warn,otel::tracing=info,otel=debug");
std::env::set_var(
"RUST_LOG",
format!(
// `otel::tracing` should be a level info to emit opentelemetry trace & span
// `otel::setup` set to debug to log detected resources, configuration read
"{},otel::tracing=trace",
std::env::var("RUST_LOG")
.or_else(|_| std::env::var("OTEL_LOG_LEVEL"))
.unwrap_or_else(|_| "info".to_string())
),
);
EnvFilter::from_default_env()
#[deprecated = "replaced by the configurable build_level_filter_layer(\"\")"]
pub fn build_loglevel_filter_layer() -> EnvFilter {
build_level_filter_layer("").unwrap_or_default()
}

/// Read the configuration from (first non empty used, priority top to bottom):
///
/// - from parameter `directives`
/// - from environment variable `RUST_LOG`
/// - from environment variable `OTEL_LOG_LEVEL`
/// - default to `Level::INFO`
///
/// And add directive to:
///
/// - `otel::tracing` should be a level info to emit opentelemetry trace & span
///
/// You can customize parameter "directives", by adding:
///
/// - `otel::setup=debug` set to debug to log detected resources, configuration read (optional)
///
/// see [Directives syntax](https://docs.rs/tracing-subscriber/latest/tracing_subscriber/filter/struct.EnvFilter.html#directives)
pub fn build_level_filter_layer(log_directives: &str) -> Result<EnvFilter, Error> {
let dirs = if log_directives.is_empty() {
std::env::var("RUST_LOG")
.or_else(|_| std::env::var("OTEL_LOG_LEVEL"))
.unwrap_or_else(|_| "info".to_string())
} else {
log_directives.to_string()
};
let directive_to_allow_otel_trace = "otel::tracing=trace".parse()?;

Ok(EnvFilter::builder()
.with_default_directive(LevelFilter::INFO.into())
.parse_lossy(dirs)
.add_directive(directive_to_allow_otel_trace))
}

pub fn build_otel_layer<S>() -> Result<(OpenTelemetryLayer<S, Tracer>, TracingGuard), TraceError>
Expand Down Expand Up @@ -105,9 +125,14 @@ impl Drop for TracingGuard {
}

pub fn init_subscribers() -> Result<TracingGuard, Error> {
init_subscribers_and_loglevel("")
}

/// see [`build_level_filter_layer`] for the syntax of `log_directives`
pub fn init_subscribers_and_loglevel(log_directives: &str) -> Result<TracingGuard, Error> {
//setup a temporary subscriber to log output during setup
let subscriber = tracing_subscriber::registry()
.with(build_loglevel_filter_layer())
.with(build_level_filter_layer(log_directives)?)
.with(build_logger_text());
let _guard = tracing::subscriber::set_default(subscriber);
info!("init logging & tracing");
Expand All @@ -116,7 +141,7 @@ pub fn init_subscribers() -> Result<TracingGuard, Error> {

let subscriber = tracing_subscriber::registry()
.with(layer)
.with(build_loglevel_filter_layer())
.with(build_level_filter_layer(log_directives)?)
.with(build_logger_text());
tracing::subscriber::set_global_default(subscriber)?;
Ok(guard)
Expand Down
Loading