Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 05b89f0

Browse files
committed
feat: support enabling tracing
1 parent 09a5943 commit 05b89f0

10 files changed

Lines changed: 86 additions & 40 deletions

File tree

.vscode/settings.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
"Finalizer",
2929
"hasher",
3030
"importee",
31+
"napi",
3132
"oxlint",
3233
"oxlintignore",
3334
"Renamer",

crates/rolldown/examples/basic.rs

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,23 +6,15 @@ use sugar_path::SugarPathBuf;
66
#[tokio::main]
77
async fn main() -> Result<()> {
88
let root = PathBuf::from(&std::env::var("CARGO_MANIFEST_DIR").unwrap());
9-
let repo_root = root.parent().unwrap().parent().unwrap();
109
let cwd = root.join("./examples").into_normalize();
1110
let mut bundler = Bundler::new(InputOptions {
12-
input: vec![InputItem {
13-
name: Some("basic".to_string()),
14-
import: repo_root
15-
.join("temp/three10x/entry.js")
16-
.into_normalize()
17-
.to_string_lossy()
18-
.to_string(),
19-
}],
11+
input: vec![InputItem { name: Some("basic".to_string()), import: "./index.js".to_string() }],
2012
cwd,
2113
..Default::default()
2214
});
2315

24-
let outputs = bundler.write(Default::default()).await.unwrap();
25-
println!("{outputs:#?}");
16+
let _outputs = bundler.write(Default::default()).await.unwrap();
17+
// println!("{outputs:#?}");
2618

2719
Ok(())
2820
}

crates/rolldown/src/bundler/bundler.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ impl Bundler<OsFileSystem> {
4040

4141
impl<T: FileSystem + Default + 'static> Bundler<T> {
4242
pub fn with_plugins_and_fs(input_options: InputOptions, plugins: Vec<BoxPlugin>, fs: T) -> Self {
43-
// rolldown_tracing::enable_tracing_on_demand();
43+
rolldown_tracing::try_init_tracing();
4444
Self {
4545
resolver: Resolver::with_cwd_and_fs(input_options.cwd.clone(), false, fs.share()).into(),
4646
plugin_driver: Arc::new(PluginDriver::new(plugins)),
@@ -107,6 +107,7 @@ impl<T: FileSystem + Default + 'static> Bundler<T> {
107107
build_ret
108108
}
109109

110+
#[tracing::instrument(skip_all)]
110111
async fn try_build(&mut self) -> BatchedResult<LinkStageOutput> {
111112
let build_info = ScanStage::new(
112113
Arc::clone(&self.input_options),
@@ -122,6 +123,7 @@ impl<T: FileSystem + Default + 'static> Bundler<T> {
122123
Ok(link_stage.link())
123124
}
124125

126+
#[tracing::instrument(skip_all)]
125127
async fn bundle_up(&mut self, output_options: OutputOptions) -> BuildResult<Vec<Output>> {
126128
tracing::trace!("InputOptions {:#?}", self.input_options);
127129
tracing::trace!("OutputOptions: {output_options:#?}",);

crates/rolldown/src/bundler/stages/bundle_stage.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ impl<'a> BundleStage<'a> {
3434
Self { link_output, output_options, input_options }
3535
}
3636

37+
#[tracing::instrument(skip_all)]
3738
pub fn bundle(&mut self) -> Vec<Output> {
3839
use rayon::prelude::*;
3940
let mut chunk_graph = self.generate_chunks();

crates/rolldown/src/bundler/stages/link_stage.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ impl LinkStage {
4242
}
4343
}
4444

45+
#[tracing::instrument(skip_all)]
4546
pub fn link(mut self) -> LinkStageOutput {
4647
self.sort_modules();
4748
Linker::new(&mut self).link();

crates/rolldown/src/bundler/stages/scan_stage.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ impl<Fs: FileSystem + Default + 'static> ScanStage<Fs> {
4646
Self { input_options, plugin_driver, fs, resolver }
4747
}
4848

49+
#[tracing::instrument(skip_all)]
4950
pub async fn scan(&self) -> BatchedResult<ScanStageOutput> {
5051
assert!(!self.input_options.input.is_empty(), "You must supply options.input to rolldown");
5152

crates/rolldown_binding/src/bundler.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use tracing::instrument;
66

77
use crate::{
88
options::InputOptions, options::OutputOptions, output::Outputs,
9-
utils::init_custom_trace_subscriber, NAPI_ENV,
9+
utils::try_init_custom_trace_subscriber, NAPI_ENV,
1010
};
1111

1212
#[napi]
@@ -18,7 +18,7 @@ pub struct Bundler {
1818
impl Bundler {
1919
#[napi(constructor)]
2020
pub fn new(env: Env, input_opts: InputOptions) -> napi::Result<Self> {
21-
init_custom_trace_subscriber(env);
21+
try_init_custom_trace_subscriber(env);
2222
Self::new_impl(env, input_opts)
2323
}
2424

Lines changed: 14 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,24 @@
11
pub mod napi_error_ext;
22
use napi::Env;
3-
use std::env;
4-
use std::sync::atomic::AtomicBool;
53
mod into_js_unknown_vec;
64
pub use into_js_unknown_vec::*;
75
mod js_callback;
86
pub use js_callback::*;
9-
use rolldown_tracing::enable_tracing_on_demand;
7+
use rolldown_tracing::try_init_tracing_with_chrome_layer;
108

11-
static IS_ENABLE_TRACING: AtomicBool = AtomicBool::new(false);
12-
13-
pub fn init_custom_trace_subscriber(mut napi_env: Env) {
14-
let value = env::var("TRACE").unwrap_or_default();
15-
if value == "1" && !IS_ENABLE_TRACING.swap(true, std::sync::atomic::Ordering::SeqCst) {
16-
let guard = enable_tracing_on_demand();
17-
if let Some(guard) = guard {
18-
napi_env
19-
.add_env_cleanup_hook(guard, |flush_guard| {
20-
flush_guard.flush();
21-
drop(flush_guard);
22-
})
23-
.expect("Should able to initialize cleanup for custom trace subscriber");
9+
pub fn try_init_custom_trace_subscriber(mut napi_env: Env) {
10+
match std::env::var("LOG_LAYER") {
11+
Ok(val) if val == "chrome" => {
12+
let guard = try_init_tracing_with_chrome_layer();
13+
if let Some(guard) = guard {
14+
napi_env
15+
.add_env_cleanup_hook(guard, |flush_guard| {
16+
flush_guard.flush();
17+
drop(flush_guard);
18+
})
19+
.expect("Should able to initialize cleanup for custom trace subscriber");
20+
}
2421
}
22+
_ => {}
2523
}
2624
}

crates/rolldown_tracing/Cargo.toml

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,12 @@ license.workspace = true
1010
edition.workspace = true
1111
repository.workspace = true
1212

13+
[lib]
14+
bench = false
15+
test = false
16+
doctest = false
17+
1318
[dependencies]
1419
tracing = { workspace = true }
15-
tracing-chrome = "0.7.0"
20+
tracing-chrome = "0.7.1"
1621
tracing-subscriber = { version = "0.3.16", features = ["env-filter"] }

crates/rolldown_tracing/src/lib.rs

Lines changed: 54 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,65 @@
1+
/// Some guidelines for tracing:
2+
/// - By default, only allow tracing events from crates of this repo.
3+
/// - Using `LOG_OUTPUT=chrome` to collect tracing events into a json file.
4+
/// - This only works on using `@rolldown/node`. If you are running rolldown in rust, this doesn't works.
5+
/// - Using `LOG=TRACE` to enable tracing or other values for more specific tracing.
6+
/// - See https://docs.rs/tracing-subscriber/latest/tracing_subscriber/filter/struct.EnvFilter.html#example-syntax for more syntax details.
17
use std::sync::atomic::AtomicBool;
28

3-
use tracing::{metadata::LevelFilter, Level};
9+
use tracing::{level_filters::LevelFilter, Level};
410
use tracing_chrome::FlushGuard;
11+
use tracing_subscriber::EnvFilter;
12+
use tracing_subscriber::{fmt, prelude::*};
513

6-
static IS_INITIALIZE: AtomicBool = AtomicBool::new(false);
14+
static IS_INITIALIZED: AtomicBool = AtomicBool::new(false);
715

8-
pub fn enable_tracing_on_demand() -> Option<FlushGuard> {
9-
use tracing_subscriber::{fmt, prelude::*, EnvFilter};
10-
if !IS_INITIALIZE.swap(true, std::sync::atomic::Ordering::SeqCst) {
16+
pub fn try_init_tracing() {
17+
if std::env::var("LOG").is_err() {
18+
// tracing will slow down the bundling process, so we only enable it when `LOG` is set.
19+
return;
20+
}
21+
if !IS_INITIALIZED.swap(true, std::sync::atomic::Ordering::SeqCst) {
1122
tracing_subscriber::registry()
12-
.with(fmt::layer())
1323
.with(
14-
tracing_subscriber::filter::Targets::new().with_targets(vec![("rolldown", Level::TRACE)]),
24+
tracing_subscriber::filter::Targets::new()
25+
.with_targets(vec![("rolldown", Level::TRACE), ("rolldown_binding", Level::TRACE)]),
26+
)
27+
.with(
28+
EnvFilter::builder()
29+
.with_env_var("LOG")
30+
.with_default_directive(LevelFilter::TRACE.into())
31+
.from_env_lossy(),
32+
)
33+
.with(fmt::layer().pretty().without_time())
34+
.init();
35+
tracing::trace!("Tracing is initialized.");
36+
}
37+
}
38+
39+
pub fn try_init_tracing_with_chrome_layer() -> Option<FlushGuard> {
40+
use tracing_chrome::ChromeLayerBuilder;
41+
use tracing_subscriber::prelude::*;
42+
if std::env::var("LOG").is_err() {
43+
// tracing will slow down the bundling process, so we only enable it when `LOG` is set.
44+
return None;
45+
}
46+
if IS_INITIALIZED.swap(true, std::sync::atomic::Ordering::SeqCst) {
47+
None
48+
} else {
49+
let (chrome_layer, guard) = ChromeLayerBuilder::new().build();
50+
tracing_subscriber::registry()
51+
.with(
52+
tracing_subscriber::filter::Targets::new()
53+
.with_targets(vec![("rolldown", Level::TRACE), ("rolldown_binding", Level::TRACE)]),
54+
)
55+
.with(
56+
EnvFilter::builder()
57+
.with_env_var("LOG")
58+
.with_default_directive(LevelFilter::TRACE.into())
59+
.from_env_lossy(),
1560
)
16-
.with(EnvFilter::builder().with_default_directive(LevelFilter::TRACE.into()).from_env_lossy())
61+
.with(chrome_layer)
1762
.init();
63+
Some(guard)
1864
}
19-
None
2065
}

0 commit comments

Comments
 (0)