From 34fccc77b5e97be3c117f3abc08d290d4cb10fe2 Mon Sep 17 00:00:00 2001 From: Andy Lok Date: Mon, 5 Feb 2024 15:56:16 +0800 Subject: [PATCH] feat: single thread mode (#201) * feat: single thread mode Signed-off-by: Andy Lok * fix Signed-off-by: Andy Lok * fix --------- Signed-off-by: Andy Lok --- minitrace/src/collector/global_collector.rs | 63 +++++++++++++-------- minitrace/src/lib.rs | 1 + 2 files changed, 39 insertions(+), 25 deletions(-) diff --git a/minitrace/src/collector/global_collector.rs b/minitrace/src/collector/global_collector.rs index ebbcd603..06b61e3d 100644 --- a/minitrace/src/collector/global_collector.rs +++ b/minitrace/src/collector/global_collector.rs @@ -9,6 +9,7 @@ use std::sync::Arc; use std::time::Duration; use minstant::Anchor; +use minstant::Instant; use once_cell::sync::Lazy; use parking_lot::Mutex; @@ -91,16 +92,25 @@ pub(crate) fn reporter_ready() -> bool { pub fn flush() { #[cfg(feature = "enable")] { - // Spawns a new thread to ensure the reporter operates outside the tokio runtime to prevent panic. - std::thread::Builder::new() - .name("minitrace-flush".to_string()) - .spawn(move || { - let mut global_collector = GLOBAL_COLLECTOR.lock(); - global_collector.handle_commands(true); - }) - .unwrap() - .join() - .unwrap(); + #[cfg(target_family = "wasm")] + { + let mut global_collector = GLOBAL_COLLECTOR.lock(); + global_collector.handle_commands(true); + } + + #[cfg(not(target_family = "wasm"))] + { + // Spawns a new thread to ensure the reporter operates outside the tokio runtime to prevent panic. + std::thread::Builder::new() + .name("minitrace-flush".to_string()) + .spawn(move || { + let mut global_collector = GLOBAL_COLLECTOR.lock(); + global_collector.handle_commands(true); + }) + .unwrap() + .join() + .unwrap(); + } } } @@ -184,7 +194,7 @@ pub(crate) struct GlobalCollector { active_collectors: HashMap, usize)>, committed_records: Vec, - last_report: std::time::Instant, + last_report: Instant, // Vectors to be reused by collection loops. They must be empty outside of the `handle_commands` loop. start_collects: Vec, @@ -204,18 +214,21 @@ impl GlobalCollector { ) } - std::thread::Builder::new() - .name("minitrace-global-collector".to_string()) - .spawn(move || { - loop { - let begin_instant = std::time::Instant::now(); - GLOBAL_COLLECTOR.lock().handle_commands(false); - std::thread::sleep( - COLLECT_LOOP_INTERVAL.saturating_sub(begin_instant.elapsed()), - ); - } - }) - .unwrap(); + #[cfg(not(target_family = "wasm"))] + { + std::thread::Builder::new() + .name("minitrace-global-collector".to_string()) + .spawn(move || { + loop { + let begin_instant = Instant::now(); + GLOBAL_COLLECTOR.lock().handle_commands(false); + std::thread::sleep( + COLLECT_LOOP_INTERVAL.saturating_sub(begin_instant.elapsed()), + ); + } + }) + .unwrap(); + } GlobalCollector { config: Config::default().max_spans_per_trace(Some(0)), @@ -223,7 +236,7 @@ impl GlobalCollector { active_collectors: HashMap::new(), committed_records: Vec::new(), - last_report: std::time::Instant::now(), + last_report: Instant::now(), start_collects: Vec::new(), drop_collects: Vec::new(), @@ -412,7 +425,7 @@ impl GlobalCollector { .as_mut() .unwrap() .report(committed_records.drain(..).as_slice()); - self.last_report = std::time::Instant::now(); + self.last_report = Instant::now(); } } } diff --git a/minitrace/src/lib.rs b/minitrace/src/lib.rs index 91b148e0..7daa1766 100644 --- a/minitrace/src/lib.rs +++ b/minitrace/src/lib.rs @@ -366,6 +366,7 @@ #![cfg_attr(not(feature = "enable"), allow(unused_mut))] #![cfg_attr(not(feature = "enable"), allow(unused_imports))] #![cfg_attr(not(feature = "enable"), allow(unused_variables))] +#![cfg_attr(target_family = "wasm", allow(dead_code))] pub mod collector; mod event;