From 6daa745446e06775d0965cebcb674e160d982518 Mon Sep 17 00:00:00 2001 From: asonix Date: Mon, 11 Dec 2023 14:58:53 -0600 Subject: [PATCH] Add optional ability to log spans --- defaults.toml | 1 + src/config/commandline.rs | 7 +++++++ src/config/defaults.rs | 2 ++ src/config/file.rs | 2 ++ src/init_tracing.rs | 12 ++++++++++-- 5 files changed, 22 insertions(+), 2 deletions(-) diff --git a/defaults.toml b/defaults.toml index 4e756ce..97a56ad 100644 --- a/defaults.toml +++ b/defaults.toml @@ -14,6 +14,7 @@ concurrency = 32 [tracing.logging] format = "normal" targets = "info" +log_spans = false [tracing.console] buffer_capacity = 102400 diff --git a/src/config/commandline.rs b/src/config/commandline.rs index 852d909..f929d3e 100644 --- a/src/config/commandline.rs +++ b/src/config/commandline.rs @@ -17,6 +17,7 @@ impl Args { old_repo_cache_capacity, log_format, log_targets, + log_spans, console_address, console_buffer_capacity, opentelemetry_url, @@ -36,6 +37,7 @@ impl Args { logging: Logging { format: log_format, targets: log_targets.map(Serde::new), + log_spans, }, console: Console { address: console_address, @@ -550,6 +552,8 @@ struct Logging { format: Option, #[serde(skip_serializing_if = "Option::is_none")] targets: Option>, + #[serde(skip_serializing_if = "std::ops::Not::not")] + log_spans: bool, } #[derive(Debug, Default, serde::Serialize)] @@ -854,6 +858,9 @@ pub(super) struct Args { /// Log levels to print to stdout, respects RUST_LOG formatting #[arg(long)] log_targets: Option, + /// Whether to log openning and closing of tracing spans to stdout + #[arg(long)] + log_spans: bool, /// Address and port to expose tokio-console metrics #[arg(long)] diff --git a/src/config/defaults.rs b/src/config/defaults.rs index 74f4a34..a56489a 100644 --- a/src/config/defaults.rs +++ b/src/config/defaults.rs @@ -53,6 +53,7 @@ struct TracingDefaults { struct LoggingDefaults { format: LogFormat, targets: Serde, + log_spans: bool, } #[derive(Clone, Debug, serde::Serialize)] @@ -207,6 +208,7 @@ impl Default for LoggingDefaults { LoggingDefaults { format: LogFormat::Normal, targets: "info".parse().expect("Valid targets string"), + log_spans: false, } } } diff --git a/src/config/file.rs b/src/config/file.rs index 541baf4..bb85d95 100644 --- a/src/config/file.rs +++ b/src/config/file.rs @@ -153,6 +153,8 @@ pub(crate) struct Logging { pub(crate) format: LogFormat, pub(crate) targets: Serde, + + pub(crate) log_spans: bool, } #[derive(Clone, Debug, serde::Deserialize, serde::Serialize)] diff --git a/src/init_tracing.rs b/src/init_tracing.rs index 6ddf7e6..bf4cd8a 100644 --- a/src/init_tracing.rs +++ b/src/init_tracing.rs @@ -6,7 +6,9 @@ use opentelemetry_sdk::{propagation::TraceContextPropagator, Resource}; use tracing::subscriber::set_global_default; use tracing_error::ErrorLayer; use tracing_log::LogTracer; -use tracing_subscriber::{layer::SubscriberExt, registry::LookupSpan, Layer, Registry}; +use tracing_subscriber::{ + fmt::format::FmtSpan, layer::SubscriberExt, registry::LookupSpan, Layer, Registry, +}; pub(super) fn init_tracing(tracing: &Tracing) -> color_eyre::Result<()> { color_eyre::install()?; @@ -15,7 +17,13 @@ pub(super) fn init_tracing(tracing: &Tracing) -> color_eyre::Result<()> { opentelemetry::global::set_text_map_propagator(TraceContextPropagator::new()); - let format_layer = tracing_subscriber::fmt::layer(); + let fmt_span = if tracing.logging.log_spans { + FmtSpan::NEW | FmtSpan::CLOSE + } else { + FmtSpan::NONE + }; + + let format_layer = tracing_subscriber::fmt::layer().with_span_events(fmt_span); match tracing.logging.format { LogFormat::Compact => with_format(format_layer.compact(), tracing),