Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: single thread mode #201

Merged
merged 3 commits into from
Feb 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
63 changes: 38 additions & 25 deletions minitrace/src/collector/global_collector.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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();
}
}
}

Expand Down Expand Up @@ -184,7 +194,7 @@ pub(crate) struct GlobalCollector {

active_collectors: HashMap<usize, (Vec<SpanCollection>, usize)>,
committed_records: Vec<SpanRecord>,
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<StartCollect>,
Expand All @@ -204,26 +214,29 @@ 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)),
reporter: None,

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(),
Expand Down Expand Up @@ -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();
}
}
}
Expand Down
1 change: 1 addition & 0 deletions minitrace/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Loading