Help us shape the future of Go! Take the 2026 Go Developer Survey

metrics

package
v2.0.7 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Sep 10, 2026 License: MIT Imports: 7 Imported by: 0

Documentation

Overview

Package metrics 提供 mgin v2 的 Prometheus 指标能力。

设计目标:

  • 零依赖业务代码即可启用:业务 controller / service 不需要主动埋点, pkg/metrics.Middleware() 自动记录所有 gin 路由的请求计数与时延。
  • 与 health 包同样策略:端点 /metrics 在 baseRouter 最早注册,避免被 casbin / jwt 中间件拦截。
  • 与 plugin.Plugin 协同:plugin.HealthAll() 的结果每周期上报为 mgin_dependency_up。

注意:本包依赖 prometheus/client_golang(v1.19.1),是 v2.1 唯一新增的 direct 第三方依赖。 如需在离线环境使用,可注释掉 middleware 引用,仅保留 build_info / dependency_up 等轻量指标。

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Handler

func Handler() http.Handler

Handler 返回 promhttp 的 /metrics handler,可直接挂到 gin 路由。

func Middleware

func Middleware() gin.HandlerFunc

Middleware 返回一个 gin handler,用于自动记录 HTTP 指标。 用法:r.Use(metrics.Middleware())

注意:path 用 c.FullPath(),避免把 URL 参数(/users/123)当成不同的 label, 否则高基数会撑爆 Prometheus。

func SetBuildInfo

func SetBuildInfo(version, commit, goVersion string)

SetBuildInfo 在启动时填入版本信息。version/commit/goVersion 任一为空会被填为 "unknown"。 建议在 mgin.NewApp 之后、app.Run 之前调用一次。

func SetDependencyUp

func SetDependencyUp(name string, up bool)

SetDependencyUp 一次性更新某个外部依赖的可用性。

func SetPluginHealth

func SetPluginHealth(name string, healthy bool)

SetPluginHealth 一次性更新某个 plugin 的健康状态。name 为空时 no-op。

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL