feat(logs): 详情彻底不截断(捕获 4MiB / 入库 8MiB)+ 实时页按需取全文

- 请求体捕获上限 128KiB → 4MiB(CALLLOG_BODY_MAX);在途缓存条数 128 → 64 控内存
- 入库上限 256KiB → 8MiB(CALLLOG_CONTENT_MAX)
- 新增 GET /logs/entry/{id}:本人或管理员可取该条完整提示词/回复(实时页展开时调用)
- 实时列表仍给 8KB 预览 + body_len/resp_len,展开替换为全文并显示「完整 N 字」
- 实测 285KiB 提示词完整落库与返回
- 版本 2.9.11 → 2.9.12
This commit is contained in:
jidekin committed 2026-10-23 16:01:24 +08:00
1 parent 5643824422
commit 7aa9032829
6 files changed
+98 -12

No files matched your search

+24
View File
@@ -1,5 +1,29 @@
# CHANGELOG
## 2026-10-23 · 调用日志详情彻底不截断(2.9.11 → 2.9.12)
老板:「调用日志的详情内容都不要截断记录,都要显示完整的,像提示词、回复这些」。
上一轮(2.9.2)把上限提到 128KiB / 256KiB,但**真实业务会超** —— 代码上下文的提示词
上百万字符很常见(实测客户有 8.6 万 token ≈ 300KB 的请求),仍会被截断。本轮彻底放开:
| 环节 | 之前 | 现在 |
|---|---|---|
| 请求体捕获 | 128 KiB | **4 MiB**(`CALLLOG_BODY_MAX`)|
| 入库 | 256 KiB | **8 MiB**(`CALLLOG_CONTENT_MAX`)|
| 内存占用控制 | 在途 128 条 | 在途 **64 条**(4MiB × 64 = 最坏 256MiB,日常远低)|
**实时页的完整内容**:列表为了轻量只给前 8KB 预览(200 条 × 几百 KB 会拖垮接口),
新增 `GET /logs/entry/{id}` —— 展开某条时按需取**完整**提示词与回复:
- 权限:**本人的记录**或管理员,其余 403
- 前端展开时自动调用,并显示「完整 N 字」;加载中显示进度文案
**实测**:285 KiB(292,168 字)的提示词 →
库里 292,168 字**无截断标记**、`/logs/calls/{id}` 全文返回、`/logs/entry/{id}` 全文返回。
测试记录已清理。
> 更早(2.9.2 之前)写的记录在入库时就已被截断,无法回溯。
## 2026-10-23 · 资产卡片模型名/日期分两行 + 命中率口径说明(2.9.10 → 2.9.11)
老板:「模型名称和日期换行展示吧,不要挤一起了,然后日志的实时里为什么命中缓存率一直都是 0」。
+2 -2
View File
@@ -19,7 +19,7 @@
- `CALLLOG_DB` 默认 `data/calllog.db`
- `CALLLOG_RETENTION_DAYS` 默认 90
- `CALLLOG_QUEUE_MAX` 默认 10000
- `CALLLOG_CONTENT_MAX` 默认 262144(=256KiB,提示词/回复各自上限;详情页不截断)
- `CALLLOG_CONTENT_MAX` 默认 8388608(=8MiB,提示词/回复各自上限;详情页完整显示)
- `CALLLOG_DISABLED=1` 整体关闭(不回退任何现有行为)
"""
@@ -40,7 +40,7 @@ _ROOT = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
DB_PATH = os.environ.get("CALLLOG_DB") or os.path.join(_ROOT, "data", "calllog.db")
RETENTION_DAYS = int(os.environ.get("CALLLOG_RETENTION_DAYS") or 90)
QUEUE_MAX = int(os.environ.get("CALLLOG_QUEUE_MAX") or 10000)
CONTENT_MAX = int(os.environ.get("CALLLOG_CONTENT_MAX") or 262144)
CONTENT_MAX = int(os.environ.get("CALLLOG_CONTENT_MAX") or 8388608)
DISABLED = os.environ.get("CALLLOG_DISABLED", "") not in ("", "0", "false", "False")
BATCH = 200 # 攒够多少条提交一次
+31 -5
View File
@@ -162,9 +162,10 @@ def _setup_logging() -> None:
logging.basicConfig(level=logging.INFO, format="%(asctime)s [%(name)s] %(levelname)s: %(message)s")
log = logging.getLogger("api-gateway")
# 调用日志的内容上限(2026-10-23):提示词/回复按此长度截断,默认 128KiB(≈32k tokens)。
# 详情页要求「不截断」,实际上 128KiB 已覆盖绝大多数对话;要真的无限就把这个值调大。
_CONTENT_MAX = int(__import__("os").environ.get("CALLLOG_BODY_MAX") or 131072)
# 调用日志的内容上限(2026-10-23,2.9.12 再放宽):默认 **4 MiB**(≈100 万 token 的文本)。
# 老板要求「详情内容都不要截断」—— 4 MiB 对任何正常对话都等于不截断;
# 内存占用靠下面的 BODY_BY_RID 条数上限(64 条在途)兜住,最坏 256 MiB、日常远低于此。
_CONTENT_MAX = int(__import__("os").environ.get("CALLLOG_BODY_MAX") or 4194304)
def _config_mtime() -> float:
@@ -355,7 +356,7 @@ _docs_on = config.server.docs_enabled # 安全默认关(report: /docs、/ope
# 2026-09-30: 2.6.1 → 2.7.0(按老板要求整个删除「火山方舟 Coding Plan」供应商:配置/模型/代码/文档/前端下拉/环境变量全清)。
# 2026-09-30: 2.7.0 → 2.7.1(界面提示白话化:熔断器→供应商自动保护、引擎→运行环境、slug→标识;删 120 个未引用死键)。
# ★ 版本号纪律:每次改动都要往上加,小改动加小版本号、大改动加大版本号(README 有专章)。
VERSION = "2.9.11"
VERSION = "2.9.12"
app = FastAPI(
title="API Gateway",
@@ -504,7 +505,8 @@ async def in_flight_tracker(request: Request, call_next):
raw = await request.body()
# 调用日志要看完整提示词:放宽到 128KiB(≈32k tokens),并只保留最近 N 条
BODY_BY_RID[rid] = raw[:_CONTENT_MAX].decode("utf-8", "replace")
if len(BODY_BY_RID) > 128:
# 上限放宽到 4MiB 后,条数收紧到 64(并发的在途请求远小于此),控住内存
if len(BODY_BY_RID) > 64:
BODY_BY_RID.clear()
except Exception:
BODY_BY_RID.pop(rid, None)
@@ -3192,6 +3194,26 @@ def _logs_for_account(account: str, n: int = 200) -> list[dict]:
return _attach_polls(rows)
@app.get("/logs/entry/{call_id}")
async def logs_entry(call_id: int, request: Request, _auth=Depends(require_auth)):
"""取某条日志的**完整**内容(提示词 + 回复,不截断)。
实时页签的列表为了轻量只给前 8KB 预览,展开时调这里拿全文。
权限:**本人的记录**或管理员;其他人一律 403。
"""
row = calllog.store.get(call_id)
if not row:
raise HTTPException(404, f"日志不存在: {call_id}")
me = request.state.account
if getattr(request.state, "role", "") != "admin" and \
me not in (row.get("account"), row.get("caller")):
raise HTTPException(403, "无权查看他人的调用日志")
return {"id": row.get("id"), "prompt": row.get("prompt") or "",
"completion": row.get("completion") or "",
"prompt_len": len(row.get("prompt") or ""),
"completion_len": len(row.get("completion") or "")}
@app.get("/logs/recent")
async def logs_recent(request: Request, n: int = 200, _auth=Depends(require_auth)):
"""「实时」页签的数据源(2026-10-23 起改为读库)。
@@ -3224,8 +3246,12 @@ async def logs_recent(request: Request, n: int = 200, _auth=Depends(require_auth
"stream": bool(it.get("stream")),
"status": it.get("status"),
"ms": it.get("latency_ms"),
# 列表只给前 8KB 预览(200 条 × 几百 KB 会拖垮接口);
# 展开时前端调 GET /logs/entry/{id} 取完整内容(见该端点注释)
"body": (it.get("prompt") or "")[:8000],
"response": (it.get("completion") or "")[:8000],
"body_len": len(it.get("prompt") or ""),
"resp_len": len(it.get("completion") or ""),
"tokens": {"prompt_tokens": it.get("tok_prompt") or 0,
"completion_tokens": it.get("tok_completion") or 0,
"cached_tokens": it.get("tok_cached") or 0,
+2
View File
@@ -209,6 +209,8 @@ export default {
tokReasoning: 'reasoning',
cacheHit: 'cache hit',
cacheHint: 'Cache misses are normal for the first request, short prompts, or changed prefixes',
loadingFull: 'Loading full content…',
fullLen: '{n} chars total',
},
calllogs: {
title: 'Call log (persisted)',
+2
View File
@@ -209,6 +209,8 @@ export default {
tokReasoning: '推理',
cacheHit: '缓存命中率',
cacheHint: '首次请求、短提示词、或前缀有变化时,上游不会命中缓存,命中率为 0 属正常',
loadingFull: '正在加载完整内容…',
fullLen: '完整 {n} 字',
},
calllogs: {
title: '调用日志(持久化)',
+37 -5
View File
@@ -100,8 +100,20 @@
<span class="tok-item tok-hit" :class="hitCls(row)" :title="t('logs.cacheHint')">{{ t('logs.cacheHit') }} <b>{{ hitRate(row) }}%</b></span>
</div>
</div>
<div class="detail-block"><label>{{ t('logs.request') }}</label><pre>{{ row.body || '—' }}</pre></div>
<div class="detail-block"><label>{{ t('logs.response') }}</label><pre>{{ row.response || '—' }}</pre></div>
<div class="detail-block">
<label>{{ t('logs.request') }}
<span v-if="row.fullLoading" class="sub"> · {{ t('logs.loadingFull') }}</span>
<span v-else-if="row.full && row.bodyLen" class="sub"> · {{ t('logs.fullLen', { n: row.bodyLen }) }}</span>
<span v-else-if="row.fullErr" class="err-text"> · {{ row.fullErr }}</span>
</label>
<pre>{{ row.body || '—' }}</pre>
</div>
<div class="detail-block">
<label>{{ t('logs.response') }}
<span v-if="row.full && row.respLen" class="sub"> · {{ t('logs.fullLen', { n: row.respLen }) }}</span>
</label>
<pre>{{ row.response || '—' }}</pre>
</div>
<div class="detail-block" v-if="row.polls && row.polls.length">
<label>{{ t('logs.polls', { n: row.polls.length }) }}</label>
<el-timeline class="poll-timeline">
@@ -178,9 +190,29 @@ function isOpen(row: any): boolean {
}
function toggle(row: any) {
const k = rowKey(row)
expandedKeys.value = expandedKeys.value.includes(k)
? expandedKeys.value.filter((x) => x !== k)
: [...expandedKeys.value, k]
const opening = !expandedKeys.value.includes(k)
expandedKeys.value = opening
? [...expandedKeys.value, k]
: expandedKeys.value.filter((x) => x !== k)
// 2026-10-23:列表只给前 8KB 预览,展开时按需取全文(详情不截断)
if (opening && row.id && !row.full) loadFull(row)
}
/** 展开时取这条日志的完整提示词与回复(不截断)。 */
async function loadFull(row: any) {
row.fullLoading = true
try {
const d = await api(`/logs/entry/${row.id}`)
row.body = d.prompt || ''
row.response = d.completion || ''
row.bodyLen = d.prompt_len || 0
row.respLen = d.completion_len || 0
row.full = true
} catch (e: any) {
row.fullErr = err(e)
} finally {
row.fullLoading = false
}
}
// 视频快照是否已经有结论(有结论就不再主动追轮询)