Add the per-request trace recorder

docsgpt.tracing records agent, LLM, tool, retrieval and embedding spans in
memory and writes the finished trace once. Container spans nest by a
per-thread stack so suspended generators cannot corrupt the tree; open
spans are cancelled at flush and previews are bounded and redacted.
This commit is contained in:
arc53-machine committed 2026-09-23 17:19:54 +01:00
1 parent f3f817b1e8
commit 3f45d019b0
6 files changed
+1124

No files matched your search

+87
View File
@@ -0,0 +1,87 @@
"""Per-request execution traces.
Records agent runs, LLM calls, tool calls, retrieval and embeddings as a tree
of timed spans. A finished trace is stored in ``request_traces`` (rendered as
a waterfall in the Logs UI) and replayed as OpenTelemetry GenAI spans when an
OTel SDK is configured. See ``docs/content/Deploying/Observability.mdx``.
Typical use::
trace = tracing.start_trace(source="stream", request_id=request_id)
with tracing.activate(trace):
with tracing.span(tracing.KIND_TOOL, "execute_tool search") as s:
s.preview("arguments", args)
...
tracing.flush(trace)
Every call is a no-op when no trace is active.
"""
from docsgpt.tracing.core import (
BINDABLE_IDS,
CONTAINER_KINDS,
KIND_AGENT,
KIND_EMBEDDING,
KIND_GUARDRAIL,
KIND_LLM,
KIND_RERANK,
KIND_RETRIEVAL,
KIND_SEARCH,
KIND_STEP,
KIND_TOOL,
NOOP_SPAN,
STATUS_CANCELLED,
STATUS_DENIED,
STATUS_ERROR,
STATUS_OK,
STATUS_PAUSED,
STATUS_PENDING,
STATUS_SKIPPED,
Span,
Trace,
activate,
bind,
bind_if_unset,
current_trace,
mark_content_blocked,
span,
start_span,
start_trace,
wrap,
)
from docsgpt.tracing.sink import discard, flush
__all__ = [
"BINDABLE_IDS",
"CONTAINER_KINDS",
"KIND_AGENT",
"KIND_EMBEDDING",
"KIND_GUARDRAIL",
"KIND_LLM",
"KIND_RERANK",
"KIND_RETRIEVAL",
"KIND_SEARCH",
"KIND_STEP",
"KIND_TOOL",
"NOOP_SPAN",
"STATUS_CANCELLED",
"STATUS_DENIED",
"STATUS_ERROR",
"STATUS_OK",
"STATUS_PAUSED",
"STATUS_PENDING",
"STATUS_SKIPPED",
"Span",
"Trace",
"activate",
"bind",
"bind_if_unset",
"current_trace",
"discard",
"flush",
"mark_content_blocked",
"span",
"start_span",
"start_trace",
"wrap",
]