Repository navigation
控制面结构化日志和 /metrics,部署文档写清怎么接 Loki 和 Prometheus (#29) - #120
Merged
Merged
Conversation
…怎么接 Loki 和 Prometheus (#29) Co-Authored-By: Claude Opus 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_01LiP6qRYYXznuykpeTFtrZC
onError 原来记 c.req.path,/api/hooks/<token> 出错时凭据会原样写进 error 日志。改记 hono/route 的 routePath(c, -1),中间件里抛的也落到具体路由上。补一条用例:webhook 开运行记录失败回 500,断言日志里没有 token。 Co-Authored-By: Claude Opus 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_01Be3SZps2JjqvyrJ1vbLmni
joesmart
force-pushed
the
joesmart/ops-structured-logs
branch
from
October 24, 2026 20:07
f8819a8 to
040516c
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
改了什么
结构化日志
packages/control-plane/src/log.ts,不引新依赖。每行一条 JSON:time、level、msg,再带上下文字段runId、computerId、groupId、botId(有哪个带哪个)和各处自己的字段。LOG_FORMAT=pretty输出HH:MM:SS.mmm LEVEL msg k=v,错误另起几行写 message 和 stack。pnpm dev给控制面默认设LOG_FORMAT=pretty,生产不设就是 JSON;写了别的值启动报LOG_FORMAT 只能是 json 或 pretty并以 1 退出。src/里除了src/scripts/*(给人复制的 CLI 输出)以外,console.*全部换成log.*。原来打完整 error 的地方,err带 name、code、message、stack;原来只记errorKind的地方(报文、牌桌、例行这类异常原文可能夹着用户正文的),改用concealed(error),只记 name、code 和 stack 里的at行,隐私边界不变。Bearer xxx换成Bearer [已隐去],UPSTREAM_API_KEY、METRICS_TOKEN的原值换成[已隐去]。电脑 token、网关 key、登录 cookie 本来就不进日志。指标
GET /metrics,Prometheus 文本格式。只有设了METRICS_TOKEN才开,要带Authorization: Bearer <token>,错了回 401,没设回 404。baize_computers_online、baize_runs{state=running|unsettled|settled}、baize_gateway_responses_total{code}(网关/chat/completions回给电脑的每个状态码,含 模型网关加上游超时和按电脑限速,Pi 拿到 504/429 报错收尾不再挂住 (#46) #116 的 504 和 429)、baize_continuation_backlog(补扫积压)、baize_websocket_connections{kind=agent|push|live}。错误上报
docs/部署.md新开「日志和指标」一节,写了字段含义、Promtail 的 docker_sd 示例、指标表、Prometheus 抓取配置和告警示例;还写明 Caddy 不转发/metrics,要从内网直接抓 3001。其他
.env.example、docs/开发.md、deploy/README.md、deploy/remote/docker-compose.yml加了LOG_FORMAT、METRICS_TOKEN。为什么
#29:控制面的日志原来是
console.error(文案, error)拼出来的散文,收集器按 run 或电脑查不了;也没有任何指标可抓。另外,旧代码在上游报错时会把上游回包的前 200 字原样写进日志,上游要是在回包里回显了 Authorization,UPSTREAM_API_KEY就进了日志(下面「main 上的证据」)。怎么验证
新加的用例:
test/metrics-endpoint.test.ts:没设 token 回 404;token 不带或错回 401,对了回 200,在线电脑数和 WebSocket 数对得上;派出一个 run 记成 running,run_done结账后挪到 settled。test/gateway.test.ts的「运维」:504 和 429 按状态码记进/metrics;上游把收到的 Authorization 原样回显,日志里既没有网关 key 也没有上游 key。test/process.test.ts:不设LOG_FORMAT时真进程每行都是合法 JSON,单聊里交上来的复盘被丢掉,那一行带runId、computerId、groupId、botId,也不带复盘正文;LOG_FORMAT=pretty输出人读的一行;写了别的值以 1 退出。msg和字段,语义不变。main 上的证据:把
packages/control-plane/src换回 main,跑新用例,8 条里红 7 条(404 那条本来就绿,因为没有这个路由)。去掉密钥那条的 JSON 解析断言再跑一次,报上游 key: expected '模型上游返回 401,正文前 200 字:bad credentials:…' not to contain 'sk-fake-upstream',说明旧代码确实会把上游 key 写进日志。Closes #29
🤖 Generated with Claude Code
https://claude.ai/code/session_01LiP6qRYYXznuykpeTFtrZC