From 8f0e5ee2d1257de878dbd981354aad48e6e97ac1 Mon Sep 17 00:00:00 2001 From: David Barsky Date: Thu, 26 May 2022 17:40:31 -0400 Subject: [PATCH] fix: remove unnecessary `Debug` bounds; fix panic (#46) Resolves https://github.com/tokio-rs/tracing/issues/2112; #45. This PR does two things: - removes unnecessary `fmt::Debug` bounds on `HierarchicalLayer`, which makes it possible to compose `HierarchicalLayer` with other `HierarchicalLayer`s (#45). - Checks whether another `HierarchicalLayer` has already placed `Data` in the extensions; skipping if it's already present. This prevents the panic reported in https://github.com/tokio-rs/tracing/issues/2112. --- src/lib.rs | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 9bb8858..dbacaa5 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -42,7 +42,6 @@ impl Visit for Data { self.kvs.push((field.name(), format!("{:?}", value))) } } - #[derive(Debug)] pub struct HierarchicalLayer io::Stderr> where @@ -209,7 +208,7 @@ where fn write_span_info(&self, id: &Id, ctx: &Context, style: SpanMode) where - S: Subscriber + for<'span> LookupSpan<'span> + fmt::Debug, + S: Subscriber + for<'span> LookupSpan<'span>, { let span = ctx .span(id) @@ -277,13 +276,16 @@ where impl Layer for HierarchicalLayer where - S: Subscriber + for<'span> LookupSpan<'span> + fmt::Debug, + S: Subscriber + for<'span> LookupSpan<'span>, W: for<'writer> MakeWriter<'writer> + 'static, { fn on_new_span(&self, attrs: &Attributes, id: &Id, ctx: Context) { - let data = Data::new(attrs); let span = ctx.span(id).expect("in new_span but span does not exist"); - span.extensions_mut().insert(data); + if span.extensions().get::().is_none() { + let data = Data::new(attrs); + span.extensions_mut().insert(data); + } + if self.config.verbose_exit { if let Some(span) = span.parent() { self.write_span_info(&span.id(), &ctx, SpanMode::PreOpen);