mirror of
https://github.com/tokio-rs/tokio.git
synced 2026-08-25 00:00:18 +02:00
## Motivation Currently, the per-task `tracing` spans generated by tokio's `tracing` feature flag include the `std::any::type_name` of the future that was spawned. When future combinators and/or libraries like Tower are in use, these future names can get _quite_ long. Furthermore, when formatting the `tracing` spans with their parent spans as context, any other task spans in the span context where the future was spawned from can _also_ include extremely long future names. In some cases, this can result in extremely high memory use just to store the future names. For example, in Linkerd, when we enable `tokio=trace` to enable the task spans, there's a spawned task whose future name is _232990 characters long_. A proxy with only 14 spawned tasks generates a task list that's over 690 KB. Enabling task spans under load results in the process getting OOM killed very quickly. ## Solution This branch removes future type names from the spans generated by `spawn`. As a replacement, to allow identifying which `spawn` call a span corresponds to, the task span now contains the source code location where `spawn` was called, when the compiler supports the `#[track_caller]` attribute. Since `track_caller` was stabilized in Rust 1.46.0, and our minimum supported Rust version is 1.45.0, we can't assume that `#[track_caller]` is always available. Instead, we have a RUSTFLAGS cfg, `tokio_track_caller`, that guards whether or not we use it. I've also added a `build.rs` that detects the compiler minor version, and sets the cfg flag automatically if the current compiler version is >= 1.46. This means users shouldn't have to enable `tokio_track_caller` manually. Here's the trace output from the `chat` example, before this change:  ...and after:  Closes #3073 Signed-off-by: Eliza Weisman <[email protected]>
23 lines
672 B
Rust
23 lines
672 B
Rust
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
|
|
);
|
|
}
|
|
}
|
|
}
|