Skip to content

Commit

Permalink
Merge branch 'master' into master
Browse files Browse the repository at this point in the history
  • Loading branch information
iGxnon authored Feb 6, 2024
2 parents 47fc517 + 34fccc7 commit 4cd0de8
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 25 deletions.
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

0 comments on commit 4cd0de8

Please sign in to comment.