-
-
Notifications
You must be signed in to change notification settings - Fork 2.5k
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
a sketch for how task naming might work for tracing #3871
Conversation
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.
Looks like a good start, left an inline comment.
tokio/src/task/spawn.rs
Outdated
|
||
cfg_trace! { | ||
#[cfg_attr(tokio_track_caller, track_caller)] | ||
pub fn spawn_named<T>(name: impl Into<std::borrow::Cow<'static, str>>, task: T) -> JoinHandle<T::Output> |
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.
Couple of thoughts.
First, is there a reason to avoid the builder patter? Generally, we try to match the std
API and specifying a name matches up w/ the thread::Builder
pattern. Also, using a builder could allow us to add Builder::spawn_blocking
as a separate API.
Then, what was the motivation to use impl Into<std::borrow::Cow<'static, str>>
? Looking at the code, it seems like &str
should work?
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.
To elaborate a bit, we can deviate from copying std
APIs and patterns, but we should have a good reason to do so.
The Cow was just to avoid people having to type the ampersand in I'll take a look at doing a builder, it just seemed like a lot of ceremony for a single property that can be set, unlike tokio::task::Builder::new().name("some task").spawn(async { … })
// vs
tokio::task::spawn_named("some task", async { … }) |
Ah, would
I expect we will have additional fields. For example, we will need a way to specify attributes. e.g. tokio::task::Builder::new()
.name("connection-handler")
.attribute("peer-addr", socket.peer_addr())
.spawn(async { ... }); Also, as I think about it, it would be an ideal place to add other configuration, like priority (if we do that). |
Backfilling explanation: The reason I thought we needed a cow was I had assumed tracing needed attribute values to be 'static, but that doesn't seem to be the case, which makes sense in retrospect |
Do you plan on opening a new PR (is that why it is closed?) |
Yep, but need to put some more thought into how this should work. I checked with @hawkw and attributes names need to be known at compile time, so it might be that annotating the spawn call with a span is actually more flexible (and therefore more ergonomic) than a builder. I'm happy to throw together a quick builder PR with names only, though. |
The initial PR should be name only, but the design direction does depend on details of things like attributes. I will ping @hawkw to weigh in here. |
Continued at #3881 |
Motivation
Currently, there is no way to attach a name to a task span for tokio-rs/console, but it would be great if that could be a predictably-present field that describes the task to a human reader. This is a quick sketch of one way this might look.
Solution
This PR adds a cfg_trace-gated unstable function tokio::task::spawn_named, which allows users to annotate a task at runtime with an appropriate name. Completing this PR would likely involve adding similar …_named spawn interfaces for spawn_blocking and spawn_local.
Example Usage
Modified from the
console-subscriber
example app: