mod flamegraph;
mod log;
pub use self::{flamegraph::FlamegraphLayer, log::LogLayer};
use crate::{util::Flamegrapher, Error};
use fern_logger::LoggerConfig;
use tracing::Metadata;
use std::path::Path;
pub fn flamegraph_layer<P: AsRef<Path>>(stack_filename: P) -> Result<(FlamegraphLayer, Flamegrapher), Error> {
#![allow(clippy::assertions_on_constants)]
assert!(
cfg!(tokio_unstable),
"task tracing requires building with RUSTFLAGS=\"--cfg tokio_unstable\"!"
);
FlamegraphLayer::new(stack_filename)
}
pub(crate) fn flamegraph_filter(meta: &Metadata<'_>) -> bool {
if meta.is_event() {
return false;
}
meta.name().starts_with("runtime") || meta.target().starts_with("tokio") || meta.target() == "trace_tools::observe"
}
pub fn log_layer(config: LoggerConfig) -> Result<LogLayer, Error> {
LogLayer::new(config)
}
pub(crate) fn log_filter(meta: &tracing::Metadata<'_>) -> bool {
meta.is_event()
}
#[cfg(feature = "tokio-console")]
pub fn console_layer() -> Result<console_subscriber::ConsoleLayer, Error> {
#![allow(clippy::assertions_on_constants)]
assert!(
cfg!(tokio_unstable),
"task tracing requires building with RUSTFLAGS=\"--cfg tokio_unstable\"!"
);
let (layer, server) = console_subscriber::ConsoleLayer::builder().with_default_env().build();
std::thread::Builder::new()
.name("console_subscriber".into())
.spawn(move || {
let runtime = tokio::runtime::Builder::new_current_thread()
.enable_io()
.enable_time()
.build()
.expect("console subscriber runtime initialization failed");
runtime.block_on(async move { server.serve().await.expect("console subscriber server failed") });
})
.expect("console subscriber could not spawn thread");
Ok(layer)
}
#[cfg(feature = "tokio-console")]
pub(crate) fn console_filter(meta: &tracing::Metadata<'_>) -> bool {
if meta.is_event() {
return meta.target().starts_with("runtime") || meta.target().starts_with("tokio");
}
meta.name().starts_with("runtime") || meta.target().starts_with("tokio")
}