Observability API¶
observability ¶
Observability primitives for orqest agents.
Provides tracing (Span, Tracer, JSONTracer) and event-driven observability (AgentEvent, EventBus) with zero external dependencies.
EventBusPublishHook ¶
EventBusPublishHook(
bus: EventBus,
*,
agent_name: str = "unknown",
result_preview_chars: int = _RESULT_PREVIEW_CHARS,
)
Publish tool lifecycle to an :class:EventBus.
Structural ToolHook — implements before_tool, after_tool,
and on_error, so it satisfies
:class:orqest.hooks.ToolHook without inheriting from the Protocol.
Wire the hook to bus and tag every published event with
agent_name.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
bus
|
EventBus
|
The :class: |
required |
agent_name
|
str
|
Value used as |
'unknown'
|
result_preview_chars
|
int
|
Max characters from tool results
included in |
_RESULT_PREVIEW_CHARS
|
Source code in orqest/observability/event_bus_hook.py
before_tool
async
¶
Publish tool.before to the bus.
Source code in orqest/observability/event_bus_hook.py
after_tool
async
¶
after_tool(
tool_name: str,
args: dict[str, Any],
result: Any,
state: Any,
duration_ms: float,
) -> None
Publish tool.after to the bus.
Source code in orqest/observability/event_bus_hook.py
on_error
async
¶
Publish tool.error to the bus.
Source code in orqest/observability/event_bus_hook.py
AgentEvent
dataclass
¶
AgentEvent(
event_type: str,
agent_name: str,
timestamp: datetime = (lambda: datetime.now(tz=UTC))(),
data: dict[str, Any] = dict(),
span_id: str | None = None,
trace_id: str | None = None,
)
Event emitted during agent execution.
Carries the event type, originating agent, and an arbitrary data payload. Optionally linked to a trace span for correlation.
EventBus ¶
In-process pub/sub for AgentEvents.
Fire-and-forget: handler errors are logged at WARNING level and never re-raised, so a broken handler cannot disrupt agent execution. Supports both sync and async handlers.
Initialize with empty handler registries.
Source code in orqest/observability/events.py
subscribe ¶
subscribe_all ¶
unsubscribe ¶
Remove a handler for a specific event type.
Silently ignores handlers that are not registered.
Source code in orqest/observability/events.py
unsubscribe_all ¶
Remove a handler registered via :meth:subscribe_all.
Silently ignores handlers that are not registered.
Source code in orqest/observability/events.py
emit
async
¶
Dispatch an event to all matching handlers.
Calls type-specific handlers first, then global handlers. Each handler is invoked independently — a failure in one does not prevent others from running.
Source code in orqest/observability/events.py
JSONTracer ¶
Default tracer — stores spans in memory, exports to JSON.
Thread-safe for single-writer scenarios (typical async usage). No external dependencies.
Initialize with an empty span store.
Source code in orqest/observability/tracer.py
start_span ¶
Create and register a new span.
If a parent is provided, the child inherits its trace_id. Otherwise a new trace_id is generated (root span).
Source code in orqest/observability/tracer.py
end_span ¶
Mark a span as finished, computing its duration.
Merges any extra attributes into the span.
Source code in orqest/observability/tracer.py
get_spans ¶
export_json ¶
Serialize all spans to JSON-safe dicts.
Source code in orqest/observability/tracer.py
Span
dataclass
¶
Span(
trace_id: str,
span_id: str,
parent_span_id: str | None,
name: str,
agent_name: str,
started_at: datetime,
ended_at: datetime | None = None,
duration_ms: float | None = None,
status: Literal["ok", "error"] = "ok",
attributes: dict[str, Any] = dict(),
events: list[dict[str, Any]] = list(),
)
A single unit of work within a trace.
Spans form a tree via parent_span_id. A root span has parent_span_id=None and defines the trace_id that child spans inherit.