Skip to content

Commit

Permalink
tracing-flame: add module_path and file_and_line configs (#1134)
Browse files Browse the repository at this point in the history
We should allow configuring whether or not to display module_path
or file/line in output.

Co-authored-by: 李小鹏 <lixiaopeng.jetspark@bytedance.com>
Co-authored-by: Jane Lusby <jlusby42@gmail.com>
Co-authored-by: Eliza Weisman <eliza@buoyant.io>
  • Loading branch information
4 people committed Dec 14, 2020
1 parent 4ec5246 commit 4c7b309
Showing 1 changed file with 35 additions and 11 deletions.
46 changes: 35 additions & 11 deletions tracing-flame/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -227,13 +227,21 @@ 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 {
fn default() -> Self {
Self {
empty_samples: true,
threads_collapsed: false,
module_path: true,
file_and_line: true,
}
}
}
Expand Down Expand Up @@ -311,6 +319,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<W> FlushGuard<W>
Expand Down Expand Up @@ -394,7 +414,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())
Expand Down Expand Up @@ -436,14 +456,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!(
Expand Down Expand Up @@ -473,22 +493,26 @@ where
}
}

fn write<S>(dest: &mut String, span: SpanRef<'_, S>) -> fmt::Result
fn write<S>(dest: &mut String, span: SpanRef<'_, S>, config: &Config) -> fmt::Result
where
S: Subscriber + 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(())
Expand Down

0 comments on commit 4c7b309

Please sign in to comment.