Skip to main content

lc_callbacks/tracing/
mod.rs

1//! Agent observability / deep tracing system
2//!
3//! Structured tracing with parent-child span trees, RAII span guards,
4//! and pluggable backends (in-memory, console, OpenTelemetry).
5//!
6//! # Quick Start
7//!
8//! ```rust,ignore
9//! use lc_callbacks::tracing::{
10//!     Tracer, InMemoryTracingBackend, SpanKind,
11//! };
12//! use std::sync::Arc;
13//!
14//! let backend = Arc::new(InMemoryTracingBackend::new());
15//! let tracer = Arc::new(Tracer::new(backend.clone()));
16//!
17//! // Start a root span
18//! let root = tracer.start("my_chain", SpanKind::Chain);
19//!
20//! // Start a child span (inherits parent from tracer context)
21//! let llm = tracer.start_child("llm_call", SpanKind::Llm);
22//! drop(llm); // ends the child span
23//!
24//! drop(root); // ends the root span
25//!
26//! // Inspect recorded spans
27//! let spans = backend.spans();
28//! ```
29
30/// Backend implementations for persisting/processing trace spans.
31pub mod backend;
32/// Span types and the span tree.
33pub mod span;
34/// The tracer, span guards, and span-stack helpers.
35pub mod tracer;
36
37#[cfg(test)]
38mod tests;
39
40// Re-export all public types to preserve the public API.
41#[cfg(feature = "opentelemetry")]
42pub use backend::OtelTracingBackend;
43pub use backend::{ConsoleTracingBackend, InMemoryTracingBackend};
44pub use span::{
45    aggregate_cost, SpanId, SpanKind, SpanStatus, SpanTokenUsage, TraceNode, TraceSpan,
46};
47pub use tracer::{clear_span_stack, init_task_span_stack, SpanGuard, Tracer};
48
49/// Backend for persisting/processing trace spans.
50pub trait TracingBackend: Send + Sync {
51    /// Called when a span starts.
52    fn start_span(&self, span: &TraceSpan);
53    /// Called when a span ends.
54    fn end_span(&self, span: &TraceSpan);
55    /// Flush any buffered spans to storage.
56    fn flush(&self);
57}