Quick Start
User Interaction Flow
TraceContext
Main tracing context that manages spans.Methods
| Method | Signature | Description |
|---|---|---|
new(name) | fn new(impl Into<String>) -> Self | Create trace context |
elapsed() | fn elapsed(&self) -> Duration | Time since trace start |
start_span(name, kind) | fn start_span(&mut self, impl Into<String>, SpanKind) -> String | Start span, returns ID |
end_span(id) | fn end_span(&mut self, &str) | End span by ID |
current_span_id() | fn current_span_id(&self) -> Option<&str> | Get current span ID |
get_span(id) | fn get_span(&self, &str) -> Option<&Span> | Get span by ID |
add_event(name) | fn add_event(&mut self, impl Into<String>) | Add event to current span |
set_attribute(key, value) | fn set_attribute(&mut self, impl Into<String>, impl Serialize) | Set attribute on current |
spans() | fn spans(&self) -> &[Span] | Get all spans |
span_count() | fn span_count(&self) -> usize | Number of spans |
to_json() | fn to_json(&self) -> Value | Export as JSON |
SpanKind
Type of operation being traced.| Kind | Use Case |
|---|---|
Internal | General internal operations |
Llm | LLM API calls |
Tool | Tool executions |
Agent | Agent message processing |
Workflow | Multi-agent workflows |
Memory | Memory read/write |
Network | External API calls |
SpanStatus
Status of a span.Span
Represents a unit of work.Methods
| Method | Signature | Description |
|---|---|---|
new(trace_id, name, kind, offset) | fn new(...) -> Self | Create span |
with_parent(id) | fn with_parent(self, impl Into<String>) -> Self | Set parent |
set_attribute(key, value) | fn set_attribute(&mut self, impl Into<String>, impl Serialize) | Add attribute |
add_event(event) | fn add_event(&mut self, SpanEvent) | Add event |
end(offset) | fn end(&mut self, Duration) | End span |
set_error(message) | fn set_error(&mut self, impl Into<String>) | Mark as error |
is_ended() | fn is_ended(&self) -> bool | Check if ended |
SpanEvent
Events within a span.Methods
| Method | Signature | Description |
|---|---|---|
new(name, timestamp) | fn new(impl Into<String>, Duration) -> Self | Create event |
with_attribute(key, value) | fn with_attribute(self, impl Into<String>, impl Serialize) -> Self | Add attribute |
TraceExporter Trait
Export traces to various backends.Built-in Exporters
| Exporter | Description |
|---|---|
ConsoleExporter | Prints traces to stdout |
Example: Full Tracing
Best Practices
Use appropriate SpanKind
Use appropriate SpanKind
Categorize spans correctly for better visualization and filtering.
Add meaningful attributes
Add meaningful attributes
Include model names, token counts, user IDs - data useful for debugging.
Use events for milestones
Use events for milestones
Events mark important points within a span without creating new spans.
Always end spans
Always end spans
Use try/finally patterns or RAII to ensure spans are always closed.

