tokio-rs / tokio

A runtime for writing reliable asynchronous applications with Rust. Provides I/O, networking, scheduling, timers, ...
https://tokio.rs
MIT License
26.97k stars 2.48k forks source link

Enrich the structure of traces emitted by Tokio #5792

Open hds opened 1 year ago

hds commented 1 year ago

Is your feature request related to a problem? Please describe. The traces produced by Tokio are great, however there are some areas in which the hierarchy in which the traces are emitted could be improved or doesn't make sense.

Specific issues are:

Describe the solution you'd like

I would like to add some additional traces to Tokio and modify others.

The proposed hierarchy is the following. Sub-items have their span's parent set. Values in parentheses are related fields which would be set. Note that all spans have a span ID implicitly, these are used for internal referencing.

Further explanation of the structure:

  1. Runtimes are top level (root) spans.
  2. Workers belong to runtimes
  3. Blocking threads belong to runtimes
    1. If a blocking thread is destroyed and another is created, it will have a different span (even if the id is the same)
  4. Tasks belong to runtimes
    1. Tasks don’t have parent tasks, but they follow_from the task that spawned them (if any)
    2. This is a breaking change
  5. Resources are top level (root) spans
    1. They may be shared between runtimes
    2. They reference the runtime (if any) and task (if any) that created the resource
  6. Async ops belong to tasks (the task that executes the async op)
    1. They reference the resource that the async op belongs to
    2. They reference the worker id where they are executed

Ideally all these changes, or at least all the breaking ones would make it into the same Tokio release. Preferably with the addition of spans for Runtimes. This would allow Tokio Console to determine the version tokio tracing variant being used and read the traces accordingly.

Describe alternatives you've considered

Parts of this hierarchy can be implemented without the rest. But it makes sense to discuss the entirety.

Additional context

This is mostly considering being consumed by Tokio Console. However it would be interesting to hear from anyone who is using tokio traces in other ways.

Darksonn commented 1 year ago

This sounds reasonable to me. You should probably coordinate with @carllerche's scheduler refactor, which may change the same parts of the codebase.

qRoC commented 8 months ago

A task may outlive the context in which is was spawned (and frequently does).

@hds What about block_on?

This runs the given future on the current thread, blocking until it is complete

Looks like him span should have parent.

hds commented 8 months ago

A task may outlive the context in which is was spawned (and frequently does).

@hds What about block_on?

This runs the given future on the current thread, blocking until it is complete

Looks like him span should have parent.

This is something we should consider. We've taken it out for now, but a subscriber could still get the context for that span and store it if it needed to.