Repository navigation
feat(go): 新增 beego v2 观测中间件 middleware/beegomw - #19
Merged
Merged
Conversation
app-cla-server 等 beego v2 服务需要一个与 ginmw 同职责的适配器: 注入 request_id、可信判定点解析 community、记 http_server_* 指标。 要点: - 用 InsertFilterChain 而非 InsertFilter。Controller.StopRun() 内部是 panic(ErrAbort),由 serveHttp 自己的 RecoverFunc 就地吞掉,AfterExec / FinishRouter 过滤器会被整个跳过;beego 里 StopRun 常用于鉴权失败路径, 用 InsertFilter 记指标会静默漏掉全部 401/403,失败率偏低且不报错。 - path label 取 beego 路由模板(Input 的 RouterPattern),未命中路由记 "unmatched"。未命中路径由外部输入决定,回退原始 URL 会撑爆时序基数。 - 状态码对齐 beego 口径(未显式写状态时按 200)。 同时更新 go/README、AGENTS.md 与 docs 的中间件清单,go.mod 加 beego 依赖。 新增 8 个 UT(含 StopRun 不丢计数、path 用路由模板两条关键断言)。 Co-Authored-By: Claude Code <noreply@anthropic.com>
Welcome To opensourceways CommunityHey @TangJia025 , thanks for your contribution to the community. Bot Usage ManualI'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 GuideIf you have any questions, please contact the SIG: infratructure , |
CLA Signature PassTangJia025, thanks for your pull request. All authors of the commits have signed the CLA. 👍 |
|
Review the following changes in direct dependencies. Learn more about Socket for GitHub.
|
1 of 2 tasks
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.
Resolves https://github.com/opensourceways/backlog/issues/2061
背景
obs-sdk-go 目前只有 net/http 与 gin 两个中间件适配。但 backlog#1938 里归到「Go + Gin」的服务中,
app-cla-server实际是 beego v2(54 条路由 / 13 个 controller),接不进来。本 PR 补上 beego v2 适配器,与ginmw同职责:注入 request_id、可信判定点解析 community、记http_server_*指标。两个必须说清的设计点
1. 必须用
InsertFilterChain,不能用InsertFilterController.StopRun()内部是panic(ErrAbort),由serveHttp自己的 RecoverFunc 就地吞掉后直接返回,不会回到Admin:标签 —— 也就是说AfterExec/FinishRouter过滤器被整个跳过。beego 里 StopRun 常被用在鉴权失败路径上(app-cla-server 的
apiPrepareWithAC→sendFailedResultAsResp→StopRun())。若按常规用InsertFilter(AfterExec)记指标,这些 401/403 会被静默漏掉,失败率偏低且不报错——正是本仓最忌讳的那类假信号。InsertFilterChain注册的过滤器包住的正是serveHttp本身(beego 内部res.chainRoot = newFilterRouter("/*", res.serveHttp)),所以用它做的中间件能保证每个请求恰好收尾一次,StopRun 也不例外。TestStopRunStillRecorded用一条复刻鉴权失败路径的 controller 锁住了这个行为。2.
pathlabel 取路由模板,不是原始 URL从
ctx.Input.GetData("RouterPattern")取 beego 命中路由时写入的模板(如/v1/cla/:link_id),未命中路由记常量unmatched。这里刻意不回退原始 URL:未命中时路径由外部输入决定(扫描流量、伪造路径),直接入 label 会撑爆时序基数。收敛成常量既保证基数有界,也如实表达「这条请求没命中任何路由」。
状态码口径对齐 beego(未显式写状态时按 200)。
依赖
go.mod新增直接依赖github.com/beego/beego/v2 v2.3.6(与 gin 同理:只有 import 了beegomw的调用方才会编译进依赖树)。测试
go vet ./...干净,go test -race ./...全绿。新增 8 个 UT:TestRequestIDInjected/TestRequestIDHonorsInboundTestCommunityResolverWritesContextTestPathLabelUsesRouterPatternTestUnmatchedRouteLabelIsBoundedunmatchedseriesTestStopRunStillRecordedTestStatusRecordedTestNilMetricsOnlyInjects文档同步:
go/README.md、AGENTS.md、README.md、docs/进度追踪.md、docs/微服务可观测性建设技术设计.md。🤖 Generated with Claude Code
AI 使用声明
当前 PR 是否有 AI 参与: