diff --git a/examples/Cargo.toml b/examples/Cargo.toml index e75edfc8..39868995 100644 --- a/examples/Cargo.toml +++ b/examples/Cargo.toml @@ -48,7 +48,7 @@ log = "0.4" # inferno example inferno = "0.10.0" -tempdir = "0.3.7" +tempfile = "3" # opentelemetry example opentelemetry = { version = "0.16", default-features = false, features = ["trace"] } diff --git a/examples/examples/inferno-flame.rs b/examples/examples/inferno-flame.rs index 171b1df2..fca5de69 100644 --- a/examples/examples/inferno-flame.rs +++ b/examples/examples/inferno-flame.rs @@ -1,9 +1,11 @@ -use std::fs::File; -use std::io::{BufReader, BufWriter}; -use std::path::Path; -use std::thread::sleep; -use std::time::Duration; -use tempdir::TempDir; +use std::{ + env, + fs::File, + io::{BufReader, BufWriter}, + path::{Path, PathBuf}, + thread::sleep, + time::Duration, +}; use tracing::{span, Level}; use tracing_flame::FlameLayer; use tracing_subscriber::{prelude::*, registry::Registry}; @@ -20,11 +22,12 @@ fn setup_global_subscriber(dir: &Path) -> impl Drop { _guard } -fn make_flamegraph(dir: &Path) { - let inf = File::open(dir.join(PATH)).unwrap(); +fn make_flamegraph(tmpdir: &Path, out: &Path) { + println!("outputting flamegraph to {}", out.display()); + let inf = File::open(tmpdir.join(PATH)).unwrap(); let reader = BufReader::new(inf); - let out = File::create(dir.join("inferno.svg")).unwrap(); + let out = File::create(out).unwrap(); let writer = BufWriter::new(out); let mut opts = inferno::flamegraph::Options::default(); @@ -32,8 +35,19 @@ fn make_flamegraph(dir: &Path) { } fn main() { + let out = if let Some(arg) = env::args().nth(1) { + PathBuf::from(arg) + } else { + let mut path = env::current_dir().expect("failed to read current directory"); + path.push("tracing-flame-inferno.svg"); + path + }; + // setup the flame layer - let tmp_dir = TempDir::new("flamegraphs").unwrap(); + let tmp_dir = tempfile::Builder::new() + .prefix("flamegraphs") + .tempdir() + .expect("failed to create temporary directory"); let guard = setup_global_subscriber(tmp_dir.path()); // do a bunch of span entering and exiting to simulate a program running @@ -52,5 +66,5 @@ fn main() { // drop the guard to make sure the layer flushes its output then read the // output to create the flamegraph drop(guard); - make_flamegraph(tmp_dir.path()); + make_flamegraph(tmp_dir.path(), out.as_ref()); }