Skip to content

Commit

Permalink
Add all named fields to the span name
Browse files Browse the repository at this point in the history
  • Loading branch information
superdump committed Nov 4, 2020
1 parent de16486 commit 8cdf8bf
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 5 deletions.
2 changes: 1 addition & 1 deletion tracing-tracy/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ Inspect tracing-enabled Rust applications with Tracy

[dependencies]
tracing-core = { version = "0.1", default-features = false, features = ["std"] }
tracing-subscriber = { version = "0.2", default-features = false, features = ["registry"] }
tracing-subscriber = { version = "0.2", default-features = false, features = ["fmt", "registry"] }
tracy-client = { path = "../tracy-client", version = "0.9.0", default-features = false }

[dev-dependencies]
Expand Down
17 changes: 13 additions & 4 deletions tracing-tracy/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,9 @@ use tracing_core::{
span::Id,
Event, Subscriber,
};
use tracing_subscriber::fmt::format::{DefaultFields, FormatFields};
use tracing_subscriber::{
fmt::FormattedFields,
layer::{Context, Layer},
registry,
};
Expand All @@ -53,7 +55,8 @@ thread_local! {

/// A tracing layer that collects data in Tracy profiling format.
#[derive(Clone)]
pub struct TracyLayer {
pub struct TracyLayer<F = DefaultFields> {
format: F,
stack_depth: u16,
}

Expand All @@ -62,7 +65,7 @@ impl TracyLayer {
///
/// Defaults to collecting stack traces.
pub fn new() -> Self {
Self { stack_depth: 64 }
Self { format: DefaultFields::new(), stack_depth: 64 }
}

/// Specify the maximum number of stack frames that will be collected.
Expand All @@ -80,18 +83,24 @@ impl Default for TracyLayer {
}
}

impl<S> Layer<S> for TracyLayer
impl<S, F> Layer<S> for TracyLayer<F>
where
S: Subscriber + for<'a> registry::LookupSpan<'a>,
F: for<'writer> FormatFields<'writer> + 'static,
{
fn on_enter(&self, id: &Id, ctx: Context<S>) {
if let Some(span_data) = ctx.span(id) {
let metadata = span_data.metadata();
let file = metadata.file().unwrap_or("<error: not available>");
let line = metadata.line().unwrap_or(0);
let name = if let Some(fields) = span_data.extensions().get::<FormattedFields<F>>() {
format!("{}: {}", metadata.name(), fields.fields.as_str())
} else {
metadata.name().to_string()
};
TRACY_SPAN_STACK.with(|s| {
s.borrow_mut().push_back((
Span::new(metadata.name(), "", file, line, self.stack_depth),
Span::new(&name, "", file, line, self.stack_depth),
id.into_u64()
));
});
Expand Down

0 comments on commit 8cdf8bf

Please sign in to comment.