Skip to content

[任务] app-cla-server 接入 obs-sdk:结构化日志 + 指标并暴露 /metrics(#1938 / #2061)-的开发实现-app-cla-server部分 - #526

Merged
TangJia025 merged 6 commits into
reviewfrom
issue-2552-from-review
Oct 16, 2026
Merged

TangJia025 merged 6 commits into
reviewfrom
issue-2552-from-review

Conversation

@fly333sky

Copy link
Copy Markdown

背景

[任务] app-cla-server 接入 obs-sdk:结构化日志 + 指标并暴露 /metrics(#1938 / #2061)(app-cla-server)· 开发流水线 · 开发预览阶段(代码已推 + 预览已部署 + UT 已补;门禁/对抗由 PR CI 异步跑)

改动内容

fix(app-cla-server): 修复 corp_pdf_email.go 测试未走 do() 真实路径导致覆盖率不达标

问题

上一轮预览失败:TestCorpPDFEmailPathRecordsEmailMetric 直接调用 recordEmailSent() 而非通过 corp_pdf_email.do() 真实代码路径,导致 do() 和 genFile() 覆盖率 0%,增量覆盖率 10.8% < 80%。

改动

仅修改 worker/metrics_test.go(测试文件),无业务源码改动:

  1. 删除无效测试 TestCorpPDFEmailPathRecordsEmailMetric(仅调 helper 未走 do())
  2. 新增 fakePDFGenerator(实现 pdf.IPDFGenerator)+ setPDFGenerator 注入/还原 helper
  3. 新增 7 个测试,全部通过 do()/genFile() 真实代码路径验证指标记录:
    • TestCorpPDFEmailDoRecordsMetrics — do() 成功路径:验证 email_sent_total{outcome="sent"} + pdf_generated_total{outcome="success"} 在真实 /metrics 文本中断言
    • TestCorpPDFEmailDoEmailFailureRecordsFailedMetric — 邮件发送失败:验证 email_sent_total{outcome="failed"} 记录
    • TestCorpPDFEmailDoPDFFailureRecordsFailedMetric — PDF 生成失败:验证 pdf_generated_total{outcome="failed"} 记录,do() 提前返回
    • TestCorpPDFEmailDoGenMsgFailure — genMsg() 失败路径:验证 do() 中 genFile 成功后 genMsg 失败的 return err 分支
    • TestCorpPDFEmailGenFileSkipsWhenFileExists — genFile() fileExist 早返回路径
    • TestCorpPDFEmailCleanRemovesFile — clean() 文件存在时删除
    • TestCorpPDFEmailCleanNoFile — clean() 文件不存在时不 panic

覆盖率

  • do(): 0% → 100%
  • genFile(): 0% → 100%
  • clean(): 0% → 75%
  • fileExist(): 100%
  • buildCorpSigningInfo(): 83.3%
  • genEmailTmpl(): 83.3%
  • 全部 169 个测试通过(原 163 - 1 删除 + 7 新增),build/vet/test 通过

UT 文件

  • worker/metrics_test.go:新增 fakePDFGenerator、setPDFGenerator、createTempPDFFile、newTestCorpPDFEmail、gatherMetricsText 辅助 + 7 个测试用例,覆盖 do() 全部分支(成功/邮件失败/PDF失败/genMsg失败)+ genFile() 全部分支(成功/失败/fileExist早返回)+ clean() 双路径

相关 Issue

resolve https://github.com/opensourceways/backlog/issues/2552

AI 使用声明

当前 PR 是否有 AI 参与:

@opensourceways-bot

Copy link
Copy Markdown
Contributor

Welcome To opensourceways Community

Hey @fly333sky , thanks for your contribution to the community.

Bot Usage Manual

I'm the Bot here serving you. You can find the instructions on how to interact with me at Here . That means you can comment below every pull request or issue to trigger Bot Commands.

Contact Guide

If you have any questions, please contact the SIG: infratructure ,
and any of the maintainers: @GeorgeCao-hw, @TangJia025, @pkking, @zhongjun2 ,
and any of the committers: @JavaPythonAIForBAT, @TomNewChao, @Zherphy, @tfhddd, @yangwei999, @zhongjun2 .

@opensourceways-bot

Copy link
Copy Markdown
Contributor

CLA Signature Pass

fly333sky, thanks for your pull request. All authors of the commits have signed the CLA. 👍

@socket-security

socket-security Bot commented Oct 15, 2026 •

Copy link
Copy Markdown

Review the following changes in direct dependencies. Learn more about Socket for GitHub.

Diff Package Supply Chain
Security
Vulnerability Quality Maintenance License
Addedgithub.com/​stretchr/​testify@​v1.9.096100100100100
Addedgithub.com/​opensourceways/​obs-sdk/​go@​v1.1.099100100100100

View full report

@opensourceways-bot

Copy link
Copy Markdown
Contributor

CLA Signature Pass

fly333sky, thanks for your pull request. All authors of the commits have signed the CLA. 👍

@opensourceways-bot

Copy link
Copy Markdown
Contributor

CLA Signature Pass

fly333sky, thanks for your pull request. All authors of the commits have signed the CLA. 👍

@opensourceways-bot

Copy link
Copy Markdown
Contributor

CLA Signature Pass

fly333sky, thanks for your pull request. All authors of the commits have signed the CLA. 👍

@xhx787474463-tech

Copy link
Copy Markdown
Contributor

/retest

@TangJia025

Copy link
Copy Markdown
Contributor
  1. 验收硬项 §3.3 / 完成标准 11 未达成:400 / 401 / 403 在日志里一行都没有
    controllers/access_controller.go:71-85(apiPrepareWithAC)的三类失败出口,代码链是:

checkPathParameter() 返回 400,checkApiReqToken() 返回 401/403 → sendFailedResultAsResp(fr, "")
→ response.go:56-60 sendFailedResponse 只在 statusCode >= 500 时写 error 日志,4xx 直接不写
→ ctl.StopRun() 前走的 sendResponse → operationLog 仍有 if user != "" 门槛;401/403 时 ctl.Data[apiAccessController] 从未写入 → user 为空 → 不写
净结果:这三类请求日志 0 行(指标侧没问题,InsertFilterChain 保证 401/403 被计数)。issue §3.3 明确写「401 无效 token / 403 权限不符 / 400 缺路径参数三类必须有日志且含 action 与 status_code」,是验收硬项。

更要紧的是这点被测试固化成了「预期」:

controllers/response_test.go:66-84 TestOperationLogCondition 断言 statusCode=401 + user 为空时 require.Len(t, entries, 0),注释写「original behavior」;
TestAddOperationLog 注释自称「(acceptance criterion 11)」,但 addOperationLog 的 4 个调用点(corp_signing.go:488、password_retrieval.go:94/139、auth_on_corp_manager.go:93)没有一个是 400/401/403 失败出口 —— 硬项被一个不测它的用例声明通过了。
commit 306a744 标题就是「恢复 operationLog 写入条件」,是有意回退的,PR 里没人要求这么做(无 review 记录),需要作者说明理由或直接补上。补的时候注意两点:按 SDK 契约 user 为空时该键会整条消失,别指望靠 user 字段;StopRun 之后 ctl.Data 不可用,日志要打在 sendFailedResponse 内部(那里还能拿到 statusCode 与 action)。

  1. 保留了 msg / time 两个契约字段名,业务字段把顶层字段覆盖掉
    signing/watch/cla_updated.go:42:log.Error("send cla updated event failed", "msg", msg)(msg 是 message.CLAUpdatedMsg 结构体)
    signing/watch/impl.go:211:"time", util.Now()(原 printf 的 time:%d,util.Now() 是 int64 Unix 秒)
    obs-sdk 的 handler 是手写 JSON、按契约顺序输出、对业务字段不做保留字过滤(log/handler.go:117-158),所以同一行会出现重复键。我用 obs-sdk v1.1.0 实测(log.SetDefault + 上面两种调用):

{"time":"2026-10-15T07:42:27.293Z","level":"error","msg":"send cla updated event failed",...,"logger":"obsdup/main.go:12","msg":"cla updated: link x"}
{...,"msg":"collected signings",...,"logger":"obsdup/main.go:13","time":1730000000}
后果正好踩在铁律 5 上:msg 被变量值覆盖(多数采集侧 JSON 解析取最后一个键)→ 每行 msg 都不同,无法聚合计数;而这条恰是「CLA 更新事件通道满丢弃」那条 —— 唯一该告警的分支反而最不可聚合。第二处 time 被 Unix 秒覆盖,会让采集侧的时间字段解析出问题。

建议把字段改名(如 event_msg / elapsed_ms 或 ts)。顺带一提 obs-sdk 侧也没做保留字防护,值得在那仓单开一条,但不属于本 PR 范围。

  1. cla_email_sent_total 两条邮件路径口径不一致,outcome="failed" 随重试放大最多 10 倍
    worker/corp_pdf_email.go:65-72 的 recordEmailSent(..., "failed") 落在 do() 里,而 do() 是 worker.go:76-86 传给 tryToSendEmail(action) 的重试体(10 次 × 1min)→ 一封最终成功的邮件最多留 9 条 failed;
    worker/worker.go:114-121 的 SendSimpleMessage 是在 tryToSendEmail 之外按最终结果记 1 次。
    同一个指标名,一条路径记「尝试」、一条记「最终结果」,企业签署邮件(主要邮件流)的失败率被系统性高估。计划文档里写的是「发信最多重试 10 次 × 1min,是真实的业务结果」——所以 do() 里的计数应该移到重试之外(或明确改名为 attempts 并让两条路径一致)。

@TangJia025

Copy link
Copy Markdown
Contributor

这次改动把日志和指标一起改了,整体改动较大。

以上3个问题:
1、4xx的错误没加错误日志;建议改
2、日志打印自定义key把obs-sdk框架key顶掉,建议自定义key改名,比如event_msg
3、上报在流程内部含重试,可以先不动,能反映错误就可以。

@opensourceways-bot

Copy link
Copy Markdown
Contributor

CLA Signature Pass

fly333sky, thanks for your pull request. All authors of the commits have signed the CLA. 👍

@TangJia025

Copy link
Copy Markdown
Contributor

/lgtm
/approve

@opensourceways-bot

Copy link
Copy Markdown
Contributor

Review Code Feedback

  • The label lgtm-tangjia025 was added to this pull request. It means that TangJia025 reviewed the code changes. 👋
Tips
  • If this pull request is not merged while all conditions are met, comment /check-pr to try again. 😄

@opensourceways-bot

Copy link
Copy Markdown
Contributor

Review Code Feedback

  • The label approved was added to this pull request. It means that TangJia025 reviewed the code changes. 👋
Tips
  • If this pull request is not merged while all conditions are met, comment /check-pr to try again. 😄

@tfhddd

tfhddd commented Oct 15, 2026

Copy link
Copy Markdown
Contributor

/lgtm
/approve

@opensourceways-bot

Copy link
Copy Markdown
Contributor

Review Code Feedback

  • The label lgtm-tfhddd was added to this pull request. It means that tfhddd reviewed the code changes. 👋
Tips
  • If this pull request is not merged while all conditions are met, comment /check-pr to try again. 😄

@opensourceways-bot

Copy link
Copy Markdown
Contributor

Review Code Feedback

  • The label approved was added to this pull request. It means that tfhddd reviewed the code changes. 👋
Tips
  • If this pull request is not merged while all conditions are met, comment /check-pr to try again. 😄

@TangJia025

Copy link
Copy Markdown
Contributor

/retest

1 similar comment
@zkhzkhz

zkhzkhz commented Oct 16, 2026

Copy link
Copy Markdown

/retest

@opensourceways-bot

Copy link
Copy Markdown
Contributor
检查项 状态
敏感信息扫描 ✅
漏洞扫描 ✅
Check代码检查 ❌
开源license合规扫描 ✅
开发阶段设计文档检查 ✅
流水线链接 点击跳转查看日志

@xhx787474463-tech

Copy link
Copy Markdown
Contributor

/retest

@opensourceways-bot

Copy link
Copy Markdown
Contributor
检查项 状态
敏感信息扫描 ✅
漏洞扫描 ✅
Check代码检查 ❌
开源license合规扫描 ✅
开发阶段设计文档检查 ✅
流水线链接 点击跳转查看日志

@TangJia025
TangJia025 merged commit 8b06433 into review Oct 16, 2026
3 of 9 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants