Skip to content

Commit

Permalink
chore: remove *all* extern crate statements (tokio-rs#1738)
Browse files Browse the repository at this point in the history
Depends on tokio-rs#1737

This branch removes all remaining `extern crate` statements. Most of
these are in old code and were not removed when updating to Rust 2018.
Whoops!

Our MSRV no longer requires `extern crate`, so we don't need these. The
exception is `extern crate` statements for `std` and `alloc`, which are
still the way these libraries are included explicitly when building for
`no_std` platforms.

In some cases, the tests had to explicitly import the `span!` and
`event!` macros at every use, because their names conflict with the
`span` and `event` modules in the test support code. Oh well.

Signed-off-by: Eliza Weisman <eliza@buoyant.io>
  • Loading branch information
hawkw authored and kaffarell committed May 22, 2024
1 parent 58c0f19 commit 0cd273d
Show file tree
Hide file tree
Showing 4 changed files with 199 additions and 14 deletions.
3 changes: 1 addition & 2 deletions tracing-core/src/subscriber.rs
Original file line number Diff line number Diff line change
Expand Up @@ -246,8 +246,7 @@ pub trait Subscriber: 'static {
/// but values for them won't be recorded at this time.
///
/// ```rust,ignore
/// #[macro_use]
/// extern crate tracing;
/// # use tracing::span;
///
/// let mut span = span!("my_span", foo = 3, bar, baz);
///
Expand Down
189 changes: 189 additions & 0 deletions tracing/benches/subscriber.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,189 @@
use criterion::{black_box, criterion_group, criterion_main, Criterion};
use tracing::Level;

use std::{
fmt,
sync::{Mutex, MutexGuard},
};
use tracing::{field, span, Event, Id, Metadata};

/// A subscriber that is enabled but otherwise does nothing.
struct EnabledSubscriber;

impl tracing::Subscriber for EnabledSubscriber {
fn new_span(&self, span: &span::Attributes<'_>) -> Id {
let _ = span;
Id::from_u64(0xDEAD_FACE)
}

fn event(&self, event: &Event<'_>) {
let _ = event;
}

fn record(&self, span: &Id, values: &span::Record<'_>) {
let _ = (span, values);
}

fn record_follows_from(&self, span: &Id, follows: &Id) {
let _ = (span, follows);
}

fn enabled(&self, metadata: &Metadata<'_>) -> bool {
let _ = metadata;
true
}

fn enter(&self, span: &Id) {
let _ = span;
}

fn exit(&self, span: &Id) {
let _ = span;
}
}

/// Simulates a subscriber that records span data.
struct VisitingSubscriber(Mutex<String>);

struct Visitor<'a>(MutexGuard<'a, String>);

impl<'a> field::Visit for Visitor<'a> {
fn record_debug(&mut self, _field: &field::Field, value: &dyn fmt::Debug) {
use std::fmt::Write;
let _ = write!(&mut *self.0, "{:?}", value);
}
}

impl tracing::Subscriber for VisitingSubscriber {
fn new_span(&self, span: &span::Attributes<'_>) -> Id {
let mut visitor = Visitor(self.0.lock().unwrap());
span.record(&mut visitor);
Id::from_u64(0xDEAD_FACE)
}

fn record(&self, _span: &Id, values: &span::Record<'_>) {
let mut visitor = Visitor(self.0.lock().unwrap());
values.record(&mut visitor);
}

fn event(&self, event: &Event<'_>) {
let mut visitor = Visitor(self.0.lock().unwrap());
event.record(&mut visitor);
}

fn record_follows_from(&self, span: &Id, follows: &Id) {
let _ = (span, follows);
}

fn enabled(&self, metadata: &Metadata<'_>) -> bool {
let _ = metadata;
true
}

fn enter(&self, span: &Id) {
let _ = span;
}

fn exit(&self, span: &Id) {
let _ = span;
}
}

const N_SPANS: usize = 100;

fn criterion_benchmark(c: &mut Criterion) {
c.bench_function("span_no_fields", |b| {
tracing::subscriber::with_default(EnabledSubscriber, || {
b.iter(|| span!(Level::TRACE, "span"))
});
});

c.bench_function("enter_span", |b| {
tracing::subscriber::with_default(EnabledSubscriber, || {
let span = span!(Level::TRACE, "span");
#[allow(clippy::unit_arg)]
b.iter(|| black_box(span.in_scope(|| {})))
});
});

c.bench_function("span_repeatedly", |b| {
#[inline]
fn mk_span(i: u64) -> tracing::Span {
span!(Level::TRACE, "span", i = i)
}

let n = black_box(N_SPANS);
tracing::subscriber::with_default(EnabledSubscriber, || {
b.iter(|| (0..n).fold(mk_span(0), |_, i| mk_span(i as u64)))
});
});

c.bench_function("span_with_fields", |b| {
tracing::subscriber::with_default(EnabledSubscriber, || {
b.iter(|| {
span!(
Level::TRACE,
"span",
foo = "foo",
bar = "bar",
baz = 3,
quuux = tracing::field::debug(0.99)
)
})
});
});

c.bench_function("span_with_fields_record", |b| {
let subscriber = VisitingSubscriber(Mutex::new(String::from("")));
tracing::subscriber::with_default(subscriber, || {
b.iter(|| {
span!(
Level::TRACE,
"span",
foo = "foo",
bar = "bar",
baz = 3,
quuux = tracing::field::debug(0.99)
)
})
});
});
}

fn bench_dispatch(c: &mut Criterion) {
let mut group = c.benchmark_group("dispatch");
group.bench_function("no_dispatch_get_ref", |b| {
b.iter(|| {
tracing::dispatcher::get_default(|current| {
black_box(&current);
})
})
});
group.bench_function("no_dispatch_get_clone", |b| {
b.iter(|| {
let current = tracing::dispatcher::get_default(|current| current.clone());
black_box(current);
})
});
group.bench_function("get_ref", |b| {
tracing::subscriber::with_default(EnabledSubscriber, || {
b.iter(|| {
tracing::dispatcher::get_default(|current| {
black_box(&current);
})
})
})
});
group.bench_function("get_clone", |b| {
tracing::subscriber::with_default(EnabledSubscriber, || {
b.iter(|| {
let current = tracing::dispatcher::get_default(|current| current.clone());
black_box(current);
})
})
});
group.finish();
}

criterion_group!(benches, criterion_benchmark, bench_dispatch);
criterion_main!(benches);
8 changes: 4 additions & 4 deletions tracing/tests/span.rs
Original file line number Diff line number Diff line change
Expand Up @@ -348,8 +348,8 @@ fn entered() {
.done()
.run_with_handle();
with_default(subscriber, || {
let _span = span!(Level::TRACE, "foo").entered();
debug!("dropping guard...");
let _span = tracing::span!(Level::TRACE, "foo").entered();
tracing::debug!("dropping guard...");
});

handle.assert_finished();
Expand All @@ -366,9 +366,9 @@ fn entered_api() {
.done()
.run_with_handle();
with_default(subscriber, || {
let span = span!(Level::TRACE, "foo").entered();
let span = tracing::span!(Level::TRACE, "foo").entered();
let _derefs_to_span = span.id();
debug!("exiting span...");
tracing::debug!("exiting span...");
let _: Span = span.exit();
});

Expand Down
13 changes: 5 additions & 8 deletions tracing/tests/subscriber.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,6 @@
// The alternative would be for each of these tests to be defined in a separate
// file, which is :(
#![cfg(feature = "std")]

#[macro_use]
extern crate tracing;
use tracing::{
field::display,
span::{Attributes, Id, Record},
Expand All @@ -34,7 +31,7 @@ fn event_macros_dont_infinite_loop() {

fn enabled(&self, meta: &Metadata<'_>) -> bool {
assert!(meta.fields().iter().any(|f| f.name() == "foo"));
event!(Level::TRACE, bar = false);
tracing::event!(Level::TRACE, bar = false);
true
}

Expand All @@ -48,7 +45,7 @@ fn event_macros_dont_infinite_loop() {

fn event(&self, event: &Event<'_>) {
assert!(event.metadata().fields().iter().any(|f| f.name() == "foo"));
event!(Level::TRACE, baz = false);
tracing::event!(Level::TRACE, baz = false);
}

fn enter(&self, _: &Id) {}
Expand All @@ -57,7 +54,7 @@ fn event_macros_dont_infinite_loop() {
}

with_default(TestSubscriber, || {
event!(Level::TRACE, foo = false);
tracing::event!(Level::TRACE, foo = false);
})
}

Expand All @@ -81,7 +78,7 @@ fn boxed_subscriber() {

with_default(subscriber, || {
let from = "my span";
let span = span!(
let span = tracing::span!(
Level::TRACE,
"foo",
bar = format_args!("hello from {}", from)
Expand Down Expand Up @@ -119,7 +116,7 @@ fn arced_subscriber() {
// Test using a clone of the `Arc`ed subscriber
with_default(subscriber.clone(), || {
let from = "my span";
let span = span!(
let span = tracing::span!(
Level::TRACE,
"foo",
bar = format_args!("hello from {}", from)
Expand Down

0 comments on commit 0cd273d

Please sign in to comment.