It is not uncommon that users who are new to tracing look at the examples in the `master` branch of the repository and find that they don't compile. This is because they are examples which compile with the code from the master branch, which is for the as yet unreleased tracing 0.2.0 ecosystem. Users should instead go to the `v0.1.x` branch to find examples compatible with the crates published on crates.io. This change adds a doc-comment to the beginning of every example file informing the user of this fact and suggesting that they check out the `v0.1.x` branch instead.
72 lines
2.3 KiB
Rust
72 lines
2.3 KiB
Rust
//! NOTE: This is pre-release documentation for the upcoming tracing 0.2.0 ecosystem. For the
|
|
//! release examples, please see the `v0.1.x` branch instead.
|
|
use std::{
|
|
env,
|
|
fs::File,
|
|
io::{BufReader, BufWriter},
|
|
path::{Path, PathBuf},
|
|
thread::sleep,
|
|
time::Duration,
|
|
};
|
|
use tracing::{span, Level};
|
|
use tracing_flame::FlameSubscriber;
|
|
use tracing_subscriber::{prelude::*, registry::Registry};
|
|
|
|
static PATH: &str = "flame.folded";
|
|
|
|
fn setup_global_collector(dir: &Path) -> impl Drop {
|
|
let (flame_layer, _guard) = FlameSubscriber::with_file(dir.join(PATH)).unwrap();
|
|
|
|
let collector = Registry::default().with(flame_layer);
|
|
|
|
tracing::collect::set_global_default(collector).unwrap();
|
|
|
|
_guard
|
|
}
|
|
|
|
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(out).unwrap();
|
|
let writer = BufWriter::new(out);
|
|
|
|
let mut opts = inferno::flamegraph::Options::default();
|
|
inferno::flamegraph::from_reader(&mut opts, reader, writer).unwrap();
|
|
}
|
|
|
|
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 = tempfile::Builder::new()
|
|
.prefix("flamegraphs")
|
|
.tempdir()
|
|
.expect("failed to create temporary directory");
|
|
let guard = setup_global_collector(tmp_dir.path());
|
|
|
|
// do a bunch of span entering and exiting to simulate a program running
|
|
span!(Level::ERROR, "outer").in_scope(|| {
|
|
sleep(Duration::from_millis(10));
|
|
span!(Level::ERROR, "Inner").in_scope(|| {
|
|
sleep(Duration::from_millis(50));
|
|
span!(Level::ERROR, "Innermost").in_scope(|| {
|
|
sleep(Duration::from_millis(50));
|
|
});
|
|
});
|
|
sleep(Duration::from_millis(5));
|
|
});
|
|
sleep(Duration::from_millis(500));
|
|
|
|
// 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(), out.as_ref());
|
|
}
|