Skip to content

Commit 8f0e5ee

Browse files
authored
fix: remove unnecessary Debug bounds; fix panic (#46)
Resolves tokio-rs/tracing#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 tokio-rs/tracing#2112.
1 parent c577326 commit 8f0e5ee

File tree

1 file changed

+7
-5
lines changed

1 file changed

+7
-5
lines changed

src/lib.rs

+7-5
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,6 @@ impl Visit for Data {
4242
self.kvs.push((field.name(), format!("{:?}", value)))
4343
}
4444
}
45-
4645
#[derive(Debug)]
4746
pub struct HierarchicalLayer<W = fn() -> io::Stderr>
4847
where
@@ -209,7 +208,7 @@ where
209208

210209
fn write_span_info<S>(&self, id: &Id, ctx: &Context<S>, style: SpanMode)
211210
where
212-
S: Subscriber + for<'span> LookupSpan<'span> + fmt::Debug,
211+
S: Subscriber + for<'span> LookupSpan<'span>,
213212
{
214213
let span = ctx
215214
.span(id)
@@ -277,13 +276,16 @@ where
277276

278277
impl<S, W> Layer<S> for HierarchicalLayer<W>
279278
where
280-
S: Subscriber + for<'span> LookupSpan<'span> + fmt::Debug,
279+
S: Subscriber + for<'span> LookupSpan<'span>,
281280
W: for<'writer> MakeWriter<'writer> + 'static,
282281
{
283282
fn on_new_span(&self, attrs: &Attributes, id: &Id, ctx: Context<S>) {
284-
let data = Data::new(attrs);
285283
let span = ctx.span(id).expect("in new_span but span does not exist");
286-
span.extensions_mut().insert(data);
284+
if span.extensions().get::<Data>().is_none() {
285+
let data = Data::new(attrs);
286+
span.extensions_mut().insert(data);
287+
}
288+
287289
if self.config.verbose_exit {
288290
if let Some(span) = span.parent() {
289291
self.write_span_info(&span.id(), &ctx, SpanMode::PreOpen);

0 commit comments

Comments
 (0)