-
Notifications
You must be signed in to change notification settings - Fork 701
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
tracing: log span lifecycle events under a separate target
## Motivation Currently, the `tracing` crate's "log" feature always logs span lifecycle (creation/enter/exit/close) events, even when the span has no fields. This can be quite noisy. Since the enter, exit, and close logs don't include the span's fields, it doesn't add that much additional context to the surrounding log records. ## Solution In order to continue recording this data while improving the signal-to-noise ratio of `tracing`-generated logs, this commit changes the log integration to log span lifecycle events under a separate target, "tracing::span". This way, log users can opt in to this data when it is desired, but logs are generally much less noisy. Span creation is still logged to the default target when the span has fields, as this often provides useful information, but is sent to the "tracing-span" target when there are no fields. Fixes #607
- Loading branch information
Showing
4 changed files
with
201 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
77 changes: 77 additions & 0 deletions
77
tracing/test-log-support/tests/span_lifecycle_can_be_filtered.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
#[macro_use] | ||
extern crate tracing; | ||
extern crate test_log_support; | ||
|
||
use test_log_support::Test; | ||
use tracing::Level; | ||
|
||
#[test] | ||
fn span_lifecycle_can_be_filtered() { | ||
let test = Test::with_filters(&[ | ||
("span_lifecycle_can_be_filtered", log::LevelFilter::Trace), | ||
("tracing::span", log::LevelFilter::Info), | ||
]); | ||
|
||
error!(foo = 5); | ||
test.assert_logged("foo=5"); | ||
|
||
warn!("hello {};", "world"); | ||
test.assert_logged("hello world;"); | ||
|
||
info!(message = "hello world;", thingy = 42, other_thingy = 666); | ||
test.assert_logged("hello world; thingy=42 other_thingy=666"); | ||
|
||
let foo = span!(Level::TRACE, "foo"); | ||
test.assert_not_logged(); | ||
|
||
foo.in_scope(|| { | ||
// enter should not be logged | ||
test.assert_not_logged(); | ||
|
||
trace!({foo = 3, bar = 4}, "hello {};", "san francisco"); | ||
test.assert_logged("hello san francisco; foo=3 bar=4"); | ||
}); | ||
// exit should not be logged | ||
test.assert_not_logged(); | ||
|
||
drop(foo); | ||
// drop should not be logged | ||
test.assert_not_logged(); | ||
|
||
trace!(foo = 1, bar = 2, "hello world"); | ||
test.assert_logged("hello world foo=1 bar=2"); | ||
|
||
let foo = span!(Level::TRACE, "foo", bar = 3, baz = false); | ||
// creating a span with fields _should_ be logged. | ||
test.assert_logged("foo; bar=3 baz=false"); | ||
|
||
foo.in_scope(|| { | ||
// entering the span should not be logged | ||
test.assert_not_logged(); | ||
}); | ||
// exiting the span should not be logged | ||
test.assert_not_logged(); | ||
|
||
foo.record("baz", &true); | ||
// recording a field should be logged | ||
test.assert_logged("foo; baz=true"); | ||
|
||
let bar = span!(Level::INFO, "bar"); | ||
// lifecycles for INFO spans should be logged | ||
test.assert_logged("bar;"); | ||
|
||
bar.in_scope(|| { | ||
// entering the INFO span should be logged | ||
test.assert_logged("-> bar"); | ||
}); | ||
// exiting the INFO span should be logged | ||
test.assert_logged("<- bar"); | ||
|
||
drop(foo); | ||
// drop should not be logged. | ||
test.assert_not_logged(); | ||
|
||
drop(bar); | ||
// dropping the INFO should be logged. | ||
test.assert_logged("-- bar"); | ||
} |
59 changes: 59 additions & 0 deletions
59
tracing/test-log-support/tests/span_lifecycle_defaults_off.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
#[macro_use] | ||
extern crate tracing; | ||
extern crate test_log_support; | ||
|
||
use test_log_support::Test; | ||
use tracing::Level; | ||
|
||
#[test] | ||
fn span_lifecycle_defaults_off() { | ||
let test = Test::with_filters(&[("span_lifecycle_defaults_off", log::LevelFilter::Trace)]); | ||
|
||
error!(foo = 5); | ||
test.assert_logged("foo=5"); | ||
|
||
warn!("hello {};", "world"); | ||
test.assert_logged("hello world;"); | ||
|
||
info!(message = "hello world;", thingy = 42, other_thingy = 666); | ||
test.assert_logged("hello world; thingy=42 other_thingy=666"); | ||
|
||
let foo = span!(Level::TRACE, "foo"); | ||
test.assert_not_logged(); | ||
|
||
foo.in_scope(|| { | ||
// enter should not be logged | ||
test.assert_not_logged(); | ||
|
||
trace!({foo = 3, bar = 4}, "hello {};", "san francisco"); | ||
test.assert_logged("hello san francisco; foo=3 bar=4"); | ||
}); | ||
// exit should not be logged | ||
test.assert_not_logged(); | ||
|
||
drop(foo); | ||
// drop should not be logged | ||
test.assert_not_logged(); | ||
|
||
trace!(foo = 1, bar = 2, "hello world"); | ||
test.assert_logged("hello world foo=1 bar=2"); | ||
|
||
let foo = span!(Level::TRACE, "foo", bar = 3, baz = false); | ||
// creating a span with fields _should_ be logged. | ||
test.assert_logged("foo; bar=3 baz=false"); | ||
|
||
foo.in_scope(|| { | ||
// entering the span should not be logged | ||
test.assert_not_logged(); | ||
}); | ||
// exiting the span should not be logged | ||
test.assert_not_logged(); | ||
|
||
foo.record("baz", &true); | ||
// recording a field should be logged | ||
test.assert_logged("foo; baz=true"); | ||
|
||
drop(foo); | ||
// drop should not be logged. | ||
test.assert_not_logged(); | ||
} |