Skip to content

Commit

Permalink
Migrate tracing-subscriber to SpanRef::scope
Browse files Browse the repository at this point in the history
  • Loading branch information
nightkr committed Jun 9, 2021
1 parent cd8148f commit 84afb38
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 24 deletions.
12 changes: 8 additions & 4 deletions tracing-subscriber/src/fmt/fmt_layer.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use crate::{
field::RecordFields,
fmt::{format, FormatEvent, FormatFields, MakeWriter, TestWriter},
layer::{self, Context, Scope},
layer::{self, Context},
registry::{LookupSpan, SpanRef},
};
use format::{FmtSpan, TimingDisplay};
Expand Down Expand Up @@ -804,8 +804,10 @@ where
F: FnMut(&SpanRef<'_, S>) -> Result<(), E>,
{
// visit all the current spans
for span in self.ctx.scope() {
f(&span)?;
if let Some(leaf) = self.ctx.lookup_current() {
for span in leaf.scope().from_root() {
f(&span)?;
}
}
Ok(())
}
Expand Down Expand Up @@ -864,7 +866,9 @@ where
/// the current span.
///
/// [stored data]: ../registry/struct.SpanRef.html
pub fn scope(&self) -> Scope<'_, S>
#[deprecated(note = "wraps crate::layer::Context::scope")]
#[allow(deprecated)]
pub fn scope(&self) -> crate::layer::Scope<'_, S>
where
S: for<'lookup> LookupSpan<'lookup>,
{
Expand Down
6 changes: 4 additions & 2 deletions tracing-subscriber/src/fmt/format/json.rs
Original file line number Diff line number Diff line change
Expand Up @@ -98,8 +98,10 @@ where
use serde::ser::SerializeSeq;
let mut serializer = serializer_o.serialize_seq(None)?;

for span in self.0.scope() {
serializer.serialize_element(&SerializableSpan(&span, self.1))?;
if let Some(leaf_span) = self.0.lookup_current() {
for span in leaf_span.scope().from_root() {
serializer.serialize_element(&SerializableSpan(&span, self.1))?;
}
}

serializer.end()
Expand Down
13 changes: 3 additions & 10 deletions tracing-subscriber/src/fmt/format/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,7 @@ use crate::{
registry::LookupSpan,
};

use std::{
fmt::{self, Write},
iter,
};
use std::fmt::{self, Write};
use tracing_core::{
field::{self, Field, Visit},
span, Event, Level, Subscriber,
Expand Down Expand Up @@ -861,9 +858,7 @@ where
.and_then(|id| self.ctx.ctx.span(&id))
.or_else(|| self.ctx.ctx.lookup_current());

let scope = span
.into_iter()
.flat_map(|span| span.from_root().chain(iter::once(span)));
let scope = span.into_iter().flat_map(|span| span.scope().from_root());

for span in scope {
seen = true;
Expand Down Expand Up @@ -933,9 +928,7 @@ where
.and_then(|id| self.ctx.ctx.span(&id))
.or_else(|| self.ctx.ctx.lookup_current());

let scope = span
.into_iter()
.flat_map(|span| span.from_root().chain(iter::once(span)));
let scope = span.into_iter().flat_map(|span| span.scope().from_root());

for span in scope {
write!(f, "{}", bold.paint(span.metadata().name()))?;
Expand Down
10 changes: 2 additions & 8 deletions tracing-subscriber/src/fmt/format/pretty.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,7 @@ use crate::{
registry::LookupSpan,
};

use std::{
fmt::{self, Write},
iter,
};
use std::fmt::{self, Write};
use tracing_core::{
field::{self, Field},
Event, Level, Subscriber,
Expand Down Expand Up @@ -187,10 +184,7 @@ where
.and_then(|id| ctx.span(&id))
.or_else(|| ctx.lookup_current());

let scope = span.into_iter().flat_map(|span| {
let parents = span.parents();
iter::once(span).chain(parents)
});
let scope = span.into_iter().flat_map(|span| span.scope());

for span in scope {
let meta = span.metadata();
Expand Down

0 comments on commit 84afb38

Please sign in to comment.