Missing events when using tracing-opentelemetry with threads #1885
-
Bug ReportVersion
PlatformOS X DescriptionI have a project that uses some I attempted to distill a repro by using the stdout tracer, removing logging entirely (as well as the inclusion of trace IDs), and have found what looks like a bug -- when starting a new thread, events in the immediate first span are not traced (they are logged if I use a standard logging subscriber), and if I create a span within that it gets a new trace ID rather than being part of the current trace. I've also tried explicitly passing the parent in, explicitly getting the current context and re-attaching it within the thread, etc. and haven't been able to find a way to get the spans within the thread properly associated. Code is shown below -- I expected to see the event
|
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
Sorry for the delay in responding. First things first: I don't recommend spawning a thread inside an async function; I would do this outside of that context or use I'm able to get the event with the message "THIS EVENT DOES NOT SHOW UP" with the following code: #[tracing::instrument]
fn some_fn() {
info!("In some_fn");
let span = info_span!("Inner span");
let thread = std::thread::spawn(move || {
let _span = span.entered();
// let _ = info_span!(parent: &parent, "Inner span").entered();
info!("THIS EVENT DOES NOT SHOW UP");
let _enter2 = info_span!("Nested span").entered();
info!("In nested_span");
});
thread.join().unwrap();
} There are two contributing factors that explain what's happening here:
|
Beta Was this translation helpful? Give feedback.
Sorry for the delay in responding. First things first: I don't recommend spawning a thread inside an async function; I would do this outside of that context or use
tokio::task::spawn_blocking
if benchmarks indicate that it's worthwhile to use. Freely spawning threads inside of a Tokio context (or any other futures executor) risks blocking the thread by not allowing the task to suspend, leading to worse performance.I'm able to get the event with the message "THIS EVENT DOES NOT SHOW UP" with the following code: