Skip to content

Commit

Permalink
fix: remove unnecessary Debug bounds; fix panic (#46)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
davidbarsky committed May 26, 2022
1 parent c577326 commit 8f0e5ee
Showing 1 changed file with 7 additions and 5 deletions.
12 changes: 7 additions & 5 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@ impl Visit for Data {
self.kvs.push((field.name(), format!("{:?}", value)))
}
}

#[derive(Debug)]
pub struct HierarchicalLayer<W = fn() -> io::Stderr>
where
Expand Down Expand Up @@ -209,7 +208,7 @@ where

fn write_span_info<S>(&self, id: &Id, ctx: &Context<S>, style: SpanMode)
where
S: Subscriber + for<'span> LookupSpan<'span> + fmt::Debug,
S: Subscriber + for<'span> LookupSpan<'span>,
{
let span = ctx
.span(id)
Expand Down Expand Up @@ -277,13 +276,16 @@ where

impl<S, W> Layer<S> for HierarchicalLayer<W>
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<S>) {
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::<Data>().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);
Expand Down

0 comments on commit 8f0e5ee

Please sign in to comment.