From 59704c2055aac21d903eb748dfcb1d8abcfee0b4 Mon Sep 17 00:00:00 2001 From: Andy Lok Date: Sun, 4 Feb 2024 22:56:46 +0800 Subject: [PATCH 1/3] feat: single thread mode Signed-off-by: Andy Lok --- minitrace/Cargo.toml | 1 + minitrace/src/collector/global_collector.rs | 56 +++++++++++++-------- minitrace/src/lib.rs | 1 + 3 files changed, 36 insertions(+), 22 deletions(-) diff --git a/minitrace/Cargo.toml b/minitrace/Cargo.toml index a63b99a8..0e8f1c36 100644 --- a/minitrace/Cargo.toml +++ b/minitrace/Cargo.toml @@ -14,6 +14,7 @@ keywords = ["tracing", "span", "datadog", "jaeger", "opentelemetry"] [features] enable = [] +single-thread = [] [dependencies] futures = "0.3" diff --git a/minitrace/src/collector/global_collector.rs b/minitrace/src/collector/global_collector.rs index ebbcd603..452b9452 100644 --- a/minitrace/src/collector/global_collector.rs +++ b/minitrace/src/collector/global_collector.rs @@ -91,16 +91,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(feature = "single-thread")] + { + let mut global_collector = GLOBAL_COLLECTOR.lock(); + global_collector.handle_commands(true); + } + + #[cfg(not(feature = "single-thread"))] + { + // 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(); + } } } @@ -204,18 +213,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(feature = "single-thread"))] + { + 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(); + } GlobalCollector { config: Config::default().max_spans_per_trace(Some(0)), diff --git a/minitrace/src/lib.rs b/minitrace/src/lib.rs index 91b148e0..4325e152 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(feature = "single-thread", allow(unused_variables))] pub mod collector; mod event; From 8eececa39cd32ad4c47ba1e456756e9a1557e562 Mon Sep 17 00:00:00 2001 From: Andy Lok Date: Sun, 4 Feb 2024 22:58:53 +0800 Subject: [PATCH 2/3] fix Signed-off-by: Andy Lok --- minitrace/src/lib.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/minitrace/src/lib.rs b/minitrace/src/lib.rs index 4325e152..8a83392c 100644 --- a/minitrace/src/lib.rs +++ b/minitrace/src/lib.rs @@ -366,7 +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(feature = "single-thread", allow(unused_variables))] +#![cfg_attr(feature = "single-thread", allow(dead_code))] pub mod collector; mod event; From 2c2aed91130acf85d707fbf70c2c7f6cadb2e3eb Mon Sep 17 00:00:00 2001 From: Andy Lok Date: Mon, 5 Feb 2024 15:13:28 +0800 Subject: [PATCH 3/3] fix --- minitrace/Cargo.toml | 1 - minitrace/src/collector/global_collector.rs | 15 ++++++++------- minitrace/src/lib.rs | 2 +- 3 files changed, 9 insertions(+), 9 deletions(-) diff --git a/minitrace/Cargo.toml b/minitrace/Cargo.toml index 0e8f1c36..a63b99a8 100644 --- a/minitrace/Cargo.toml +++ b/minitrace/Cargo.toml @@ -14,7 +14,6 @@ keywords = ["tracing", "span", "datadog", "jaeger", "opentelemetry"] [features] enable = [] -single-thread = [] [dependencies] futures = "0.3" diff --git a/minitrace/src/collector/global_collector.rs b/minitrace/src/collector/global_collector.rs index 452b9452..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,13 +92,13 @@ pub(crate) fn reporter_ready() -> bool { pub fn flush() { #[cfg(feature = "enable")] { - #[cfg(feature = "single-thread")] + #[cfg(target_family = "wasm")] { let mut global_collector = GLOBAL_COLLECTOR.lock(); global_collector.handle_commands(true); } - #[cfg(not(feature = "single-thread"))] + #[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() @@ -193,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, @@ -213,13 +214,13 @@ impl GlobalCollector { ) } - #[cfg(not(feature = "single-thread"))] + #[cfg(not(target_family = "wasm"))] { std::thread::Builder::new() .name("minitrace-global-collector".to_string()) .spawn(move || { loop { - let begin_instant = std::time::Instant::now(); + let begin_instant = Instant::now(); GLOBAL_COLLECTOR.lock().handle_commands(false); std::thread::sleep( COLLECT_LOOP_INTERVAL.saturating_sub(begin_instant.elapsed()), @@ -235,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(), @@ -424,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 8a83392c..7daa1766 100644 --- a/minitrace/src/lib.rs +++ b/minitrace/src/lib.rs @@ -366,7 +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(feature = "single-thread", allow(dead_code))] +#![cfg_attr(target_family = "wasm", allow(dead_code))] pub mod collector; mod event;