Skip to content

Commit

Permalink
feat(rome_cli): add tracing metrics collection and printing (rome#2234)
Browse files Browse the repository at this point in the history
  • Loading branch information
leops authored Mar 18, 2022
1 parent 07b18c9 commit 8f8517c
Show file tree
Hide file tree
Showing 11 changed files with 433 additions and 5 deletions.
23 changes: 23 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions crates/rome_cli/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,8 @@ rome_path = { path = "../rome_path", version = "0.0.0" }
pico-args = "0.4.2"
rayon = "1.5.1"
memmap2 = "0.3.0"
tracing = "0.1.31"
tracing-subscriber = "0.3.6"
parking_lot = "0.12.0"
lazy_static = "1.4.0"
hdrhistogram = { version = "7.5.0", default-features = false }
7 changes: 3 additions & 4 deletions crates/rome_cli/src/commands/format.rs
Original file line number Diff line number Diff line change
Expand Up @@ -365,6 +365,7 @@ struct FormatFileParams<'a> {
/// This function performs the actual formatting: it reads the file from disk
/// (or map it into memory it), parse and format it; then, it either writes the
/// result back or compares it with the original content and emit a diagnostic
#[tracing::instrument(level = "trace", skip_all, fields(path = ?params.path))]
fn format_file(params: FormatFileParams) -> Result<Vec<Diagnostic>, Diagnostic> {
if !params.app.can_format(&RomePath::new(params.path)) {
return Err(Diagnostic::error(
Expand All @@ -376,10 +377,8 @@ fn format_file(params: FormatFileParams) -> Result<Vec<Diagnostic>, Diagnostic>

let source_type = SourceType::try_from(params.path).unwrap_or_else(|_| SourceType::js_module());

let mut file = fs::File::options()
.read(true)
.write(true)
.open(params.path)
let mut file = tracing::debug_span!("open")
.in_scope(|| fs::File::options().read(true).write(true).open(params.path))
.with_file_id(params.file_id)?;

let input = FileBuffer::read(&mut file, !params.is_check).with_file_id(params.file_id)?;
Expand Down
13 changes: 12 additions & 1 deletion crates/rome_cli/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use pico_args::Arguments;
use rome_core::App;

mod commands;
mod metrics;

/// Global context for an execution of the CLI
pub struct CliSession {
Expand All @@ -22,13 +23,23 @@ impl CliSession {

/// Main function to run Rome CLI
pub fn run_cli(mut session: CliSession) {
let has_metrics = session.args.contains("--show-metrics");
if has_metrics {
crate::metrics::init_metrics();
}

let has_help = session.args.contains("--help");
let subcommand = session.args.subcommand();

match subcommand.as_ref().map(Option::as_deref) {
Ok(Some(cmd)) if has_help => crate::commands::help::help(Some(cmd)),

Ok(Some("format")) => crate::commands::format::format(session),
Ok(Some("format")) => {
crate::commands::format::format(session);
if has_metrics {
crate::metrics::print_metrics();
}
}

Ok(None | Some("help")) => crate::commands::help::help(None),

Expand Down
Loading

0 comments on commit 8f8517c

Please sign in to comment.