mainspring-otel
OpenTelemetry 导出 + 成本统计 + 会话回放器(T4-4)。把 core 的 RunEvent 事件流映射成
run / llm / tool / guardrail span 树,导出到 Jaeger(OTLP HTTP),并支持按 run_id
重放 JSONL 事件日志。
用法
from mainspring_otel import otlp_sink
runner = Runner(sink=otlp_sink()) # 默认导到 http://127.0.0.1:4318/v1/traces
result = await runner.run(agent, "开始")
await runner.sink.aclose() # force_flush:批量处理器这时才真正发出
端点优先级:endpoint 参数 → OTEL_EXPORTER_OTLP_TRACES_ENDPOINT →
OTEL_EXPORTER_OTLP_ENDPOINT → compose 默认 http://127.0.0.1:4318/v1/traces。
测试 / 冒烟用内存收集器:
from mainspring_otel import in_memory_sink
sink, exporter = in_memory_sink()
...
spans = exporter.get_finished_spans()
多 sink 合流(例如一边进 Jaeger 一边做成本汇总):
from mainspring_otel import CostTracker, FanoutSink, PriceTable
sink = FanoutSink([otlp_sink(), CostTracker(PriceTable.from_csv("prices.csv"))])
成本表按 model 计价(美元 / 百万 token):
from mainspring_otel import PriceTable
prices = PriceTable.from_csv("prices.csv") # 列:model,input_per_1m,output_per_1m,
# cache_read_per_1m,cache_write_per_1m
tracker = CostTracker(prices)
await tracker.emit(event) # 只消费 llm_call
summary = tracker.summary() # .entries / .total_usd / .unpriced
回放 CLI(不依赖 OTel,直接把 JSONL 重新喂给 ConsoleSink):
uv run mainspring-replay events.jsonl --run-id run_abc # 只回放该 run
uv run mainspring-replay events.jsonl --run-id run_abc --children # 连子 run 一起
span 映射
| 事件 | span | 关键属性 |
|---|---|---|
run_started / run_finished / run_failed |
run <agent> |
mainspring.run_id / 总 usage / finish_reason / status |
llm_call |
chat <model> |
gen_ai.request.model / gen_ai.usage.* / attempts / 可选 cost_usd |
tool_call |
tool <name> |
call_id / is_error(ERROR status) / result_length |
guardrail |
guardrail <name> |
verdict / phase / reason |
hook / model_fallback |
hook <name> / fallback a->b |
全 payload 逐项落属性 |
其余(如 context_compact) |
挂到 run span 的 event | payload 逐项 |
所有子 span 带 mainspring.run_id / step_id / seq,据此把 span 与事件流对齐。
设计要点
- 显式父子而非隐式当前 span:Runner 不压栈,每个子 span 用
trace.set_span_in_context(parent)指定父上下文,并发 run 不会串线。 - parent_run_id 建树:workflow 的根 run 是虚拟的,子 run 首事件会先建一个
mainspring.synthetic=True的占位根 span,Jaeger 里整棵调用树仍然完整。 - redaction 默认开启:发 span 前统一过 core
redact_payload,密钥 / SecretStr 不进属性; 需要原样导出时显式redact=False。 - 时间轴用事件 ts:llm / tool 事件在完成后才发,子 span 为瞬时点;根 span 覆盖整段 run。
- 回放器复用 ConsoleSink:回放输出与线上日志逐行一致,不另造一套格式。
验收
docker compose up -d jaeger后跑uv run --all-extras python scripts/smoke_otel_jaeger.py, 断言一次 run 的完整 span 树、run_id+step_id关联、服务名出现;pytest tests/test_otel.py覆盖 span 树 / redaction / 成本汇总 / 回放与 CLI;- core 零反向依赖由 import-linter 守护。
Metadata
Release files for mainspring-otel 0.1.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| mainspring_otel-0.1.0.tar.gz | 13.3 kB | With provenance | Details for mainspring_otel-0.1.0.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| mainspring_otel-0.1.0-py3-none-any.whl | Python 3 | none | any | Details for mainspring_otel-0.1.0-py3-none-any.whl |
Total release size: 29.5 kB
Release files / mainspring_otel-0.1.0.tar.gz
| Download URL | mainspring_otel-0.1.0.tar.gz |
|---|---|
| Size | 13.3 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
65af9ea3fdfe4ad488fc92a136e61f31b37891f55a2ebada8533733027a61768
|
|
BLAKE2b-256 checksum How to use checksums |
ca821c1eb9af6cf05ddf3e1ea5b7b34215d04fc3fb6f4bcb87051c24b08f83bb
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/7.0.0 CPython/3.13.16
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Dec 6, 2026.
Transparency logRelease files / mainspring_otel-0.1.0-py3-none-any.whl
| Download URL | mainspring_otel-0.1.0-py3-none-any.whl |
|---|---|
| Size | 16.2 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
970c37c3a96946bcaaaf0acd35278b551ea17e3c6b3eeddf8936fa47fc4f6071
|
|
BLAKE2b-256 checksum How to use checksums |
40e4458e338bd1636b56ab586aa1d05807d61ae4a889af2aa85fc83d2f7dd255
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/7.0.0 CPython/3.13.16
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Dec 6, 2026.
Transparency logSecurity for mainspring-otel 0.1.0
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
Full provenance
Both files in this release have been uploaded with provenance metadata.
Attestation for mainspring_otel-0.1.0.tar.gz
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Dec 6, 2026.
Transparency logA note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.