Skip to main content

harn_vm/
tracing.rs

1//! Pipeline Observability: structured tracing spans with parent/child relationships.
2//!
3//! When tracing is enabled (`vm.enable_tracing()`), the VM automatically emits
4//! spans for pipeline execution, function calls, LLM calls, tool invocations,
5//! imports, and async operations. Spans form a tree via parent_span_id.
6//!
7//! Access via builtins: `trace_spans()` returns all completed spans,
8//! `trace_summary()` returns a formatted summary.
9
10use std::cell::RefCell;
11use std::collections::BTreeMap;
12use std::rc::Rc;
13use std::time::Instant;
14
15use crate::value::VmValue;
16
17// =============================================================================
18// Span types
19// =============================================================================
20
21/// The kind of operation a span represents.
22#[derive(Debug, Clone, Copy, PartialEq, Eq)]
23pub enum SpanKind {
24    Pipeline,
25    FnCall,
26    LlmCall,
27    ToolCall,
28    Import,
29    Parallel,
30    Spawn,
31}
32
33impl SpanKind {
34    pub fn as_str(self) -> &'static str {
35        match self {
36            Self::Pipeline => "pipeline",
37            Self::FnCall => "fn_call",
38            Self::LlmCall => "llm_call",
39            Self::ToolCall => "tool_call",
40            Self::Import => "import",
41            Self::Parallel => "parallel",
42            Self::Spawn => "spawn",
43        }
44    }
45}
46
47/// A completed tracing span.
48#[derive(Debug, Clone)]
49pub struct Span {
50    pub span_id: u64,
51    pub parent_id: Option<u64>,
52    pub kind: SpanKind,
53    pub name: String,
54    pub start_ms: u64,
55    pub duration_ms: u64,
56    pub metadata: BTreeMap<String, serde_json::Value>,
57}
58
59/// An in-flight span (not yet completed).
60struct OpenSpan {
61    span_id: u64,
62    parent_id: Option<u64>,
63    kind: SpanKind,
64    name: String,
65    started_at: Instant,
66    metadata: BTreeMap<String, serde_json::Value>,
67}
68
69// =============================================================================
70// Collector
71// =============================================================================
72
73/// Thread-local span collector. Accumulates completed spans and tracks the
74/// active span stack for automatic parent assignment.
75pub struct SpanCollector {
76    next_id: u64,
77    /// Stack of open span IDs — the top is the current active span.
78    active_stack: Vec<u64>,
79    /// Open (in-flight) spans keyed by ID.
80    open: BTreeMap<u64, OpenSpan>,
81    /// Completed spans in chronological order.
82    completed: Vec<Span>,
83    /// Epoch for relative timing.
84    epoch: Instant,
85}
86
87impl Default for SpanCollector {
88    fn default() -> Self {
89        Self::new()
90    }
91}
92
93impl SpanCollector {
94    pub fn new() -> Self {
95        Self {
96            next_id: 1,
97            active_stack: Vec::new(),
98            open: BTreeMap::new(),
99            completed: Vec::new(),
100            epoch: Instant::now(),
101        }
102    }
103
104    /// Start a new span. Returns the span ID.
105    pub fn start(&mut self, kind: SpanKind, name: String) -> u64 {
106        let id = self.next_id;
107        self.next_id += 1;
108        let parent_id = self.active_stack.last().copied();
109        let now = Instant::now();
110
111        self.open.insert(
112            id,
113            OpenSpan {
114                span_id: id,
115                parent_id,
116                kind,
117                name,
118                started_at: now,
119                metadata: BTreeMap::new(),
120            },
121        );
122        self.active_stack.push(id);
123        id
124    }
125
126    /// Attach metadata to an open span.
127    pub fn set_metadata(&mut self, span_id: u64, key: &str, value: serde_json::Value) {
128        if let Some(span) = self.open.get_mut(&span_id) {
129            span.metadata.insert(key.to_string(), value);
130        }
131    }
132
133    /// End a span. Moves it from open to completed.
134    pub fn end(&mut self, span_id: u64) {
135        if let Some(span) = self.open.remove(&span_id) {
136            let duration = span.started_at.elapsed();
137            let start_ms = span.started_at.duration_since(self.epoch).as_millis() as u64;
138
139            self.completed.push(Span {
140                span_id: span.span_id,
141                parent_id: span.parent_id,
142                kind: span.kind,
143                name: span.name,
144                start_ms,
145                duration_ms: duration.as_millis() as u64,
146                metadata: span.metadata,
147            });
148
149            // Remove from active stack
150            if let Some(pos) = self.active_stack.iter().rposition(|&id| id == span_id) {
151                self.active_stack.remove(pos);
152            }
153        }
154    }
155
156    /// Get the current active span ID (if any).
157    pub fn current_span_id(&self) -> Option<u64> {
158        self.active_stack.last().copied()
159    }
160
161    /// Take all completed spans (drains the collector).
162    pub fn take_spans(&mut self) -> Vec<Span> {
163        std::mem::take(&mut self.completed)
164    }
165
166    /// Peek at all completed spans (non-destructive).
167    pub fn spans(&self) -> &[Span] {
168        &self.completed
169    }
170
171    /// Reset the collector.
172    pub fn reset(&mut self) {
173        self.active_stack.clear();
174        self.open.clear();
175        self.completed.clear();
176        self.next_id = 1;
177        self.epoch = Instant::now();
178    }
179}
180
181// =============================================================================
182// Thread-local collector
183// =============================================================================
184
185thread_local! {
186    static COLLECTOR: RefCell<SpanCollector> = RefCell::new(SpanCollector::new());
187    static TRACING_ENABLED: RefCell<bool> = const { RefCell::new(false) };
188}
189
190/// Enable or disable VM tracing for the current thread.
191pub fn set_tracing_enabled(enabled: bool) {
192    TRACING_ENABLED.with(|e| *e.borrow_mut() = enabled);
193    if enabled {
194        COLLECTOR.with(|c| c.borrow_mut().reset());
195    }
196}
197
198/// Check if tracing is enabled.
199pub fn is_tracing_enabled() -> bool {
200    TRACING_ENABLED.with(|e| *e.borrow())
201}
202
203/// Start a span (no-op if tracing disabled). Returns span ID or 0.
204pub fn span_start(kind: SpanKind, name: String) -> u64 {
205    if !is_tracing_enabled() {
206        return 0;
207    }
208    COLLECTOR.with(|c| c.borrow_mut().start(kind, name))
209}
210
211/// Attach metadata to an open span (no-op if span_id is 0).
212pub fn span_set_metadata(span_id: u64, key: &str, value: serde_json::Value) {
213    if span_id == 0 {
214        return;
215    }
216    COLLECTOR.with(|c| c.borrow_mut().set_metadata(span_id, key, value));
217}
218
219/// End a span (no-op if span_id is 0).
220pub fn span_end(span_id: u64) {
221    if span_id == 0 {
222        return;
223    }
224    COLLECTOR.with(|c| c.borrow_mut().end(span_id));
225}
226
227/// Take all completed spans.
228pub fn take_spans() -> Vec<Span> {
229    COLLECTOR.with(|c| c.borrow_mut().take_spans())
230}
231
232/// Peek at completed spans (cloned).
233pub fn peek_spans() -> Vec<Span> {
234    COLLECTOR.with(|c| c.borrow().spans().to_vec())
235}
236
237/// Reset the tracing collector.
238pub fn reset_tracing() {
239    COLLECTOR.with(|c| c.borrow_mut().reset());
240}
241
242// =============================================================================
243// VmValue conversion
244// =============================================================================
245
246/// Convert a span to a VmValue dict for user access.
247pub fn span_to_vm_value(span: &Span) -> VmValue {
248    let mut d = BTreeMap::new();
249    d.insert("span_id".into(), VmValue::Int(span.span_id as i64));
250    d.insert(
251        "parent_id".into(),
252        span.parent_id
253            .map(|id| VmValue::Int(id as i64))
254            .unwrap_or(VmValue::Nil),
255    );
256    d.insert("kind".into(), VmValue::String(Rc::from(span.kind.as_str())));
257    d.insert("name".into(), VmValue::String(Rc::from(span.name.as_str())));
258    d.insert("start_ms".into(), VmValue::Int(span.start_ms as i64));
259    d.insert("duration_ms".into(), VmValue::Int(span.duration_ms as i64));
260
261    if !span.metadata.is_empty() {
262        let meta: BTreeMap<String, VmValue> = span
263            .metadata
264            .iter()
265            .map(|(k, v)| (k.clone(), crate::stdlib::json_to_vm_value(v)))
266            .collect();
267        d.insert("metadata".into(), VmValue::Dict(Rc::new(meta)));
268    }
269
270    VmValue::Dict(Rc::new(d))
271}
272
273/// Generate a formatted summary of all spans.
274pub fn format_summary() -> String {
275    let spans = peek_spans();
276    if spans.is_empty() {
277        return "No spans recorded.".into();
278    }
279
280    let mut lines = Vec::new();
281    let total_ms: u64 = spans
282        .iter()
283        .filter(|s| s.parent_id.is_none())
284        .map(|s| s.duration_ms)
285        .sum();
286
287    lines.push(format!("Trace: {} spans, {total_ms}ms total", spans.len()));
288    lines.push(String::new());
289
290    // Build tree structure
291    fn print_tree(spans: &[Span], parent_id: Option<u64>, depth: usize, lines: &mut Vec<String>) {
292        let children: Vec<&Span> = spans.iter().filter(|s| s.parent_id == parent_id).collect();
293        for span in children {
294            let indent = "  ".repeat(depth);
295            let meta_str = if span.metadata.is_empty() {
296                String::new()
297            } else {
298                let parts: Vec<String> = span
299                    .metadata
300                    .iter()
301                    .map(|(k, v)| format!("{k}={v}"))
302                    .collect();
303                format!(" ({})", parts.join(", "))
304            };
305            lines.push(format!(
306                "{indent}{} {} {}ms{meta_str}",
307                span.kind.as_str(),
308                span.name,
309                span.duration_ms,
310            ));
311            print_tree(spans, Some(span.span_id), depth + 1, lines);
312        }
313    }
314
315    print_tree(&spans, None, 0, &mut lines);
316    lines.join("\n")
317}
318
319#[cfg(test)]
320mod tests {
321    use super::*;
322
323    #[test]
324    fn test_span_collector_basic() {
325        let mut c = SpanCollector::new();
326        let id = c.start(SpanKind::Pipeline, "main".into());
327        assert_eq!(id, 1);
328        assert_eq!(c.current_span_id(), Some(1));
329        c.end(id);
330        assert_eq!(c.current_span_id(), None);
331        assert_eq!(c.spans().len(), 1);
332        assert_eq!(c.spans()[0].name, "main");
333        assert_eq!(c.spans()[0].parent_id, None);
334    }
335
336    #[test]
337    fn test_span_parent_child() {
338        let mut c = SpanCollector::new();
339        let parent = c.start(SpanKind::Pipeline, "main".into());
340        let child = c.start(SpanKind::FnCall, "helper".into());
341        c.end(child);
342        c.end(parent);
343        assert_eq!(c.spans().len(), 2);
344        assert_eq!(c.spans()[0].parent_id, Some(parent));
345        assert_eq!(c.spans()[1].parent_id, None);
346    }
347
348    #[test]
349    fn test_span_metadata() {
350        let mut c = SpanCollector::new();
351        let id = c.start(SpanKind::LlmCall, "gpt-4".into());
352        c.set_metadata(id, "tokens", serde_json::json!(100));
353        c.end(id);
354        assert_eq!(c.spans()[0].metadata["tokens"], serde_json::json!(100));
355    }
356
357    #[test]
358    fn test_noop_when_disabled() {
359        set_tracing_enabled(false);
360        let id = span_start(SpanKind::Pipeline, "test".into());
361        assert_eq!(id, 0);
362        span_end(id); // should not panic
363    }
364}