📊 agentprof 可视化指南 · 目录 用法 6 / 14
用法

db + ingest-otlp:存数据库 + 接入 OTLP

agentprof 默认每次 analyze 都要重新 parse JSONL —— 单 session 还好,跨 30 天几百个 session 就开始肉眼可见地慢。hybrid storage 让 cache 自动接管(dev 默认开),db 子命令族把 sessions 显式持久化到 store,ingest-otlpClaude Code / Codex 的 OTel SDK 直接 push session 进来 —— file-based 之外的第二条数据进入路径。

💾
Cache (默认)
Adapter → SQLite cache @ XDG_CACHE_HOME
🗄️
Store (显式)
agentprof db init --storage-path ...
📡
OTLP push
OTel SDK → :4317 (gRPC) or :4318 (HTTP) → SQLite
🔌 生活类比
grep 单文件升级到 SQLite + Prometheus push gateway —— 不再每次扫一遍 JSONL,不再要求 agent 必须先写 events.jsonl 才能被分析;Claude Code 跑着就把 spans 推进来,agentprof 在另一边实时聚合。

3 种数据流模式 — 一眼对照

同一份 session 数据,可以从三条路径进入 agentprof。不是互斥的 —— 大多数用户 cache 默认开 + 偶尔升级到 store + 团队场景上 OTLP

模式数据流命令
Cache(默认)Adapter → SQLite cache(XDG_CACHE_HOME)→ analyze 走 cacheagentprof analyze(隐式 cache 启用)
Store(显式持久化)Adapter → SQLite store(XDG_DATA_HOMEagentprof db init --storage-path ~/.local/share/agentprof/store.sqlite
OTLP pushClaude Code / Codex OTel SDK → gRPC :4317 or HTTP :4318 → agentprof-storageagentprof ingest-otlp --bind 127.0.0.1:4317

👇 三张卡片展开 db 子命令家族 / hybrid 概念(含流程图)/ OTLP 接入:① 为什么 · ② agentprof 怎么做 · ③ 其他选择

1 agentprof db {init,ingest,stats,prune,vacuum,export} — SQLite 存储管理 点击展开
🤔 为什么
repeated analyze 慢 —— 30 天窗口里 200 个 session、每个 JSONL 几 MB,重 parse 就是「每次跑都要等几秒」。需要一个持久化层把 parse 结果固化,下次跨 session 查询直接走 SQLite index,毫秒级响应
🛠 agentprof 怎么做
agentprof db 是 6 个子命令的家族:
  • init —— 在指定路径建空 store(schema migration 自动跑)
  • ingest —— 批量把 adapter 输出的 sessions 写进 store
  • stats —— 看 store 当前大小 / session 数 / 最早最晚时间
  • prune --since 30d —— 按时间窗口删除老 session(释放空间)
  • vacuum —— SQLite VACUUM 整理碎片
  • export —— 把 store 内容导出成 JSONL(迁移 / 备份)
配合 hybrid mode(见下一张卡片),dev 端基本不用动 store;CI / 团队场景才显式 init
🪜 其他选择
直接走 Adapter 不开 cache(--no-cache)—— 每次重 parse,适合「跑完即弃」的一次性诊断;或者「怀疑 cache 损坏」想强制重读 source-of-truth 的场景。生产 / 长期 trend 场景几乎总是要 store。
2 hybrid cache vs store — XDG path + ownership + 何时升级 点击展开
🤔 用户视角:cache 还是 store?
一句话决策:dev 本地 → cache(不用管,自动开);CI / 团队 / 长期 trend / OTLP push → store(显式 --storage-path)。两者 schema 完全相同,从 cache 升级到 store 不需要数据迁移:重新 analyze --storage-mode store 即可,旧 cache 文件可保留也可删。
🛠 怎么开
默认就是 cache,啥都不用做。想用 store:
agentprof db init --storage-path ~/.local/share/agentprof/store.sqlite
agentprof analyze --storage-mode store   # 后续命令显式带 --storage-mode store
也可以写进 ~/.config/agentprof/config.toml 一劳永逸:
[storage]
mode = "store"
path = "~/.local/share/agentprof/store.sqlite"
📂 文件落在哪
Cache$XDG_CACHE_HOME/agentprof/cache.sqlite(默认 ~/.cache/agentprof/,OS 可以随时清,agentprof 容忍丢)
Store$XDG_DATA_HOME/agentprof/store.sqlite(默认 ~/.local/share/agentprof/,用户拥有,agentprof 不主动动)。
OTLP receiver 写当前 mode 对应的单一 storage,由 --storage-mode / --storage-path 决定,默认 cache。
🔁 数据流(cache / store 通用)
events.jsonl Adapter compute_analysis SQLite
🪜 为什么这么设计
完整设计决策(XDG 命名背后的取舍 / dual-path read fan-out / dual-write 为何被否决)在 Wiki §5 「存储层 hybrid mode」详述,本课只覆盖「用户怎么选」。简短回答:cache 是「性能优化」,store 是「业务数据」,两者职责清晰不混 —— 见 ADR-0019
3 agentprof ingest-otlp — Claude Code / Codex OTel SDK 接入 点击展开
🤔 为什么
file-based 模式(read JSONL)只覆盖 Copilot CLI / 老 Claude 这类「写日志到磁盘」的 agent。Claude Code(新版)/ Codex 用 OTel SDK 是 native —— 它们已经会发 spans,agentprof 只需要在另一头听就行,不用让 agent 团队额外写「导出 events.jsonl」逻辑。
🛠 启动 receiver(agentprof 端)
# gRPC(默认,性能最好)
agentprof ingest-otlp --bind 127.0.0.1:4317 \
  --storage-path ~/.local/share/agentprof/store.sqlite

# HTTP/protobuf(防火墙穿透更友好)
agentprof ingest-otlp --bind 127.0.0.1:4318 --protocol http \
  --storage-path ~/.local/share/agentprof/store.sqlite

# 想加 bearer auth(生产建议)
agentprof ingest-otlp --bind 0.0.0.0:4317 \
  --auth-token-file ~/.config/agentprof/otlp-bearer.txt \
  --tls-cert ./server.pem --tls-key ./server.key
需要编译时开 otlp feature(拉 workspace dep tonic + prost + axum)。full 默认含。
⚙️ Agent 端(OTel SDK / Claude Code)配置
Claude Code / Codex SDK 都遵循 OTel 标准环境变量协议:
# Bash / zsh(gRPC 默认)
export OTEL_EXPORTER_OTLP_ENDPOINT=http://127.0.0.1:4317
export OTEL_EXPORTER_OTLP_PROTOCOL=grpc
export OTEL_SERVICE_NAME=claude-code
# 可选:bearer auth
export OTEL_EXPORTER_OTLP_HEADERS="authorization=Bearer $(cat ~/.config/agentprof/otlp-bearer.txt)"

# 之后跑 agent,spans 自动 push 到 agentprof
claude code "your task"

⚠️ Claude Code 当前 v1.x 通过 OTEL_* 环境变量配 OTLP。如果你跑的是自家 agent / 用了 OTel collector 链路(agent → collector → agentprof),collector 端把 agentprof 作为 exporter:

# otel-collector-config.yaml
receivers:
  otlp:
    protocols:
      grpc:
        endpoint: 0.0.0.0:4317

exporters:
  otlp/agentprof:
    endpoint: 127.0.0.1:4317
    tls:
      insecure: true

service:
  pipelines:
    traces:
      receivers: [otlp]
      exporters: [otlp/agentprof]
🛡 4 层防御 + 排错
详见 ADR-0022:Bearer 常时间比较(防时序攻击)/ 每信号大小上限 8/2/8 MiB(防 OOM)/ LRU eviction 1024 sessions(防内存撑爆)/ session.id 256-byte 上限(防 path injection)。
# 验证 receiver 在线
curl -v http://127.0.0.1:4318/v1/traces -X POST   # 期望 415 (no protobuf body)

# 看进了多少 session
agentprof db stats --storage-path ~/.local/share/agentprof/store.sqlite

# 看实时 receiver 日志
RUST_LOG=agentprof_storage::otlp=debug agentprof ingest-otlp ...
🪜 其他选择
自建 OTel Collector + ETL 到 agentprof —— 技术上行,但多一层进程 + 多一套配置,单机 / 小团队不值得。agentprof 内置 OTLP receiver 已够覆盖「5–50 个 dev 同时推」量级;超过这个量级再上正经 collector。

典型工作流:从 cache 升级到 store + OTLP

三步走,对应上面三张卡片:

# 1. dev 阶段 — 啥都不做,cache 自动开
agentprof analyze
agentprof list --since 7d

# 2. 想长期保留 / 跨机器同步 — 显式 store
agentprof db init --storage-path ~/.local/share/agentprof/store.sqlite
agentprof db ingest --since 30d
agentprof db stats

# 3. 接 Claude Code / Codex 实时数据 — OTLP receiver
agentprof ingest-otlp --bind 127.0.0.1:4317 \
  --storage-path ~/.local/share/agentprof/store.sqlite
# (另一边 Claude Code 配 OTEL_EXPORTER_OTLP_ENDPOINT=http://127.0.0.1:4317)

结语:用法章节完结

这是「用法」章节的最后一节(6/6)。学完之后你已经能用 agentprof 覆盖:单 session 分析(analyze)、跨 session 聚合(list / aggregate)、浏览器看板(serve)、持久化 + OTLP 接入(本节)。

接下来是 Wiki 章节(8 节),面向想深入原理 + 给项目贡献代码的中阶 / 开发者读者:火焰图算法、ROI 公式、tokenizer 选型、adapter 协议、SQLite schema、OTLP 防御、TUI 渲染、xtask 工具链 —— 每节对应一篇 ADR + 一组 source 链接。

📂 相关源码: agentprof-storage/db.rs  Db

📂 相关源码: agentprof-storage/query.rs  query_sessions_since

📂 相关源码: agentprof-storage/otlp/mod.rs  otlp