Skip to content

Commit

Permalink
trace: Remove default trace level and make levels mandatory on span! …
Browse files Browse the repository at this point in the history
…macro (#1025)

## Motivation 

Was determined that having the span! macro default to the TRACE level is
probably not ideal (see discussion on #952). 

Closes #1013

## Solution 

Remove default trace level and make log lvl mandatory on span! macro,
and add the respective `trace_span!`, `debug_span!`, `info_span!`,
`warn_span!` and `error_span!` macros that behave as span! macro, but
with defined log levels

## Notes 

I think this is it, also removed some captures that were repeated, and
some testcases that also seemed repeated after adding the mandatory log
level, but please review it, if more tests or examples are needed happy
to provide (tried to find a way to get the generated macros log level,
but didn't find one, if there is a way i can add tests to assert that
the generated macro has the matching log level ). thanks
  • Loading branch information
jxs authored and hawkw committed Apr 2, 2019
1 parent 599955f commit 597f271
Show file tree
Hide file tree
Showing 13 changed files with 789 additions and 218 deletions.
4 changes: 3 additions & 1 deletion tokio-trace/benches/no_subscriber.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,12 @@ extern crate tokio_trace;
extern crate log;
extern crate test;
use test::Bencher;
use tokio_trace::Level;

#[bench]
fn bench_span_no_subscriber(b: &mut Bencher) {
b.iter(|| {
span!("span");
span!(Level::TRACE, "span");
});
}

Expand All @@ -24,6 +25,7 @@ fn bench_log_no_logger(b: &mut Bencher) {
fn bench_costly_field_no_subscriber(b: &mut Bencher) {
b.iter(|| {
span!(
Level::TRACE,
"span",
foo = tokio_trace::field::display(format!("bar {:?}", 2))
);
Expand Down
11 changes: 8 additions & 3 deletions tokio-trace/benches/subscriber.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
extern crate tokio_trace;
extern crate test;
use test::Bencher;
use tokio_trace::Level;

use std::{
fmt,
Expand Down Expand Up @@ -97,21 +98,23 @@ const N_SPANS: usize = 100;

#[bench]
fn span_no_fields(b: &mut Bencher) {
tokio_trace::subscriber::with_default(EnabledSubscriber, || b.iter(|| span!("span")));
tokio_trace::subscriber::with_default(EnabledSubscriber, || {
b.iter(|| span!(Level::TRACE, "span"))
});
}

#[bench]
fn enter_span(b: &mut Bencher) {
tokio_trace::subscriber::with_default(EnabledSubscriber, || {
b.iter(|| test::black_box(span!("span").enter(|| {})))
b.iter(|| test::black_box(span!(Level::TRACE, "span").enter(|| {})))
});
}

#[bench]
fn span_repeatedly(b: &mut Bencher) {
#[inline]
fn mk_span(i: u64) -> tokio_trace::Span {
span!("span", i = i)
span!(Level::TRACE, "span", i = i)
}

let n = test::black_box(N_SPANS);
Expand All @@ -125,6 +128,7 @@ fn span_with_fields(b: &mut Bencher) {
tokio_trace::subscriber::with_default(EnabledSubscriber, || {
b.iter(|| {
span!(
Level::TRACE,
"span",
foo = "foo",
bar = "bar",
Expand All @@ -141,6 +145,7 @@ fn span_with_fields_record(b: &mut Bencher) {
tokio_trace::subscriber::with_default(subscriber, || {
b.iter(|| {
span!(
Level::TRACE,
"span",
foo = "foo",
bar = "bar",
Expand Down
12 changes: 9 additions & 3 deletions tokio-trace/examples/counters.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use tokio_trace::{
field::{Field, Visit},
span,
subscriber::{self, Subscriber},
Event, Id, Metadata,
Event, Id, Level, Metadata,
};

use std::{
Expand Down Expand Up @@ -125,10 +125,16 @@ fn main() {

tokio_trace::subscriber::with_default(subscriber, || {
let mut foo: u64 = 2;
span!("my_great_span", foo_count = &foo).enter(|| {
span!(Level::TRACE, "my_great_span", foo_count = &foo).enter(|| {
foo += 1;
info!({ yak_shaved = true, yak_count = 1 }, "hi from inside my span");
span!("my other span", foo_count = &foo, baz_count = 5).enter(|| {
span!(
Level::TRACE,
"my other span",
foo_count = &foo,
baz_count = 5
)
.enter(|| {
warn!({ yak_shaved = false, yak_count = -1 }, "failed to shave yak");
});
});
Expand Down
10 changes: 5 additions & 5 deletions tokio-trace/examples/sloggish/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
#[macro_use]
extern crate tokio_trace;

use tokio_trace::field;
use tokio_trace::{field, Level};

mod sloggish_subscriber;
use self::sloggish_subscriber::SloggishSubscriber;
Expand All @@ -22,16 +22,16 @@ fn main() {
let subscriber = SloggishSubscriber::new(2);

tokio_trace::subscriber::with_default(subscriber, || {
span!("", version = &field::display(5.0)).enter(|| {
span!("server", host = "localhost", port = 8080).enter(|| {
span!(Level::TRACE, "", version = &field::display(5.0)).enter(|| {
span!(Level::TRACE, "server", host = "localhost", port = 8080).enter(|| {
info!("starting");
info!("listening");
let mut peer1 = span!("conn", peer_addr = "82.9.9.9", port = 42381);
let mut peer1 = span!(Level::TRACE, "conn", peer_addr = "82.9.9.9", port = 42381);
peer1.enter(|| {
debug!("connected");
debug!({ length = 2 }, "message received");
});
let mut peer2 = span!("conn", peer_addr = "8.8.8.8", port = 18230);
let mut peer2 = span!(Level::TRACE, "conn", peer_addr = "8.8.8.8", port = 18230);
peer2.enter(|| {
debug!("connected");
});
Expand Down
15 changes: 9 additions & 6 deletions tokio-trace/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,9 +56,10 @@
//! construct one span and perform the entire loop inside of that span, like:
//! ```rust
//! # #[macro_use] extern crate tokio_trace;
//! # use tokio_trace::Level;
//! # fn main() {
//! # let n = 1;
//! span!("my loop").enter(|| {
//! span!(Level::TRACE, "my loop").enter(|| {
//! for i in 0..n {
//! # let _ = i;
//! // ...
Expand All @@ -69,11 +70,12 @@
//! Or, should we create a new span for each iteration of the loop, as in:
//! ```rust
//! # #[macro_use] extern crate tokio_trace;
//! # use tokio_trace::Level;
//! # fn main() {
//! # let n = 1u64;
//! for i in 0..n {
//! # let _ = i;
//! span!("my loop", iteration = i).enter(|| {
//! span!(Level::TRACE, "my loop", iteration = i).enter(|| {
//! // ...
//! })
//! }
Expand Down Expand Up @@ -154,9 +156,10 @@
//! ```rust
//! # #[macro_use]
//! # extern crate tokio_trace;
//! # use tokio_trace::Level;
//! # fn main() {
//! // Construct a new span named "my span".
//! let mut span = span!("my span");
//! // Construct a new span named "my span" with trace log level.
//! let mut span = span!(Level::TRACE, "my span");
//! span.enter(|| {
//! // Any trace events in this closure or code called by it will occur within
//! // the span.
Expand Down Expand Up @@ -188,15 +191,15 @@
//! ```rust
//! #[macro_use]
//! extern crate tokio_trace;
//! use tokio_trace::field;
//! use tokio_trace::{field, Level};
//! # #[derive(Debug)] pub struct Yak(String);
//! # impl Yak { fn shave(&mut self, _: u32) {} }
//! # fn find_a_razor() -> Result<u32, u32> { Ok(1) }
//! # fn main() {
//! pub fn shave_the_yak(yak: &mut Yak) {
//! // Create a new span for this invocation of `shave_the_yak`, annotated
//! // with the yak being shaved as a *field* on the span.
//! span!("shave_the_yak", yak = field::debug(&yak)).enter(|| {
//! span!(Level::TRACE, "shave_the_yak", yak = field::debug(&yak)).enter(|| {
//! // Since the span is annotated with the yak, it is part of the context
//! // for everything happening inside the span. Therefore, we don't need
//! // to add it to the message for this event, as the `log` crate does.
Expand Down
Loading

0 comments on commit 597f271

Please sign in to comment.