Skip to main content

integrated_context_demo/
integrated_context_demo.rs

1//! Integrated context demo (observability_core)
2//!
3//! `observability_core` provides a small, WASM-safe trace context implementation
4//! and RAII-style scoped execution via `with_context(...)`.
5//!
6//! Higher-level domain context managers (LLM/A2A/request) live in higher crates
7//! like `structured_logging` and are intentionally not part of this foundation crate.
8
9use observability_core::{TraceContext, get_current_context, with_context};
10
11fn main() {
12    println!("🔌 observability_core context demo");
13    println!("==================================\n");
14
15    println!("Before: {:?}", get_current_context());
16
17    let root = TraceContext::new_root();
18    let trace_id = root.trace_id.clone();
19
20    let out = with_context(root, || {
21        println!("Inside: {:?}", get_current_context());
22        "ok"
23    });
24
25    println!("After: {:?}", get_current_context());
26    println!("Result: {out} (trace_id={trace_id})");
27}
28
29#[cfg(test)]
30mod tests {
31    use super::*;
32    use observability_core::clear_current_context;
33
34    #[test]
35    fn with_context_restores_previous() {
36        clear_current_context();
37        assert!(get_current_context().is_none());
38
39        let root = TraceContext::new_root();
40        let tid = root.trace_id.clone();
41
42        let r = with_context(root, || get_current_context().unwrap().trace_id);
43        assert_eq!(r, tid);
44
45        // Context is cleared after closure (since there was no previous context).
46        assert!(get_current_context().is_none());
47    }
48}