返回顶部
p

prometheus-go-code-reviewPrometheus代码审查

Reviews Prometheus instrumentation in Go code for proper metric types, labels, and patterns. Use when reviewing code with prometheus/client_golang metrics.

作者: admin | 来源: ClawHub
源自
ClawHub
版本
V 2.3.0
安全检测
已通过
132
下载量
免费
免费
0
收藏
概述
安装方式
版本历史

prometheus-go-code-review

Prometheus Go 代码审查

审查清单

  • - [ ] 指标类型与测量语义匹配(计数器/仪表盘/直方图)
  • [ ] 标签具有低基数(无用户ID、时间戳、路径)
  • [ ] 指标名称遵循约定(蛇形命名法、单位后缀)
  • [ ] 直方图使用适当的桶边界
  • [ ] 指标只注册一次,而非每次请求
  • [ ] 采集器在竞态条件下不会崩溃
  • [ ] /metrics 端点已暴露并可访问

指标类型选择

测量项类型示例
处理的请求数计数器requeststotal
队列中的项目数
仪表盘 | queuelength | | 请求持续时间 | 直方图 | requestdurationseconds | | 并发连接数 | 仪表盘 | active_connections | | 自启动以来的错误数 | 计数器 | errors_total | | 内存使用量 | 仪表盘 | memory_bytes |

关键反模式

1. 高基数标签

go
// 错误 - 每个用户/请求唯一
counter := promauto.NewCounterVec(
prometheus.CounterOpts{Name: requests_total},
[]string{user_id, path}, // 数百万个序列!
)
counter.WithLabelValues(userID, request.URL.Path).Inc()

// 正确 - 有界的标签值
counter := promauto.NewCounterVec(
prometheus.CounterOpts{Name: requests_total},
[]string{method, status_code}, // <100个序列
)
counter.WithLabelValues(r.Method, statusCode).Inc()

2. 错误的指标类型

go
// 错误 - 对单调递增的值使用仪表盘
requestCount := promauto.NewGauge(prometheus.GaugeOpts{
Name: http_requests,
})
requestCount.Inc() // 应该使用计数器!

// 正确
requestCount := promauto.NewCounter(prometheus.CounterOpts{
Name: httprequeststotal,
})
requestCount.Inc()

3. 每次请求注册

go
// 错误 - 每次请求创建新指标
func handler(w http.ResponseWriter, r *http.Request) {
counter := prometheus.NewCounter(...) // 每次都创建新的!
prometheus.MustRegister(counter) // 重复时崩溃!
}

// 正确 - 只注册一次
var requestCounter = promauto.NewCounter(prometheus.CounterOpts{
Name: httprequeststotal,
})

func handler(w http.ResponseWriter, r *http.Request) {
requestCounter.Inc()
}

4. 缺少单位后缀

go
// 错误
duration := promauto.NewHistogram(prometheus.HistogramOpts{
Name: request_duration, // 没有单位!
})

// 正确
duration := promauto.NewHistogram(prometheus.HistogramOpts{
Name: requestdurationseconds, // 名称中包含单位
})

良好模式

指标定义

go
var (
httpRequests = promauto.NewCounterVec(
prometheus.CounterOpts{
Namespace: myapp,
Subsystem: http,
Name: requests_total,
Help: 处理的HTTP请求总数,
},
[]string{method, status},
)

httpDuration = promauto.NewHistogramVec(
prometheus.HistogramOpts{
Namespace: myapp,
Subsystem: http,
Name: requestdurationseconds,
Help: HTTP请求延迟,
Buckets: []float64{.005, .01, .025, .05, .1, .25, .5, 1, 2.5, 5, 10},
},
[]string{method},
)
)

中间件模式

go
func metricsMiddleware(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
timer := prometheus.NewTimer(httpDuration.WithLabelValues(r.Method))
defer timer.ObserveDuration()

wrapped := &responseWriter{ResponseWriter: w, status: 200}
next.ServeHTTP(wrapped, r)

httpRequests.WithLabelValues(r.Method, strconv.Itoa(wrapped.status)).Inc()
})
}

暴露指标

go
import github.com/prometheus/client_golang/prometheus/promhttp

func main() {
http.Handle(/metrics, promhttp.Handler())
http.ListenAndServe(:9090, nil)
}

审查问题

  1. 1. 指标类型是否正确(计数器 vs 仪表盘 vs 直方图)?
  2. 标签值是否有界(无UUID、时间戳、路径)?
  3. 指标名称是否包含单位(seconds、bytes)?
  4. 指标是否只注册一次(而非每次请求)?
  5. /metrics 端点是否正确暴露?

标签

skill ai

通过对话安装

该技能支持在以下平台通过对话安装:

OpenClaw WorkBuddy QClaw Kimi Claude

方式一:安装 SkillHub 和技能

帮我安装 SkillHub 和 prometheus-go-code-review-1776089581 技能

方式二:设置 SkillHub 为优先技能安装源

设置 SkillHub 为我的优先技能安装源,然后帮我安装 prometheus-go-code-review-1776089581 技能

通过命令行安装

skillhub install prometheus-go-code-review-1776089581

下载

⬇ 下载 prometheus-go-code-review v2.3.0(免费)

文件大小: 2.28 KB | 发布时间: 2026-4-14 09:40

v2.3.0 最新 2026-4-14 09:40
- Expanded documentation with a comprehensive review checklist, best practices, and anti-pattern examples for Prometheus instrumentation in Go.
- Added detailed code snippets illustrating proper and improper usage of metric types, labels, and registration patterns.
- Included a metric type selection table and review questions to guide code review process.
- Strengthened guidance on metric naming conventions, label cardinality, and safe /metrics exposition.

Archiver·手机版·闲社网·闲社论坛·智能体自动化市场· 多链控股集团有限公司 · 苏ICP备2025199260号-1

Powered by Discuz! X5.0   © 2024-2026 闲社网·AI智能体论坛·AI自动化解决方案·http://xianshe.com

p2p_official_large
返回顶部