Documentation
¶
Overview ¶
Package metrics 提供通用的 Prometheus 指标注册和初始化(framework-agnostic)。
所有 enjoydream 后端服务共享此包,保持指标命名和标签的一致性。 本包只暴露 Registry 与指标类型,不绑定任何 HTTP 框架; HTTP 适配器位于 hertz.MetricsHandler / hertz.MetricsHandlerFor / hertz.InitAndRegister。
提供的指标 ¶
- app_build_info{service, commit, branch, build_time, go_version} — 构建信息
- app_start_time_seconds — 进程启动时间戳
快速接入 ¶
在 main.go 中:
metrics.Init("my-service")
// Hertz 适配器:
r.GET("/metrics", hertz.MetricsHandler())
// 或使用自定义 Registry(推荐与 middleware.PrometheusMetrics 共享):
r.GET("/metrics", hertz.MetricsHandlerFor(metrics.Registry))
build.sh 改动 ¶
所有服务改为注入 ekit/version 包的变量:
MOD="github.com/yi-nology/ekit-metrics"
LDFLAGS="-X ${MOD}/version.BuildCommit=${GIT_COMMIT} ..."
自定义 Registry ¶
使用独立的 Registry 避免与全局 prometheus.DefaultRegistry 冲突:
registry := metrics.Registry // 传给 middleware.NewPrometheusMetrics 或 hertz.MetricsHandlerFor
Index ¶
Constants ¶
This section is empty.
Variables ¶
View Source
var ( // BuildInfo 暴露构建元数据(commit、branch、build_time、go_version)。 BuildInfo = prometheus.NewGaugeVec(prometheus.GaugeOpts{ Name: "app_build_info", Help: "Build information about the service", }, []string{"service", "commit", "branch", "build_time", "go_version"}) // StartTimeSeconds 记录进程启动的 Unix 时间戳。 StartTimeSeconds = prometheus.NewGauge(prometheus.GaugeOpts{ Name: "app_start_time_seconds", Help: "Unix timestamp of service start time", }) )
View Source
var Registry = prometheus.NewRegistry()
Registry 是自定义的 Prometheus 注册器。 传入 ekit/middleware 的 PrometheusMetrics 时使用,避免全局 Registry 冲突。
Functions ¶
Types ¶
This section is empty.
Directories
¶
| Path | Synopsis |
|---|---|
|
Package resilience 提供客户端弹性设施(熔断器/重试)的共享 Prometheus 指标集,供 network/httpx 与 nns/endpoint 等弹性栈消费方复用,保证 全库熔断/重试可观测性的指标命名与标签基数规范一致(PLATFORM_CONTRACT §5.5)。
|
Package resilience 提供客户端弹性设施(熔断器/重试)的共享 Prometheus 指标集,供 network/httpx 与 nns/endpoint 等弹性栈消费方复用,保证 全库熔断/重试可观测性的指标命名与标签基数规范一致(PLATFORM_CONTRACT §5.5)。 |
Click to show internal directories.
Click to hide internal directories.