From c6d92b83358410c108f9106285be2237f0fab0fc Mon Sep 17 00:00:00 2001 From: Justin Abrahms Date: Tue, 9 Jan 2024 13:55:06 -0800 Subject: [PATCH 1/2] feat: Add support for BYO tracing_subscriber, which exfiltrating instrumentation from noosphere --- rust/noosphere-core/src/tracing.rs | 38 +++++++++++++++++++++++------- 1 file changed, 30 insertions(+), 8 deletions(-) diff --git a/rust/noosphere-core/src/tracing.rs b/rust/noosphere-core/src/tracing.rs index 181b4c1e9..03c07dbd3 100644 --- a/rust/noosphere-core/src/tracing.rs +++ b/rust/noosphere-core/src/tracing.rs @@ -183,10 +183,10 @@ mod inner { use tracing::{Event, Subscriber}; use tracing_subscriber::{ filter::Directive, - fmt::{format, FmtContext, FormatEvent, FormatFields, FormattedFields, Layer}, + fmt::{format, FmtContext, FormatEvent, FormatFields, FormattedFields, Layer as FmtLayer}, prelude::*, registry::LookupSpan, - EnvFilter, + EnvFilter, Layer, Registry, }; // Mainly we disable this for iOS because it causes XCode @@ -247,13 +247,33 @@ mod inner { /// [2]: https://docs.rs/env_logger/0.10.0/env_logger/#enabling-logging pub fn initialize_tracing(noosphere_log: Option) { INITIALIZE_TRACING.call_once(|| { - if let Err(error) = initialize_tracing_subscriber(noosphere_log) { + if let Err(error) = initialize_tracing_subscriber::< + Option + Send + Sync>>, + >(noosphere_log, None) + { println!("Failed to initialize tracing: {}", error); } }); } - fn initialize_tracing_subscriber(noosphere_log: Option) -> anyhow::Result<()> { + pub fn initialize_tracing_with_layer(noosphere_log: Option, layer: T) + where + T: Layer + Send + Sync + Sized, + { + INITIALIZE_TRACING.call_once(|| { + if let Err(error) = initialize_tracing_subscriber(noosphere_log, layer) { + println!("Failed to initialize tracing: {}", error); + } + }); + } + + fn initialize_tracing_subscriber( + noosphere_log: Option, + layer: T, + ) -> anyhow::Result<()> + where + T: Layer + Send + Sync + Sized, + { let rust_log_env = std::env::var("RUST_LOG").ok(); let noosphere_log_env = std::env::var("NOOSPHERE_LOG").ok(); let noosphere_log_level_env = std::env::var("NOOSPHERE_LOG_LEVEL").ok(); @@ -315,12 +335,14 @@ mod inner { env_filter = env_filter.add_directive(directive) } - let subscriber = tracing_subscriber::registry().with(env_filter); + let subscriber = layer + .and_then(env_filter) + .with_subscriber(tracing_subscriber::registry()); match noosphere_log_format { NoosphereLogFormat::Minimal => { let subscriber = subscriber.with( - Layer::default().event_format(NoosphereMinimalFormatter::new( + FmtLayer::default().event_format(NoosphereMinimalFormatter::new( tracing_subscriber::fmt::format() .without_time() .with_target(false) @@ -344,7 +366,7 @@ mod inner { } NoosphereLogFormat::Pretty => { let subscriber = - subscriber.with(Layer::default().pretty().with_ansi(USE_ANSI_COLORS)); + subscriber.with(FmtLayer::default().pretty().with_ansi(USE_ANSI_COLORS)); #[cfg(feature = "sentry")] let subscriber = subscriber.with(sentry_tracing::layer()); @@ -353,7 +375,7 @@ mod inner { } NoosphereLogFormat::Structured => { let subscriber = - subscriber.with(Layer::default().json().with_ansi(USE_ANSI_COLORS)); + subscriber.with(FmtLayer::default().json().with_ansi(USE_ANSI_COLORS)); #[cfg(feature = "sentry")] let subscriber = subscriber.with(sentry_tracing::layer()); From cca033694b51ccafd87d5aad353ad6e5a15404d0 Mon Sep 17 00:00:00 2001 From: Justin Abrahms Date: Wed, 10 Jan 2024 10:09:40 -0800 Subject: [PATCH 2/2] Docs for new tracing method --- rust/noosphere-core/src/tracing.rs | 2 ++ 1 file changed, 2 insertions(+) diff --git a/rust/noosphere-core/src/tracing.rs b/rust/noosphere-core/src/tracing.rs index 03c07dbd3..a382676dc 100644 --- a/rust/noosphere-core/src/tracing.rs +++ b/rust/noosphere-core/src/tracing.rs @@ -256,6 +256,8 @@ mod inner { }); } + /// Identical to [initialize_tracing], but provides the ability to add in + /// your own [Layer] for tracing. pub fn initialize_tracing_with_layer(noosphere_log: Option, layer: T) where T: Layer + Send + Sync + Sized,