Skip to content

docs(agents): 修正 Go 指标命名口径 —— 传最终名,SDK 不补后缀 - #17

Open
TangJia025 wants to merge 1 commit into
mainfrom
fix/2061-agents-go-metric-name
Open

TangJia025 wants to merge 1 commit into
mainfrom
fix/2061-agents-go-metric-name

Conversation

@TangJia025

Copy link
Copy Markdown
Contributor

AGENTS.md 的 Go 段写着:

指标注册:……名称为基础名(不带 _total / _seconds)

这句是错的,且方向正好相反 —— Go 侧必须传最终名,SDK 与 client_golang 都不会补任何后缀。

为什么错

m.Handler() 是 promhttp.HandlerFor(reg, promhttp.HandlerOpts{}),HandlerOpts 零值下 EnableOpenMetrics 为 false —— 永远走经典 text/plain; version=0.0.4。而 client_golang 只在 OpenMetrics 格式下才补 _total。所以注册什么名字就暴露什么名字。

那句「自动补后缀」的语义来自 Java / Micrometer(java/src/main/java/io/opensourceways/obssdk/ObsMetrics.java 的注释),被错误地写进了 Go 段。

实测(非推断)

直接引本仓 go/ 模块,对一个真实 Metrics 实例打 m.Handler():

m := obsmetrics.New(obsmetrics.Config{Service: "review"})
m.NewCounterVec("review_events_total", "h", "entry").Inc("pr_event")   // 带 _total
m.NewCounterVec("review_no_suffix", "h").Inc()                         // 故意不带,看会不会补
m.NewHistogramVec("review_latency_seconds", "h", "path").Observe(0.1, "sig")

输出:

# TYPE review_events_total counter
review_events_total{community="unknown",entry="pr_event",env="unknown",...} 1

# TYPE review_no_suffix counter
review_no_suffix{community="unknown",env="unknown",...} 1        ← 没有补 _total

# TYPE review_latency_seconds histogram
review_latency_seconds_bucket{...,path="sig",le="0.005"} 0
review_latency_seconds_sum{...,path="sig"} 0.1
review_latency_seconds_count{...,path="sig"} 1                   ← 也没补 _seconds

go/metrics/metrics_test.go 本来就是这个口径(NewCounterVec("events_total", …) 后断言 GetName() == "events_total"),文档与自己的测试对不上。

对照(两个都不是 Go 的行为):

  • Node:prom-client 同样要最终名,SDK 不改名 —— AGENTS.md 的 Node 段本来就写对了。
  • Java:Micrometer 会补后缀。java/src/test/.../ObsMetricsTest.java:50,58 注册 m.counter("events", …) 后断言输出含 events_total,实测成立。

影响

这不是纯文字问题。按原句写出来的指标名没有 _total,直接违反 spec/metrics-format.md「单位后缀遵循 Prometheus 约定(_total / _seconds / …)」;而且指标一旦少了后缀,查询侧无从判断是 SDK 没补还是调用方没写,只会当成命名不规范。

已经在按这个口径写埋点的地方(如 robot-universal-review 接入)需要一并纠正 —— 见 backlog#2162。

改动

  • Go 代码示例:NewCounterVec("built_releases", …) → NewCounterVec("built_releases_total", …)
  • 命名说明改为「传最终名」,附上原因(EnableOpenMetrics 默认 false)、实测结论,以及 Node / Java 对照 —— 避免后来人"照 Node/Java 类推"再改回去

只动 AGENTS.md,不改任何代码与 spec(spec/metrics-format.md 的表述本来就对)。

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

🤖 Generated with Claude Code

AI 使用声明

当前 PR 是否有 AI 参与:

  • 否
  • 是
    1. AI Agent 平台:Claude Code
    2. AI 模型:deepseek-v4-flash
    3. Prompt 上下文:为 robot-universal-review 设计指标埋点(backlog#2162)时,被 AGENTS.md 中「名称为基础名、SDK 自动补后缀」误导写错了指标名;实测推翻后顺带修正该文档

AGENTS.md 的 Go 段写着「名称为**基础名**(不带 `_total` / `_seconds`)」,与实际行为相反:

`m.Handler()` 是 `promhttp.HandlerFor(reg, promhttp.HandlerOpts{})`,`EnableOpenMetrics`
默认为 false,永远走经典 text 格式;而 `client_golang` 只在 OpenMetrics 格式下才补 `_total`。
**注册什么名字就暴露什么名字。**

这句话还会诱导写出违反 `spec/metrics-format.md`「单位后缀遵循 Prometheus 约定」的指标名 ——
而且指标一旦少了 `_total`,查询侧无从判断是 SDK 没补还是调用方没写。

改动:
- Go 代码示例 `NewCounterVec("built_releases", …)` → `NewCounterVec("built_releases_total", …)`
- 命名说明改为「传最终名」,附实测结论与 Node/Java 对照
  (Node 的 prom-client 同样要最终名;只有 Java 的 Micrometer 会自动补后缀)

实测方式与输出见 PR 正文。

Refs opensourceways/backlog#2162

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. 👍

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