主题
Runtime Tracing
定位
本文件定义 runtime 内部 Agent Tracer 的逻辑 API,不绑定具体语言签名或 transport。
TypeScript、Python 或其他 runtime 实现 MAY 暴露不同语法糖,但外部语义必须收敛到同一套 tracing 能力。
逻辑对象
StartSpanOptions- 见
schemas/common/tracing.json#/$defs/StartSpanOptions
- 见
TraceContext- 见
schemas/common/tracing.json#/$defs/TraceContext
- 见
PropagationCarrier- 见
schemas/common/otel.json#/$defs/PropagationCarrier
- 见
AgentSpan- 见
schemas/resources/agent-span.json
- 见
TraceEvent- 见
schemas/resources/trace-event.json
- 见
TraceSummary- 见
schemas/resources/trace-summary.json
- 见
TracerConfig- 见
schemas/common/tracing.json#/$defs/TracerConfig
- 见
GenAIObservation- 见
schemas/common/otel.json#/$defs/GenAIObservation
- 见
标准能力面
startSpan(options) -> SpanController- 创建一个需要手动结束的
runningspan
- 创建一个需要手动结束的
wrap(options, fn) -> T | Promise<T>- 自动开始、执行并结束 span
wrapChild(options, fn) -> T | Promise<T>- 复用当前上下文,明确表达创建子 span 的意图
wrapDetached(options, fn) -> T | Promise<T>- 复用当前 trace,但不写入
parentSpanId
- 复用当前 trace,但不写入
endSpan(spanId, status, error?) -> AgentSpan | null- 通过 span 标识手动结束一个活动 span
getTraceContext() -> TraceContext | null- 获取当前上下文中的最小 trace 传播信息
injectContext(carrier, format?) -> PropagationCarrier- 将当前上下文注入 header、text-map 或 environment carrier
extractContext(carrier, format?) -> TraceContext | null- 从外部 carrier 解析 trace 上下文
withContext(context, fn) -> T | Promise<T>- 在指定远端或显式上下文下执行回调
injectToLogger() -> void- 安装 trace 到日志格式化器或 logger context 的注入器
recordEvent(spanId, event) -> TraceEvent | null- 为某个活动 span 追加结构化事件
recordGenAI(spanId, details) -> TraceEvent | null- 记录 LLM / GenAI 输入输出详情,默认映射到
gen_ai.client.inference.operation.details
- 记录 LLM / GenAI 输入输出详情,默认映射到
destroy(errorMessage?) -> void- 释放 tracer,并结束保留中的内部 span 或缓冲导出器
SpanController 合约
spanIdrootSpanIdgetSpan()setSpanType(spanType)setMetadata(partialMetadata)setAttributes(partialAttributes)addEvent(name, attributes?, payload?)recordGenAIDetails(details)end(status, error?)
实现 MAY 提供更多 helper,但不得削弱上述标准能力。
语义要求
wrapMUST在回调成功时结束为success,抛错时结束为failedwrapChild在没有活动上下文时SHOULD报错或显式降级为wrap,实现必须固定一种行为并可测试wrapDetachedMUST NOT继承parentSpanId,但SHOULD保留traceId与rootSpanIdendSpan对未知或已结束 spanMUST返回空值或 no-op,不得重复导出getTraceContext返回值MUST至少包含traceId和spanIdinjectContext/extractContextSHOULD默认兼容traceparent、tracestate和baggagewithContextMUST在回调期内让wrap/startSpan看到新的父上下文recordEventMUST记录到当前 span 或指定 span,不得生成脱离 trace 的孤儿事件recordGenAIMAY将摘要投影到 span attributes,将大内容体投影到 span events 或外部 blob
语言绑定
- TypeScript
MAY暴露位置参数简写:startSpan(spanName, extra)wrap(spanName, fn, extra)
- Python
MAY暴露异步别名:awrapawrap_childawrap_detached
这些差异属于语言层绑定,不影响逻辑契约。
日志注入
injectToLoggerSHOULD注入:traceIdspanIdrootSpanIdrunId
- logger 注入
MUST是只增不改的上下文增强,不得破坏调用方原有日志字段
GenAI 语义
- LLM / agent 调用
SHOULD使用spanKind=client或等价语义来表达对远端模型服务的调用 - 对于 prompt、message、tool call、tool result、completion、usage 等详情,平台
SHOULD提供两层表达:- 轻量摘要放在
otel.attributes/genAI - 大内容体放在
TraceEvent.payload
- 轻量摘要放在
recordGenAI产生的事件名默认是gen_ai.client.inference.operation.details- 平台
MUST支持contentCaptureMode,以便在none、redacted、span_attributes、span_events、external_blob之间治理内容落点
Context Propagation
- runtime
SHOULD同时支持:- HTTP header carrier
- 通用 text-map carrier
- 子进程 / shell environment carrier
- baggage
MUST视为受治理的附加上下文,不得默认承载 secret 或大 payload
导出与读面
- runtime 内部 tracer
MUST以AgentSpan作为统一逻辑对象 - completed span
MUST通过 trace-export.asyncapi.yaml 描述的稳定协议导出 - span event 与 GenAI operation details
MUST能通过同一 trace export 协议导出 - 读面 trace 与 span 查询
MUST与导出协议共享同一份 schema
实现绑定提示
- 不同语言绑定
MAY在同步 / 异步命名、参数顺序和 helper API 上存在差异,但不得改变逻辑契约。 - 实现
MAY引入内部 housekeeping span、vendor span 或 adapter 事件;对外稳定接口必须仍以本规范 schema 为准。 - 如果 exporter 先产出 vendor-specific 或 runtime-specific 事件形状,
MUST在进入平台 trace plane 前归一化为稳定契约。