Skip to content

Commit 7a01260

Browse files
authored
subscriber: unify span traversal (#1431)
## Motivation Fixes #1429 ## Solution Implemented as described in #1429, and migrated all internal uses of the deprecated methods. All tests passed both before and after the migration (9ec8130). - Add a new method `SpanRef::scope(&self)` that returns a leaf-to-root `Iterator`, including the specified leaf - Add a new method `Scope::from_root(self)` (where `Scope` is the type returned by `SpanRef::scope` defined earlier) that returns a root-to-leaf `Iterator` that ends at the current leaf (in other words: it's essentially the same as `Scope::collect::<Vec<_>>().into_iter().rev()`) - Deprecate all existing iterators, since they can be replaced by the new unified mechanism: - `Span::parents` is equivalent to `Span::scope().skip(1)` (although the `skip` is typically a bug) - `Span::from_root` is equivalent to `Span::scope().skip(1).from_root()` (although the `skip` is typically a bug) - `Context::scope` is equivalent to `Context::lookup_current().scope().from_root()` (although the `lookup_current` is sometimes a bug, see also #1428)
1 parent f910a2a commit 7a01260

File tree

17 files changed

+414
-157
lines changed

17 files changed

+414
-157
lines changed

examples/examples/sloggish/sloggish_subscriber.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,6 @@ impl<'a> Visit for Event<'a> {
133133
Style::new().bold().paint(format!("{:?}", value))
134134
)
135135
.unwrap();
136-
self.comma = true;
137136
} else {
138137
write!(
139138
&mut self.stderr,
@@ -142,8 +141,8 @@ impl<'a> Visit for Event<'a> {
142141
value
143142
)
144143
.unwrap();
145-
self.comma = true;
146144
}
145+
self.comma = true;
147146
}
148147
}
149148

tracing-core/src/metadata.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -829,10 +829,10 @@ mod tests {
829829
(LevelFilter::TRACE, Some(Level::TRACE)),
830830
];
831831
for (filter, level) in mapping.iter() {
832-
assert_eq!(filter.clone().into_level(), *level);
832+
assert_eq!(filter.into_level(), *level);
833833
match level {
834834
Some(level) => {
835-
let actual: LevelFilter = level.clone().into();
835+
let actual: LevelFilter = (*level).into();
836836
assert_eq!(actual, *filter);
837837
}
838838
None => {

tracing-error/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ default = ["traced-error"]
3838
traced-error = []
3939

4040
[dependencies]
41-
tracing-subscriber = { path = "../tracing-subscriber", version = "0.2.7", default-features = false, features = ["registry", "fmt"] }
41+
tracing-subscriber = { path = "../tracing-subscriber", version = "0.2.19", default-features = false, features = ["registry", "fmt"] }
4242
tracing = { path = "../tracing", version = "0.1.12", default-features = false, features = ["std"] }
4343

4444
[badges]

tracing-error/src/layer.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -90,8 +90,7 @@ where
9090
let span = subscriber
9191
.span(id)
9292
.expect("registry should have a span for the current ID");
93-
let parents = span.parents();
94-
for span in std::iter::once(span).chain(parents) {
93+
for span in span.scope() {
9594
let cont = if let Some(fields) = span.extensions().get::<FormattedFields<F>>() {
9695
f(span.metadata(), fields.fields.as_str())
9796
} else {

tracing-flame/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ default = ["smallvec"]
2525
smallvec = ["tracing-subscriber/smallvec"]
2626

2727
[dependencies]
28-
tracing-subscriber = { path = "../tracing-subscriber", version = "0.2.3", default-features = false, features = ["registry", "fmt"] }
28+
tracing-subscriber = { path = "../tracing-subscriber", version = "0.2.19", default-features = false, features = ["registry", "fmt"] }
2929
tracing = { path = "../tracing", version = "0.1.12", default-features = false, features = ["std"] }
3030
lazy_static = "1.3.0"
3131

tracing-flame/src/lib.rs

Lines changed: 9 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -398,12 +398,10 @@ where
398398

399399
let first = ctx.span(id).expect("expected: span id exists in registry");
400400

401-
if !self.config.empty_samples && first.from_root().count() == 0 {
401+
if !self.config.empty_samples && first.parent().is_none() {
402402
return;
403403
}
404404

405-
let parents = first.from_root();
406-
407405
let mut stack = String::new();
408406

409407
if !self.config.threads_collapsed {
@@ -412,9 +410,12 @@ where
412410
stack += "all-threads";
413411
}
414412

415-
for parent in parents {
416-
stack += "; ";
417-
write(&mut stack, parent, &self.config).expect("expected: write to String never fails");
413+
if let Some(second) = first.parent() {
414+
for parent in second.scope().from_root() {
415+
stack += "; ";
416+
write(&mut stack, parent, &self.config)
417+
.expect("expected: write to String never fails");
418+
}
418419
}
419420

420421
write!(&mut stack, " {}", samples.as_nanos())
@@ -444,28 +445,22 @@ where
444445

445446
let samples = self.time_since_last_event();
446447
let first = expect!(ctx.span(&id), "expected: span id exists in registry");
447-
let parents = first.from_root();
448448

449449
let mut stack = String::new();
450450
if !self.config.threads_collapsed {
451451
THREAD_NAME.with(|name| stack += name.as_str());
452452
} else {
453453
stack += "all-threads";
454454
}
455-
stack += "; ";
456455

457-
for parent in parents {
456+
for parent in first.scope().from_root() {
457+
stack += "; ";
458458
expect!(
459459
write(&mut stack, parent, &self.config),
460460
"expected: write to String never fails"
461461
);
462-
stack += "; ";
463462
}
464463

465-
expect!(
466-
write(&mut stack, first, &self.config),
467-
"expected: write to String never fails"
468-
);
469464
expect!(
470465
write!(&mut stack, " {}", samples.as_nanos()),
471466
"expected: write to String never fails"

tracing-journald/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,4 +16,4 @@ keywords = ["tracing", "journald"]
1616

1717
[dependencies]
1818
tracing-core = { path = "../tracing-core", version = "0.1.10" }
19-
tracing-subscriber = { path = "../tracing-subscriber", version = "0.2.5" }
19+
tracing-subscriber = { path = "../tracing-subscriber", version = "0.2.19" }

tracing-journald/src/lib.rs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ where
126126
let span = ctx.span(id).expect("unknown span");
127127
let mut buf = Vec::with_capacity(256);
128128

129-
let depth = span.parents().count();
129+
let depth = span.scope().skip(1).count();
130130

131131
writeln!(buf, "S{}_NAME", depth).unwrap();
132132
put_value(&mut buf, span.name().as_bytes());
@@ -143,7 +143,7 @@ where
143143

144144
fn on_record(&self, id: &Id, values: &Record, ctx: Context<S>) {
145145
let span = ctx.span(id).expect("unknown span");
146-
let depth = span.parents().count();
146+
let depth = span.scope().skip(1).count();
147147
let mut exts = span.extensions_mut();
148148
let buf = &mut exts.get_mut::<SpanFields>().expect("missing fields").0;
149149
values.record(&mut SpanVisitor {
@@ -157,7 +157,11 @@ where
157157
let mut buf = Vec::with_capacity(256);
158158

159159
// Record span fields
160-
for span in ctx.scope() {
160+
for span in ctx
161+
.lookup_current()
162+
.into_iter()
163+
.flat_map(|span| span.scope().from_root())
164+
{
161165
let exts = span.extensions();
162166
let fields = exts.get::<SpanFields>().expect("missing fields");
163167
buf.extend_from_slice(&fields.0);

tracing-subscriber/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "tracing-subscriber"
3-
version = "0.2.18"
3+
version = "0.2.19"
44
authors = [
55
"Eliza Weisman <eliza@buoyant.io>",
66
"David Barsky <me@davidbarsky.com>",

tracing-subscriber/src/filter/env/mod.rs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -271,7 +271,7 @@ impl EnvFilter {
271271
let bold = Style::new().bold();
272272
let mut warning = Color::Yellow.paint("warning");
273273
warning.style_ref_mut().is_bold = true;
274-
format!("{}{} {}", warning, bold.clone().paint(":"), bold.paint(msg))
274+
format!("{}{} {}", warning, bold.paint(":"), bold.paint(msg))
275275
};
276276
eprintln!("{}", msg);
277277
};
@@ -308,7 +308,6 @@ impl EnvFilter {
308308
};
309309
let level = directive
310310
.level
311-
.clone()
312311
.into_level()
313312
.expect("=off would not have enabled any filters");
314313
ctx(&format!(
@@ -396,8 +395,8 @@ impl<S: Subscriber> Layer<S> for EnvFilter {
396395
return Some(LevelFilter::TRACE);
397396
}
398397
std::cmp::max(
399-
self.statics.max_level.clone().into(),
400-
self.dynamics.max_level.clone().into(),
398+
self.statics.max_level.into(),
399+
self.dynamics.max_level.into(),
401400
)
402401
}
403402

0 commit comments

Comments
 (0)