diff --git a/ipa-core/benches/oneshot/ipa.rs b/ipa-core/benches/oneshot/ipa.rs index 7b4ddcce56..d0ac69da6e 100644 --- a/ipa-core/benches/oneshot/ipa.rs +++ b/ipa-core/benches/oneshot/ipa.rs @@ -100,11 +100,14 @@ async fn run(args: Args) -> Result<(), Error> { let _prep_time = Instant::now(); let config = TestWorldConfig { gateway_config: GatewayConfig::new(args.active()), + extra_log_target: option_env!("CARGO_CRATE_NAME"), ..TestWorldConfig::default() }; + // Construct TestWorld early to initialize logging. + let world = TestWorld::new_with(&config); let seed = args.random_seed.unwrap_or_else(|| random()); - tracing::trace!( + tracing::info!( "Using random seed: {seed} for {q} records", q = args.query_size ); @@ -154,12 +157,11 @@ async fn run(args: Args) -> Result<(), Error> { &order, ); - let world = TestWorld::new_with(config.clone()); tracing::trace!("Preparation complete in {:?}", _prep_time.elapsed()); let _protocol_time = Instant::now(); test_oprf_ipa::(&world, raw_data, &expected_results, args.config()).await; - tracing::trace!( + tracing::info!( "{m:?} IPA for {q} records took {t:?}", m = args.mode, q = args.query_size, diff --git a/ipa-core/src/helpers/transport/in_memory/sharding.rs b/ipa-core/src/helpers/transport/in_memory/sharding.rs index 0700793cc2..603134dc5c 100644 --- a/ipa-core/src/helpers/transport/in_memory/sharding.rs +++ b/ipa-core/src/helpers/transport/in_memory/sharding.rs @@ -97,7 +97,7 @@ mod tests { #[test] fn shards_talk_to_each_other() { - logging::setup(); + logging::setup(None); run(|| async { let shard_count = 5; let shard_network = InMemoryShardNetwork::with_shards(shard_count); diff --git a/ipa-core/src/test_fixture/logging.rs b/ipa-core/src/test_fixture/logging.rs index 8490387f84..21525d7340 100644 --- a/ipa-core/src/test_fixture/logging.rs +++ b/ipa-core/src/test_fixture/logging.rs @@ -1,12 +1,12 @@ -use std::sync::Once; +use std::{env, sync::Once}; + +static INIT: Once = Once::new(); /// Set up logging for IPA /// /// ## Panics /// Does not, but compiler cannot be convinced otherwise -pub fn setup() { - static INIT: Once = Once::new(); - +pub fn setup(extra_log_target: Option<&'static str>) { INIT.call_once(|| { #[cfg(feature = "tokio-console")] { @@ -15,26 +15,31 @@ pub fn setup() { #[cfg(not(feature = "tokio-console"))] { - use std::str::FromStr; - use metrics_tracing_context::MetricsLayer; use tracing::Level; use tracing_subscriber::{ - filter::Directive, fmt, layer::SubscriberExt, util::SubscriberInitExt, EnvFilter, + fmt, layer::SubscriberExt, util::SubscriberInitExt, EnvFilter, }; - let default_directive = if let Some(crate_name) = option_env!("CARGO_CRATE_NAME") { - // only print IPA crate logging by default - Directive::from_str(&format!("{crate_name}=INFO")).unwrap() - } else { - Level::INFO.into() - }; + let directives = env::var(EnvFilter::DEFAULT_ENV).unwrap_or_else(|_| { + // The purpose of this logic is to only print logs from IPA + // library/binary crate(s) by default. + match (option_env!("CARGO_CRATE_NAME"), extra_log_target) { + (Some(t1), Some(t2)) => { + format!("{t1}=INFO,{t2}=INFO") + } + (Some(t1), None) | (None, Some(t1)) => { + format!("{t1}=INFO") + } + (None, None) => String::new(), + } + }); tracing_subscriber::registry() .with( EnvFilter::builder() - .with_default_directive(default_directive) - .from_env_lossy(), + .with_default_directive(Level::INFO.into()) + .parse_lossy(directives), ) .with(fmt::layer()) .with(MetricsLayer::new()) @@ -42,3 +47,7 @@ pub fn setup() { } }); } + +pub fn is_initialized() -> bool { + INIT.is_completed() +} diff --git a/ipa-core/src/test_fixture/metrics.rs b/ipa-core/src/test_fixture/metrics.rs index 80acf46206..f8a37fdc53 100644 --- a/ipa-core/src/test_fixture/metrics.rs +++ b/ipa-core/src/test_fixture/metrics.rs @@ -19,7 +19,7 @@ static ONCE: OnceCell = OnceCell::new(); fn setup() { // logging is required to import span fields as metric values - logging::setup(); + assert!(logging::is_initialized()); ONCE.get_or_init(|| { assert!( diff --git a/ipa-core/src/test_fixture/world.rs b/ipa-core/src/test_fixture/world.rs index 654fc811e8..4be843b3b9 100644 --- a/ipa-core/src/test_fixture/world.rs +++ b/ipa-core/src/test_fixture/world.rs @@ -94,6 +94,8 @@ pub struct TestWorldConfig { pub role_assignment: Option, /// Seed for random generators used in PRSS pub seed: u64, + /// Additional target to include in default logging directive + pub extra_log_target: Option<&'static str>, } impl ShardingScheme for NotSharded { @@ -230,7 +232,7 @@ impl TestWorld { /// If more than [`std::u32::MAX`] shards are requested. #[must_use] pub fn with_config(config: &TestWorldConfig) -> Self { - logging::setup(); + logging::setup(config.extra_log_target); println!("Using seed {seed}", seed = config.seed); let shard_count = ShardIndex::try_from(S::SHARDS).unwrap(); @@ -268,6 +270,7 @@ impl Default for TestWorldConfig { metrics_level: Level::DEBUG, role_assignment: None, seed: thread_rng().next_u64(), + extra_log_target: None, } } }