Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: fine grained tracing #3002

Merged
merged 6 commits into from
May 4, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 38 additions & 5 deletions crates/bench/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,24 +1,49 @@
mod termcolorful;
use std::str::FromStr;
use std::{path::PathBuf, time::Instant};

use mimalloc_rust::GlobalMiMalloc;
use rspack_core::Compiler;
use rspack_fs::AsyncNativeFileSystem;
use rspack_testing::apply_from_fixture;
#[cfg(feature = "tracing")]
use rspack_tracing::enable_tracing_by_env_with_chrome_layer;
use rspack_tracing::{enable_tracing_by_env, enable_tracing_by_env_with_chrome_layer};
use termcolorful::println_string_with_fg_color;

#[cfg(all(not(all(target_os = "linux", target_arch = "aarch64", target_env = "musl"))))]
#[global_allocator]
static GLOBAL: GlobalMiMalloc = GlobalMiMalloc;

#[derive(Default, Clone, Copy)]
enum Layer {
#[default]
Logger,
Chrome,
}

impl FromStr for Layer {
type Err = ();

fn from_str(s: &str) -> Result<Self, Self::Err> {
Ok(match s {
"chrome" => Self::Chrome,
"logger" => Self::Logger,
_ => unimplemented!("Unknown layer {s}, please use one of `chrome`, `logger` "),
})
}
}

#[tokio::main]
async fn main() {
#[cfg(feature = "tracing")]
let layer = std::env::var("layer")
.ok()
.and_then(|var| Layer::from_str(&var).ok())
.unwrap_or_default();
let path_list = vec![
// "examples/cjs-tree-shaking-basic",
// "examples/basic",
"smoke-examples/basic",
"smoke-example/basic",
// "examples/export-star-chain",
// "examples/bbb",
/* "examples/named-export-decl-with-src-eval",
Expand All @@ -27,13 +52,21 @@ async fn main() {
];
for p in path_list {
println_string_with_fg_color(p, termcolorful::Color::Red);
run(p).await;
run(
p,
#[cfg(feature = "tracing")]
layer,
)
.await;
}
}

async fn run(relative_path: &str) {
async fn run(relative_path: &str, #[cfg(feature = "tracing")] layer: Layer) {
#[cfg(feature = "tracing")]
let guard = enable_tracing_by_env_with_chrome_layer();
let guard = match layer {
Layer::Logger => enable_tracing_by_env(),
Layer::Chrome => enable_tracing_by_env_with_chrome_layer(),
};
let manifest_dir = PathBuf::from(env!("CARGO_WORKSPACE_DIR"));
// let bundle_dir = manifest_dir.join("tests/fixtures/postcss/pxtorem");
let bundle_dir: PathBuf = manifest_dir.join(relative_path);
Expand Down
82 changes: 49 additions & 33 deletions crates/rspack_tracing/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
use std::str::FromStr;
use std::sync::atomic::AtomicBool;

use tracing::Level;
use tracing_chrome::FlushGuard;
use tracing_subscriber::{fmt::format::FmtSpan, layer::Filter};
use tracing_subscriber::{EnvFilter, Layer};
// use tracing_chrome::FlushGuard;

static IS_TRACING_ENABLED: AtomicBool = AtomicBool::new(false);
Expand All @@ -17,62 +19,76 @@ impl<S> Filter<S> for FilterEvent {
!meta.is_event()
}
}
pub fn enable_tracing_by_env() {
let is_enable_tracing = std::env::var("TRACE").map_or(false, |x| {
matches!(x.as_str(), "TRACE" | "DEBUG" | "INFO" | "WARN" | "ERROR")
});
pub fn enable_tracing_by_env() -> Option<FlushGuard> {
let trace_var = std::env::var("TRACE").ok();
let is_enable_tracing = trace_var.is_some();
if is_enable_tracing && !IS_TRACING_ENABLED.swap(true, std::sync::atomic::Ordering::SeqCst) {
use tracing_subscriber::{fmt, prelude::*, EnvFilter};
use tracing_subscriber::{fmt, prelude::*};
let layers = generate_common_layers(trace_var);

tracing_subscriber::registry()
// .with(EnvFilter::from_env("TRACE").and_then(rspack_only_layer))
.with(layers)
.with(
fmt::layer()
.pretty()
.with_file(true)
// To keep track of the closing point of spans
.with_span_events(FmtSpan::CLOSE),
)
.with(
tracing_subscriber::filter::Targets::new().with_targets(vec![
("rspack_core", Level::TRACE),
("rspack", Level::TRACE),
("rspack_node", Level::TRACE),
("rspack_plugin_javascript", Level::TRACE),
("rspack_plugin_split_chunks", Level::TRACE),
("rspack_binding_options", Level::TRACE),
]),
)
// Using TRACE=[TRACE|DEBUG|INFO|WARN|ERROR] to set max trace level.
.with(EnvFilter::from_env("TRACE"))
.init();
tracing::trace!("enable_tracing_by_env");
}
None
}

fn generate_common_layers(
trace_var: Option<String>,
) -> Vec<Box<dyn Layer<tracing_subscriber::Registry> + Send + Sync>> {
let default_level = trace_var.as_ref().and_then(|var| Level::from_str(var).ok());

let mut layers = vec![];
if let Some(default_level) = default_level {
layers.push(
tracing_subscriber::filter::Targets::new()
.with_targets(vec![
("rspack_core", default_level),
("rspack", default_level),
("rspack_node", default_level),
("rspack_plugin_javascript", default_level),
("rspack_plugin_split_chunks", default_level),
("rspack_binding_options", default_level),
])
.boxed(),
);
} else {
// SAFETY: we know that trace_var is `Ok(StrinG)` now,
// for the second unwrap, if we can't parse the directive, then the tracing result would be
// unexpected, then panic is reasonable
let env_layer = EnvFilter::builder()
.with_regex(true)
.parse(trace_var.expect("Should not be empty"))
.expect("Parse tracing directive syntax failed,for details about the directive syntax you could refer https://docs.rs/tracing-subscriber/latest/tracing_subscriber/filter/struct.EnvFilter.html#directives");

layers.push(env_layer.boxed());
}
layers
}

pub fn enable_tracing_by_env_with_chrome_layer() -> Option<FlushGuard> {
let is_enable_tracing = std::env::var("TRACE").map_or(false, |x| {
matches!(x.as_str(), "TRACE" | "DEBUG" | "INFO" | "WARN" | "ERROR")
});
let trace_var = std::env::var("TRACE").ok();
let is_enable_tracing = trace_var.is_some();
if is_enable_tracing && !IS_TRACING_ENABLED.swap(true, std::sync::atomic::Ordering::SeqCst) {
use tracing_chrome::ChromeLayerBuilder;
use tracing_subscriber::{prelude::*, EnvFilter};
use tracing_subscriber::prelude::*;

let (chrome_layer, guard) = ChromeLayerBuilder::new().include_args(true).build();
let layers = generate_common_layers(trace_var);
// If we don't do this. chrome_layer will collect nothing.
// std::mem::forget(guard);
tracing_subscriber::registry()
.with(layers)
.with(chrome_layer.with_filter(FilterEvent {}))
.with(
tracing_subscriber::filter::Targets::new().with_targets(vec![
("rspack_core", Level::TRACE),
("rspack", Level::TRACE),
("rspack_node", Level::TRACE),
("rspack_plugin_javascript", Level::TRACE),
("rspack_plugin_split_chunks", Level::TRACE),
("rspack_binding_options", Level::TRACE),
]),
)
// Using TRACE=[TRACE|DEBUG|INFO|WARN|ERROR] to set max trace level.
.with(EnvFilter::from_env("TRACE"))
.init();
Some(guard)
} else {
Expand Down