Skip to content

Do tasks inherit tracing spans? #6008

Answered by hawkw
BGR360 asked this question in Q&A
Sep 14, 2023 · 1 comments · 3 replies
Discussion options

You must be logged in to vote

Tasks don't inherit the current tracing span unless the current span is explicitly propagated to the spawned task. For example, if you want to spawn a task in the same span as the context in which it's spawned, you can use Instrument::in_current_span:

use tracing::Instrument;

tokio::spawn(my_task().in_current_span());
// which is equivalent to
tokio::spawn(my_task().instrument(tracing::Span::current());

Alternatively, if you instrument the spawned future with a new span, that span will have the current span as its parent, so that task will be conceptually "inside" the current span:

use tracing::{Instrument, info_span};

// creates a new span called `my_task`, which is a child of the curr…

Replies: 1 comment 3 replies

Comment options

You must be logged in to vote
3 replies
@BGR360
Comment options

@BGR360
Comment options

@hawkw
Comment options

Answer selected by BGR360
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants