-
-
Notifications
You must be signed in to change notification settings - Fork 2.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
trace-core: Dispatchers unset themselves (#1033)
This branch changes `dispatcher::get_default` to unset the thread's current dispatcher while the reference to it is held by the closure. This prevents infinite loops if the subscriber calls code paths which emit events or construct spans. Note that this also means that nested calls to `get_default` inside of a `get_default` closure will receive a `None` dispatcher rather than the "actual" dispatcher. However, it was necessary to unset the default in `get_default` rather than in dispatch methods such as `Dispatch::enter`, as when those functions are called, the current state has already been borrowed. Before: ``` test enter_span ... bench: 3 ns/iter (+/- 0) test span_no_fields ... bench: 51 ns/iter (+/- 12) test span_repeatedly ... bench: 5,073 ns/iter (+/- 1,528) test span_with_fields ... bench: 56 ns/iter (+/- 49) test span_with_fields_record ... bench: 363 ns/iter (+/- 61) ``` After: ``` test enter_span ... bench: 3 ns/iter (+/- 0) test span_no_fields ... bench: 35 ns/iter (+/- 12) test span_repeatedly ... bench: 4,165 ns/iter (+/- 298) test span_with_fields ... bench: 48 ns/iter (+/- 12) test span_with_fields_record ... bench: 363 ns/iter (+/- 91) ``` Closes #1032 Signed-off-by: Eliza Weisman <eliza@buoyant.io>
- Loading branch information
Showing
2 changed files
with
247 additions
and
25 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
#[macro_use] | ||
extern crate tokio_trace; | ||
use tokio_trace::{ | ||
span, | ||
subscriber::{with_default, Interest, Subscriber}, | ||
Event, Level, Metadata, | ||
}; | ||
|
||
#[test] | ||
fn event_macros_dont_infinite_loop() { | ||
// This test ensures that an event macro within a subscriber | ||
// won't cause an infinite loop of events. | ||
struct TestSubscriber; | ||
impl Subscriber for TestSubscriber { | ||
fn register_callsite(&self, _: &Metadata) -> Interest { | ||
// Always return sometimes so that `enabled` will be called | ||
// (which can loop). | ||
Interest::sometimes() | ||
} | ||
|
||
fn enabled(&self, meta: &Metadata) -> bool { | ||
assert!(meta.fields().iter().any(|f| f.name() == "foo")); | ||
event!(Level::TRACE, bar = false); | ||
true | ||
} | ||
|
||
fn new_span(&self, _: &span::Attributes) -> span::Id { | ||
span::Id::from_u64(0xAAAA) | ||
} | ||
|
||
fn record(&self, _: &span::Id, _: &span::Record) {} | ||
|
||
fn record_follows_from(&self, _: &span::Id, _: &span::Id) {} | ||
|
||
fn event(&self, event: &Event) { | ||
assert!(event.metadata().fields().iter().any(|f| f.name() == "foo")); | ||
event!(Level::TRACE, baz = false); | ||
} | ||
|
||
fn enter(&self, _: &span::Id) {} | ||
|
||
fn exit(&self, _: &span::Id) {} | ||
} | ||
|
||
with_default(TestSubscriber, || { | ||
event!(Level::TRACE, foo = false); | ||
}) | ||
} |
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