Skip to content

Commit 27b2877

Browse files
xiaopengli89yaahchawkw
authored
tracing-flame: add module_path and file_and_line configs (#1134)
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>
1 parent 4f93a6e commit 27b2877

File tree

1 file changed

+35
-11
lines changed

1 file changed

+35
-11
lines changed

tracing-flame/src/lib.rs

Lines changed: 35 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -223,13 +223,21 @@ struct Config {
223223

224224
/// Don't include thread_id
225225
threads_collapsed: bool,
226+
227+
/// Don't display module_path
228+
module_path: bool,
229+
230+
/// Don't display file and line
231+
file_and_line: bool,
226232
}
227233

228234
impl Default for Config {
229235
fn default() -> Self {
230236
Self {
231237
empty_samples: true,
232238
threads_collapsed: false,
239+
module_path: true,
240+
file_and_line: true,
233241
}
234242
}
235243
}
@@ -307,6 +315,18 @@ where
307315
self.config.threads_collapsed = enabled;
308316
self
309317
}
318+
319+
/// Configures whether or not module paths should be included in the output.
320+
pub fn with_module_path(mut self, enabled: bool) -> Self {
321+
self.config.module_path = enabled;
322+
self
323+
}
324+
325+
/// Configures whether or not file and line should be included in the output.
326+
pub fn with_file_and_line(mut self, enabled: bool) -> Self {
327+
self.config.file_and_line = enabled;
328+
self
329+
}
310330
}
311331

312332
impl<W> FlushGuard<W>
@@ -390,7 +410,7 @@ where
390410

391411
for parent in parents {
392412
stack += "; ";
393-
write(&mut stack, parent).expect("expected: write to String never fails");
413+
write(&mut stack, parent, &self.config).expect("expected: write to String never fails");
394414
}
395415

396416
write!(&mut stack, " {}", samples.as_nanos())
@@ -432,14 +452,14 @@ where
432452

433453
for parent in parents {
434454
expect!(
435-
write(&mut stack, parent),
455+
write(&mut stack, parent, &self.config),
436456
"expected: write to String never fails"
437457
);
438458
stack += "; ";
439459
}
440460

441461
expect!(
442-
write(&mut stack, first),
462+
write(&mut stack, first, &self.config),
443463
"expected: write to String never fails"
444464
);
445465
expect!(
@@ -469,22 +489,26 @@ where
469489
}
470490
}
471491

472-
fn write<C>(dest: &mut String, span: SpanRef<'_, C>) -> fmt::Result
492+
fn write<C>(dest: &mut String, span: SpanRef<'_, C>, config: &Config) -> fmt::Result
473493
where
474494
C: Collect + for<'span> LookupSpan<'span>,
475495
{
476-
if let Some(module_path) = span.metadata().module_path() {
477-
write!(dest, "{}::", module_path)?;
496+
if config.module_path {
497+
if let Some(module_path) = span.metadata().module_path() {
498+
write!(dest, "{}::", module_path)?;
499+
}
478500
}
479501

480502
write!(dest, "{}", span.name())?;
481503

482-
if let Some(file) = span.metadata().file() {
483-
write!(dest, ":{}", file)?;
484-
}
504+
if config.file_and_line {
505+
if let Some(file) = span.metadata().file() {
506+
write!(dest, ":{}", file)?;
507+
}
485508

486-
if let Some(line) = span.metadata().line() {
487-
write!(dest, ":{}", line)?;
509+
if let Some(line) = span.metadata().line() {
510+
write!(dest, ":{}", line)?;
511+
}
488512
}
489513

490514
Ok(())

0 commit comments

Comments
 (0)