-
-
Notifications
You must be signed in to change notification settings - Fork 2.6k
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
tracing: replace future names with spawn locations in task spans #3074
Changes from all commits
b43d90d
e48d3e3
a0bc98a
5e78045
00e7f02
eb11d8e
e04688d
0a91353
f05a469
47017ec
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 |
---|---|---|
@@ -0,0 +1,22 @@ | ||
use autocfg::AutoCfg; | ||
|
||
fn main() { | ||
match AutoCfg::new() { | ||
Ok(ac) => { | ||
// The #[track_caller] attribute was stabilized in rustc 1.46.0. | ||
if ac.probe_rustc_version(1, 46) { | ||
autocfg::emit("tokio_track_caller") | ||
} | ||
} | ||
|
||
Err(e) => { | ||
// If we couldn't detect the compiler version and features, just | ||
// print a warning. This isn't a fatal error: we can still build | ||
// Tokio, we just can't enable cfgs automatically. | ||
println!( | ||
"cargo:warning=tokio: failed to detect compiler features: {}", | ||
e | ||
); | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change | ||||||||
---|---|---|---|---|---|---|---|---|---|---|
@@ -1,47 +1,27 @@ | ||||||||||
cfg_trace! { | ||||||||||
cfg_rt! { | ||||||||||
use std::future::Future; | ||||||||||
use std::pin::Pin; | ||||||||||
use std::task::{Context, Poll}; | ||||||||||
use pin_project_lite::pin_project; | ||||||||||
|
||||||||||
use tracing::Span; | ||||||||||
|
||||||||||
pin_project! { | ||||||||||
/// A future that has been instrumented with a `tracing` span. | ||||||||||
#[derive(Debug, Clone)] | ||||||||||
pub(crate) struct Instrumented<T> { | ||||||||||
#[pin] | ||||||||||
inner: T, | ||||||||||
span: Span, | ||||||||||
} | ||||||||||
} | ||||||||||
|
||||||||||
impl<T: Future> Future for Instrumented<T> { | ||||||||||
type Output = T::Output; | ||||||||||
|
||||||||||
fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> { | ||||||||||
let this = self.project(); | ||||||||||
let _enter = this.span.enter(); | ||||||||||
this.inner.poll(cx) | ||||||||||
} | ||||||||||
} | ||||||||||
|
||||||||||
impl<T> Instrumented<T> { | ||||||||||
pub(crate) fn new(inner: T, span: Span) -> Self { | ||||||||||
Self { inner, span } | ||||||||||
} | ||||||||||
} | ||||||||||
pub(crate) use tracing::instrument::Instrumented; | ||||||||||
|
||||||||||
#[inline] | ||||||||||
#[cfg_attr(tokio_track_caller, track_caller)] | ||||||||||
pub(crate) fn task<F>(task: F, kind: &'static str) -> Instrumented<F> { | ||||||||||
use tracing::instrument::Instrument; | ||||||||||
#[cfg(tokio_track_caller)] | ||||||||||
let location = std::panic::Location::caller(); | ||||||||||
#[cfg(tokio_track_caller)] | ||||||||||
let span = tracing::trace_span!( | ||||||||||
target: "tokio::task", | ||||||||||
"task", | ||||||||||
%kind, | ||||||||||
spawn.location = %format_args!("{}:{}:{}", location.file(), location.line(), location.column()), | ||||||||||
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.
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. also, we could alternatively record the filename, line number, and column as separate fields on the task span, like
Suggested change
and rely on the |
||||||||||
); | ||||||||||
#[cfg(not(tokio_track_caller))] | ||||||||||
let span = tracing::trace_span!( | ||||||||||
target: "tokio::task", | ||||||||||
"task", | ||||||||||
%kind, | ||||||||||
future = %std::any::type_name::<F>(), | ||||||||||
); | ||||||||||
Instrumented::new(task, span) | ||||||||||
task.instrument(span) | ||||||||||
} | ||||||||||
} | ||||||||||
} | ||||||||||
|
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.
Note that this enables
track_caller
onspawn
even when it's not used by the "tracing" feature flag. I think this is actually a good thing, since it means the panic when spawn is called outside the runtime will have the callsite's location, which may make debugging a little easier for users?However, if folks prefer, I can change this to only enable
track_caller
when the cfg is set and the "tracing" feature flag is enabled.