Skip to content

feat(go): 新增 beego v2 观测中间件 middleware/beegomw - #19

Merged
TangJia025 merged 1 commit into
mainfrom
feat/2061-go-beego-mw
Sep 28, 2026
Merged

TangJia025 merged 1 commit into
mainfrom
feat/2061-go-beego-mw

Conversation

@TangJia025

@TangJia025 TangJia025 commented Sep 28, 2026 •

Copy link
Copy Markdown
Contributor

Resolves https://github.com/opensourceways/backlog/issues/2061

下游落地:backlog#2405(app-cla-server 接入指标)、backlog#2406(app-cla-server 日志转 obslog)。

背景

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_* 指标。

import "github.com/opensourceways/obs-sdk/go/middleware/beegomw"

m := obsmetrics.New(obsmetrics.Config{Service: "cla"})
beego.InsertFilterChain("/*", beegomw.Middleware(beegomw.Options{Metrics: m}))

两个必须说清的设计点

1. 必须用 InsertFilterChain,不能用 InsertFilter

Controller.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 锁住了这个行为。

before:  InsertFilter(AfterExec)  →  401 不进指标
after:   InsertFilterChain        →  401 正常计数

2. path label 取路由模板,不是原始 URL

从 ctx.Input.GetData("RouterPattern") 取 beego 命中路由时写入的模板(如 /v1/cla/:link_id),未命中路由记常量 unmatched。

这里刻意不回退原始 URL:未命中时路径由外部输入决定(扫描流量、伪造路径),直接入 label 会撑爆时序基数。收敛成常量既保证基数有界,也如实表达「这条请求没命中任何路由」。

顺带指出(不在本 PR 修复,按仓库约定只指出不顺手改):现有 middleware/middleware.go:99 与 middleware/ginmw/ginmw.go:77 都把原始 URL.Path 当 path label。gin 场景下 c.FullPath() 就是现成的路由模板,net/http 场景也可由接入方在路由层归一化——两者当前都只留了一句注释,参数化路由下同样是基数隐患。是否需要单开 issue 跟进请 reviewer 定夺。

状态码口径对齐 beego(未显式写状态时按 200)。

依赖

go.mod 新增直接依赖 github.com/beego/beego/v2 v2.3.6(与 gin 同理:只有 import 了 beegomw 的调用方才会编译进依赖树)。

测试

go vet ./... 干净,go test -race ./... 全绿。新增 8 个 UT:

用例 断言
TestRequestIDInjected / TestRequestIDHonorsInbound 无入站头则生成,有则透传
TestCommunityResolverWritesContext resolver 写入 ctx,且指标上体现覆盖值
TestPathLabelUsesRouterPattern path = 路由模板,原始 URL 不出现在任何 series
TestUnmatchedRouteLabelIsBounded 两条随机未命中路径只产生一条 unmatched series
TestStopRunStillRecorded StopRun 的 401 仍被计数(本 PR 的核心)
TestStatusRecorded 非 2xx 正常返回路径
TestNilMetricsOnlyInjects 不配 Metrics 时只注入上下文、不注册指标

文档同步:go/README.md、AGENTS.md、README.md、docs/进度追踪.md、docs/微服务可观测性建设技术设计.md。

🤖 Generated with Claude Code

AI 使用声明

当前 PR 是否有 AI 参与:

  • 否
  • 是
    1. AI Agent 平台:Claude Code
    2. AI 模型:deepseek-v4-flash
    3. Prompt 上下文:用户要求「先把 obs-sdk 的 beego 适配器做出来,提 pr」;起因是排查 app-cla-server 接入观测性时发现它是 beego v2、现有 SDK 无对应适配器

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>
@opensourceways-bot

Copy link
Copy Markdown

Welcome To opensourceways Community

Hey @TangJia025 , 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: @GeorgeCao-hw, @TangJia025, @pkking, @zkhzkhz .

@opensourceways-bot

Copy link
Copy Markdown

CLA Signature Pass

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

@socket-security

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
Addedgolang/​github.com/​beego/​beego/​v2@​v2.3.673100100100100

View full report

@TangJia025
TangJia025 merged commit 01110ad into main Sep 28, 2026
7 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.

2 participants