Skip to content

[tracing-subscriber][tracing] improve duplciate tracing situation a bit #3319

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions tracing-subscriber/src/fmt/fmt_layer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -797,6 +797,7 @@ impl<E: ?Sized> fmt::Display for FormattedFields<E> {
}
}

// TODO: Evaluate removing Deref in future versions, as its a powerful tool and barely useful (consumers can just call `.fields` directly)
impl<E: ?Sized> Deref for FormattedFields<E> {
type Target = String;
fn deref(&self) -> &Self::Target {
Expand Down Expand Up @@ -1631,8 +1632,7 @@ mod test {
.with_timer(MockTime)
.with_span_events(FmtSpan::ACTIVE);

let (reloadable_layer, reload_handle) =
crate::reload::Layer::new(inner_layer);
let (reloadable_layer, reload_handle) = crate::reload::Layer::new(inner_layer);
let reload = reloadable_layer.with_subscriber(Registry::default());

with_default(reload, || {
Expand Down
4 changes: 2 additions & 2 deletions tracing-subscriber/src/fmt/format/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -969,7 +969,7 @@ where

let ext = span.extensions();
if let Some(fields) = &ext.get::<FormattedFields<N>>() {
if !fields.is_empty() {
if !fields.fields.is_empty() {
write!(writer, "{}{}{}", bold.paint("{"), fields, bold.paint("}"))?;
}
}
Expand Down Expand Up @@ -1146,7 +1146,7 @@ where
{
let exts = span.extensions();
if let Some(fields) = exts.get::<FormattedFields<N>>() {
if !fields.is_empty() {
if !fields.fields.is_empty() {
write!(writer, " {}", dimmed.paint(&fields.fields))?;
}
}
Expand Down
4 changes: 2 additions & 2 deletions tracing-subscriber/src/fmt/format/pretty.rs
Original file line number Diff line number Diff line change
Expand Up @@ -324,7 +324,7 @@ where
let fields = &ext
.get::<FormattedFields<N>>()
.expect("Unable to find FormattedFields in extensions; this is a bug");
if !fields.is_empty() {
if !fields.fields.is_empty() {
write!(writer, " {} {}", dimmed.paint("with"), fields)?;
}
writer.write_char('\n')?;
Expand All @@ -346,7 +346,7 @@ impl<'writer> FormatFields<'writer> for Pretty {
current: &'writer mut FormattedFields<Self>,
fields: &span::Record<'_>,
) -> fmt::Result {
let empty = current.is_empty();
let empty = current.fields.is_empty();
let writer = current.as_writer();
let mut v = PrettyVisitor::new(writer, empty);
fields.record(&mut v);
Expand Down
5 changes: 5 additions & 0 deletions tracing/src/span.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1187,6 +1187,11 @@ impl Span {
/// **Note**: To record several values in just one call, see the [`record_all!`](crate::record_all!) macro.
/// </pre></div>
///
/// <pre class="ignore" style="white-space:normal;font:inherit;">
/// <strong>Note</strong>: <a href="https://docs.rs/tracing-subscriber/latest/tracing_subscriber/fmt/struct.Subscriber.html"><code>FmtSubscriber</code></a> has a bug
/// where it might duplicates span fields because of its implementation. See details at: <a href="https://github.com/tokio-rs/tracing/issues/1372">Github</a>
/// </pre>
///
/// [`field::Empty`]: super::field::Empty
/// [`Metadata`]: super::Metadata
pub fn record<Q: field::AsField + ?Sized, V: field::Value>(
Expand Down