From 27b28773513696220d7dca0d800fe25da67b0f68 Mon Sep 17 00:00:00 2001 From: Xiaopeng Li Date: Tue, 15 Dec 2020 02:56:52 +0800 Subject: [PATCH] tracing-flame: add module_path and file_and_line configs (#1134) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit We should allow configuring whether or not to display module_path or file/line in output. Co-authored-by: 李小鹏 Co-authored-by: Jane Lusby Co-authored-by: Eliza Weisman --- tracing-flame/src/lib.rs | 46 ++++++++++++++++++++++++++++++---------- 1 file changed, 35 insertions(+), 11 deletions(-) diff --git a/tracing-flame/src/lib.rs b/tracing-flame/src/lib.rs index 7ee91ba4f1..ae11da165b 100644 --- a/tracing-flame/src/lib.rs +++ b/tracing-flame/src/lib.rs @@ -223,6 +223,12 @@ struct Config { /// Don't include thread_id threads_collapsed: bool, + + /// Don't display module_path + module_path: bool, + + /// Don't display file and line + file_and_line: bool, } impl Default for Config { @@ -230,6 +236,8 @@ impl Default for Config { Self { empty_samples: true, threads_collapsed: false, + module_path: true, + file_and_line: true, } } } @@ -307,6 +315,18 @@ where self.config.threads_collapsed = enabled; self } + + /// Configures whether or not module paths should be included in the output. + pub fn with_module_path(mut self, enabled: bool) -> Self { + self.config.module_path = enabled; + self + } + + /// Configures whether or not file and line should be included in the output. + pub fn with_file_and_line(mut self, enabled: bool) -> Self { + self.config.file_and_line = enabled; + self + } } impl FlushGuard @@ -390,7 +410,7 @@ where for parent in parents { stack += "; "; - write(&mut stack, parent).expect("expected: write to String never fails"); + write(&mut stack, parent, &self.config).expect("expected: write to String never fails"); } write!(&mut stack, " {}", samples.as_nanos()) @@ -432,14 +452,14 @@ where for parent in parents { expect!( - write(&mut stack, parent), + write(&mut stack, parent, &self.config), "expected: write to String never fails" ); stack += "; "; } expect!( - write(&mut stack, first), + write(&mut stack, first, &self.config), "expected: write to String never fails" ); expect!( @@ -469,22 +489,26 @@ where } } -fn write(dest: &mut String, span: SpanRef<'_, C>) -> fmt::Result +fn write(dest: &mut String, span: SpanRef<'_, C>, config: &Config) -> fmt::Result where C: Collect + for<'span> LookupSpan<'span>, { - if let Some(module_path) = span.metadata().module_path() { - write!(dest, "{}::", module_path)?; + if config.module_path { + if let Some(module_path) = span.metadata().module_path() { + write!(dest, "{}::", module_path)?; + } } write!(dest, "{}", span.name())?; - if let Some(file) = span.metadata().file() { - write!(dest, ":{}", file)?; - } + if config.file_and_line { + if let Some(file) = span.metadata().file() { + write!(dest, ":{}", file)?; + } - if let Some(line) = span.metadata().line() { - write!(dest, ":{}", line)?; + if let Some(line) = span.metadata().line() { + write!(dest, ":{}", line)?; + } } Ok(())