use std::time::Duration;
use dial9::Dial9Handle;
use dial9::DiskBuffer;
const TRACE_DIR: &str = "/tmp/dial9-on-trigger-windows";
#[dial9::main(config = || {
use dial9::{Dial9HandleTokioExt, RecorderPipelineExt, TokioAttachOptions};
let _ = std::fs::remove_dir_all(TRACE_DIR);
let _ = std::fs::create_dir_all(TRACE_DIR);
let writer = DiskBuffer::builder()
.base_path(TRACE_DIR)
// Fast-rotating writer so the demo seals a segment every ~half second.
.max_file_size(4 * 1024)
.max_total_size(10 * 1024 * 1024)
.rotation_period(Duration::from_millis(500))
.build()
.expect("open trace writer");
let recorder = dial9::recorder(writer)
// No debounce here: we want the two concurrent dumps to each register,
// not fold into one another.
.with_custom_pipeline(|p| p.gzip().write_back())
.with_dump_trigger(|_| {})
.build();
let mut builder = tokio::runtime::Builder::new_multi_thread();
builder.enable_all().worker_threads(2);
let runtime = recorder
.handle()
.attach_tokio_runtime(builder, TokioAttachOptions::default())?;
Ok((recorder, runtime))
})]
async fn main() {
let trigger = Dial9Handle::current()
.dump_trigger()
.expect("on-demand mode enabled");
for id in 0..8 {
dial9::spawn(async move {
for _ in 0..800 {
tokio::time::sleep(Duration::from_millis(25)).await;
std::hint::black_box(id);
}
});
}
tokio::time::sleep(Duration::from_secs(3)).await;
let receipt = trigger
.dump_time_range(Duration::from_secs(3600), Duration::ZERO)
.with_metadata("reason", "look-back")
.await
.expect("look-back dump");
println!(
"look-back dump {}: {} segment(s), span {:?}",
receipt.dump_id, receipt.segments_processed, receipt.time_range,
);
let receipt = trigger
.dump_time_range(Duration::from_secs(1), Duration::from_secs(3))
.with_metadata("reason", "look-forward")
.await
.expect("look-forward dump");
println!(
"look-forward dump {}: {} segment(s), span {:?}",
receipt.dump_id, receipt.segments_processed, receipt.time_range,
);
let (a, b) = tokio::join!(
trigger.dump_time_range(Duration::from_secs(1), Duration::from_secs(3)),
trigger.dump_time_range(Duration::from_secs(1), Duration::from_secs(3)),
);
let a = a.expect("concurrent dump a");
let b = b.expect("concurrent dump b");
assert_ne!(a.dump_id, b.dump_id, "concurrent dumps get distinct ids");
println!(
"concurrent dumps {} ({} seg) and {} ({} seg) ran independently",
a.dump_id, a.segments_processed, b.dump_id, b.segments_processed,
);
println!("processed to disk: run `ls {TRACE_DIR}/*.bin.gz`");
}