From 5c29f156404002d6946fbc7a71d76a6c97d7b82a Mon Sep 17 00:00:00 2001 From: OJ Kwon Date: Fri, 25 Feb 2022 12:28:17 -0800 Subject: [PATCH] feat(plugin): Support tracing plugin execution (#3744) --- .gitignore | 4 +++- Cargo.lock | 1 + crates/node/src/util.rs | 6 ++++-- crates/swc/src/plugin.rs | 16 ++++++++++++++++ crates/swc_ecma_transforms_compat/Cargo.toml | 2 +- crates/swc_plugin_runner/Cargo.toml | 1 + crates/swc_plugin_runner/src/lib.rs | 3 +++ 7 files changed, 29 insertions(+), 4 deletions(-) diff --git a/.gitignore b/.gitignore index f1bbd2ebdf6..1d8998f466c 100644 --- a/.gitignore +++ b/.gitignore @@ -52,4 +52,6 @@ pkg/ *.tmp.js # Used to see output of babel. -/lab \ No newline at end of file +/lab + +*.mm_profdata \ No newline at end of file diff --git a/Cargo.lock b/Cargo.lock index 70f1b5a37e7..df70e56a8cf 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -3549,6 +3549,7 @@ dependencies = [ "swc_ecma_parser", "swc_ecma_visit", "testing", + "tracing", "wasmer", "wasmer-cache", "wasmer-wasi", diff --git a/crates/node/src/util.rs b/crates/node/src/util.rs index 6ed0333ec36..7c5aff477f8 100644 --- a/crates/node/src/util.rs +++ b/crates/node/src/util.rs @@ -34,9 +34,11 @@ pub fn init_trace_once( FLUSH_GUARD.get_or_init(|| { if enable_chrome_trace { let layer = if let Some(trace_out_file) = trace_out_file { - ChromeLayerBuilder::new().file(trace_out_file) - } else { ChromeLayerBuilder::new() + .file(trace_out_file) + .include_args(true) + } else { + ChromeLayerBuilder::new().include_args(true) }; let (chrome_layer, guard) = layer.build(); diff --git a/crates/swc/src/plugin.rs b/crates/swc/src/plugin.rs index 718bc0930a2..08a39b73d4d 100644 --- a/crates/swc/src/plugin.rs +++ b/crates/swc/src/plugin.rs @@ -68,6 +68,7 @@ struct RustPlugins { } impl RustPlugins { + #[tracing::instrument(level = "trace", skip_all, name = "apply_plugins")] #[cfg(feature = "plugin")] fn apply(&mut self, n: Program) -> Result { use std::{path::PathBuf, sync::Arc}; @@ -86,6 +87,13 @@ impl RustPlugins { // transform. if let Some(plugins) = &self.plugins { for p in plugins { + let span = tracing::span!( + tracing::Level::TRACE, + "serialize_context", + plugin_module = p.0.as_str() + ); + let context_span_guard = span.enter(); + let config_json = serde_json::to_string(&p.1) .context("Failed to serialize plugin config as json") .and_then(|value| Serialized::serialize(&value))?; @@ -103,7 +111,14 @@ impl RustPlugins { } else { anyhow::bail!("Failed to resolve plugin path: {:?}", resolved_path); }; + drop(context_span_guard); + let span = tracing::span!( + tracing::Level::TRACE, + "execute_plugin_runner", + plugin_module = p.0.as_str() + ); + let transform_span_guard = span.enter(); serialized = swc_plugin_runner::apply_js_plugin( &p.0, &path, @@ -112,6 +127,7 @@ impl RustPlugins { config_json, context_json, )?; + drop(transform_span_guard); } } diff --git a/crates/swc_ecma_transforms_compat/Cargo.toml b/crates/swc_ecma_transforms_compat/Cargo.toml index 16e7b937279..a2c68e15a7c 100644 --- a/crates/swc_ecma_transforms_compat/Cargo.toml +++ b/crates/swc_ecma_transforms_compat/Cargo.toml @@ -34,7 +34,7 @@ swc_ecma_transforms_classes = {version = "0.50.0", path = "../swc_ecma_transform swc_ecma_transforms_macros = {version = "0.3.0", path = "../swc_ecma_transforms_macros"} swc_ecma_utils = {version = "0.68.0", path = "../swc_ecma_utils"} swc_ecma_visit = {version = "0.54.0", path = "../swc_ecma_visit"} -swc_trace_macro = {versio = "0.1.0", path = "../swc_trace_macro"} +swc_trace_macro = {version = "0.1.0", path = "../swc_trace_macro"} tracing = "0.1.31" [dev-dependencies] diff --git a/crates/swc_plugin_runner/Cargo.toml b/crates/swc_plugin_runner/Cargo.toml index 7315a7ca814..50763b2c6d5 100644 --- a/crates/swc_plugin_runner/Cargo.toml +++ b/crates/swc_plugin_runner/Cargo.toml @@ -16,6 +16,7 @@ serde = {version = "1.0.126", features = ["derive"]} serde_json = "1.0.64" swc_common = {version = "0.17.0", path = "../swc_common", features = ["plugin-rt", "concurrent"]} swc_ecma_ast = {version = "0.68.0", path = "../swc_ecma_ast", features = ["rkyv-impl"]} +tracing = "0.1.31" wasmer = "2.1.1" wasmer-cache = "2.1.1" wasmer-wasi = "2.1.1" diff --git a/crates/swc_plugin_runner/src/lib.rs b/crates/swc_plugin_runner/src/lib.rs index 0f8426b007c..6d52d04b1f3 100644 --- a/crates/swc_plugin_runner/src/lib.rs +++ b/crates/swc_plugin_runner/src/lib.rs @@ -195,6 +195,7 @@ struct HostEnvironment { transform_result: Arc>>, } +#[tracing::instrument(level = "trace", skip_all)] fn load_plugin( plugin_path: &Path, cache: &Lazy, @@ -347,6 +348,7 @@ struct PluginTransformTracker { } impl PluginTransformTracker { + #[tracing::instrument(level = "trace", skip(cache))] fn new(path: &Path, cache: &Lazy) -> Result { let (instance, transform_result) = load_plugin(path, cache)?; @@ -414,6 +416,7 @@ impl PluginTransformTracker { } } + #[tracing::instrument(level = "trace", skip_all)] fn transform( &mut self, program: &Serialized,