-
In this PR I tried to implement a diagonistic fetching context info from tokio's task local values. For thread-local, I can write: thread_local! {
static CONTEXT: RefCell<BTreeMap<String, String>> = const { RefCell::new(BTreeMap::new()) };
}
pub struct ThreadLocalDiagnostic {}
impl ThreadLocalDiagnostic {
pub fn insert<K, V>(key: K, value: V)
where
K: Into<String>,
V: Into<String>,
{
CONTEXT.with(|map| {
map.borrow_mut().insert(key.into(), value.into());
});
}
}
impl Diagnostic for ThreadLocalDiagnostic {
fn visit(&self, visitor: &mut dyn Visitor) -> Result<(), Error> {
CONTEXT.with(|map| {
let map = map.borrow();
for (key, value) in map.iter() {
let key = Key::new_ref(key.as_str());
let value = Value::from(value);
visitor.visit(key, value)?;
}
Ok(())
})
}
} However, for task-local, while I write: task_local! {
static CONTEXT: Vec<(String, String)>;
}
pub struct TaskLocalDiagnostic {}
impl Diagnostic for TaskLocalDiagnostic {
fn visit(&self, visitor: &mut dyn Visitor) -> Result<(), Error> {
CONTEXT.with(|map| {
for (k, v) in map {
let key = Key::new_ref(k.as_str());
let value = Value::from(v);
visitor.visit(key, value)?;
}
Ok(())
})
}
}
pub trait FutureExt: Future {
fn with_task_local_context(
self,
kvs: impl IntoIterator<Item = (String, String)>,
) -> impl Future<Output = Self::Output>
where
Self: Sized,
Self::Output: 'static,
{
let mut context = CONTEXT.try_with(|v| v.clone()).unwrap_or_default();
context.extend(kvs);
CONTEXT.scope(context, self)
}
} And use it as: rt.block_on(
async {
let diag = TaskLocalDiagnostic::default();
diag.visit(&mut PrintVisitor).unwrap();
}
.with_task_local_context([("user_id".to_string(), "42".to_string())])
.with_task_local_context([("request_id".to_string(), "abc123".to_string())]),
); It seems the most inner future would fetch only the most inner CONTEXT and print only What I expect is:
Any suggestion? |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 2 replies
-
The problem is that |
Beta Was this translation helpful? Give feedback.
-
Resolved in fast/logforth@c66b21e The original requirement is task local context and I learn from tokio's tech without directly depend on tokio's task_local. |
Beta Was this translation helpful? Give feedback.
Resolved in fast/logforth@c66b21e
The original requirement is task local context and I learn from tokio's tech without directly depend on tokio's task_local.