You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Our trace relays on the Context in thread local storage to store global state. But in tokio single thread executor, everything runs in the same context. Thus, we you spawn a new task, the context will be the same.
Example
use opentelemetry::exporter::trace::stdout;use opentelemetry::trace::{Tracer,TraceContextExt,StatusCode};use opentelemetry::{Context,Key};use std::thread::{spawn, sleep};use std::borrow::Cow;fnmain(){letmut runtime = tokio::runtime::Builder::new().threaded_scheduler().enable_all().build().unwrap();
runtime.block_on(async{let(tracer, _uninstall) = stdout::new_pipeline().install();let span = tracer.start("test op1");let cx = Context::current_with_span(span);let _guard = cx.clone().attach();
tokio::spawn(asyncmove{// in sync enviroment, we will spawn a new thread // and as a result we will have a new Context in a new thread local // but here we are reuse the same context and thread localanother_operation(None);}).await;sleep(std::time::Duration::from_secs(2));});}fnanother_operation(cx:Option<Context>){let cx = Context::current();let span = cx.span();
span.update_name("test op2".to_string());}
Actually this problem could also occur in multiple thread executors since there is no guarantee which thread task will run on.
The text was updated successfully, but these errors were encountered:
Our trace relays on the
Context
in thread local storage to store global state. But in tokio single thread executor, everything runs in the same context. Thus, we you spawn a new task, the context will be the same.Example
Actually this problem could also occur in multiple thread executors since there is no guarantee which thread task will run on.
The text was updated successfully, but these errors were encountered: