-
-
Notifications
You must be signed in to change notification settings - Fork 2.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
tokio: Enable trace subscriber propagation in the runtime #966
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,6 +11,7 @@ use tokio_reactor; | |
use tokio_threadpool::Builder as ThreadPoolBuilder; | ||
use tokio_timer::clock::{self, Clock}; | ||
use tokio_timer::timer::{self, Timer}; | ||
use tokio_trace_core::dispatcher::{self, Dispatch}; | ||
|
||
/// Builds Tokio Runtime with custom configuration values. | ||
/// | ||
|
@@ -90,10 +91,10 @@ impl Builder { | |
|
||
/// Set builder to set up the thread pool instance. | ||
#[deprecated( | ||
since="0.1.9", | ||
note="use the `core_threads`, `blocking_threads`, `name_prefix`, \ | ||
`keep_alive`, and `stack_size` functions on `runtime::Builder`, \ | ||
instead")] | ||
since = "0.1.9", | ||
note = "use the `core_threads`, `blocking_threads`, `name_prefix`, \ | ||
`keep_alive`, and `stack_size` functions on `runtime::Builder`, \ | ||
instead")] | ||
#[doc(hidden)] | ||
pub fn threadpool_builder(&mut self, val: ThreadPoolBuilder) -> &mut Self { | ||
self.threadpool_builder = val; | ||
|
@@ -330,14 +331,24 @@ impl Builder { | |
// Get a handle to the clock for the runtime. | ||
let clock = self.clock.clone(); | ||
|
||
let pool = self.threadpool_builder | ||
// Get the current trace dispatcher. | ||
// TODO(eliza): when `tokio-trace-core` is stable enough to take a | ||
// public API dependency, we should allow users to set a custom | ||
// subscriber for the runtime. | ||
let dispatch = dispatcher::get_default(Dispatch::clone); | ||
|
||
let pool = self | ||
.threadpool_builder | ||
.around_worker(move |w, enter| { | ||
let index = w.id().to_usize(); | ||
let dispatch = dispatch.clone(); | ||
|
||
tokio_reactor::with_default(&reactor_handles[index], enter, |enter| { | ||
clock::with_default(&clock, enter, |enter| { | ||
timer::with_default(&timer_handles[index], enter, |_| { | ||
w.run(); | ||
dispatcher::with_default(dispatch, || { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Just noting, out of context, "dispatcher" sounds like a task dispatcher, not something about traces... There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @seanmonstar in f409c12, I changed the way the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I do agree w/ sean... I'm not going to make a request either way. I just wanted to provide these thoughts to @seanmonstar and @hawkw |
||
w.run(); | ||
}) | ||
}); | ||
}) | ||
}); | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is this clone required? It seems like most of the other
with_default
things are fine with a reference.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@seanmonstar A
Dispatch
is internally just anArc
--- we could potentially makewith_default
take an&Dispatch
and perform the clone itself. I thought it was better to take it by value since the dispatcher might not always need to be cloned whenwith_default
is used in other cases`...There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That’s a good point. with_dispatch should probably take a ref to follow the pattern.
The fn can clone internally.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I've opened #971 to make this change. As I had suspected, it has the downside of requiring the arc to be cloned twice when entering a span using the
tokio-trace::Span
type. If we decide this is acceptable, I can rebase onto that branch.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Setting the dispatcher every time a span is entered is unexpected to me. Can you expand on that decision?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@carllerche
Setting the default dispatcher on span entry is a relic of when spans tracked their parent's ID. At that time, it was necessary to ensure that any spans created inside a span were observed by the same subscriber that originally provided the entered span with an ID, as otherwise, new spans would be created with parent IDs that did not originate from that subscriber.
Now that spans don't track their parent ID, this is no longer necessary. However, removing this behavior does mean that if a span is entered outside of the subscriber context it was created in, any subsequent spans will be observed by the current default subscriber and thus will not be part of the original span's trace tree. Since subscribers are not expected to change frequently, and spans are not expected to move between them, this is likely acceptable.
I've removed this on my branch for #971 in commit 345567d.
Not setting the dispatcher on entry does make span entry/exit significantly faster. Here are the results of running a benchmark that enters a span, does nothing, and immediately exits it, before this change:
...and after: