Observability API Reference¶
This page documents the complete API for Clearstone's observability and tracing system.
TracerProvider¶
The entry point for the tracing system.
from clearstone.observability import TracerProvider
provider = TracerProvider(db_path="traces.db")
tracer = provider.get_tracer("my_agent", version="1.0")
clearstone.observability.provider.TracerProvider
¶
A central provider that manages the lifecycle of the entire tracing system, including the storage backend, buffer, and individual tracers.
Tracer¶
Creates and manages spans.
clearstone.observability.tracer.Tracer
¶
The primary API for creating and managing spans within a trace. Supports both integrated mode (with external buffer) and legacy mode (internal buffer).
clear_buffer()
¶
Clears the in-memory buffer (legacy mode only).
get_buffered_spans()
¶
Returns a copy of the current in-memory span buffer (legacy mode only).
span(name, kind=SpanKind.INTERNAL, attributes=None)
¶
Creates a new span that is managed by a context manager.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name
|
str
|
A human-readable name for the operation (e.g., "agent.think"). |
required |
kind
|
SpanKind
|
The OTel-aligned kind of the span (e.g., SpanKind.CLIENT). |
INTERNAL
|
attributes
|
Optional[Dict[str, Any]]
|
A dictionary of initial attributes for the span. |
None
|
Returns:
| Type | Description |
|---|---|
SpanContextManager
|
A SpanContextManager to be used in a 'with' statement. |
Span Models¶
Span¶
Represents a single operation with timing and metadata.
clearstone.observability.models.Span
¶
Bases: BaseModel
The core data structure for a single traced operation, designed for high-fidelity capture and replay.
duration_ns
property
¶
Calculates the span duration in nanoseconds if the span is complete.
Trace¶
A complete execution flow (collection of spans).
trace = provider.trace_store.get_trace(trace_id)
print(f"Root: {trace.root_span.name}")
for span in trace.spans:
print(f" - {span.name}")
clearstone.observability.models.Trace
¶
Bases: BaseModel
A collection of spans for a complete agent execution.
SpanKind¶
Enum defining the type of operation.
from clearstone.observability import SpanKind
with tracer.span("api_call", kind=SpanKind.CLIENT):
pass
clearstone.observability.models.SpanKind
¶
Bases: str, Enum
OTel-aligned span kind categorization.
TraceStore¶
Query and analyze stored traces.
trace_store = provider.trace_store
traces = trace_store.list_traces(limit=100)
trace = trace_store.get_trace(trace_id)
clearstone.storage.sqlite.TraceStore
¶
Bases: BaseTraceStore
Manages the persistence of traces to a local SQLite database. This class handles database connections, schema creation, and writing data.
Storage Components¶
SpanBuffer¶
Asynchronous buffer for batching span writes.
clearstone.storage.sqlite.SpanBuffer
¶
Bases: BaseSpanBuffer
An in-memory, thread-safe buffer for spans that flushes them to a writer periodically or when the buffer is full. This decouples span creation from the I/O of writing to disk.