metrics

package module
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Oct 14, 2026 License: MIT Imports: 5 Imported by: 0

README

ekit-metrics

Prometheus 指标门面——framework-agnostic Registry 与弹性指标族。

本仓库是 ekit(git.enjoye.top/enjoydream/ekit v0.34.0) 拆分出的独立 Go module,属于 yi-nology ekit 子项目矩阵(Round-2 深拆粒度)。

安装

go get github.com/yi-nology/ekit-metrics@v0.1.0

包清单

  • 根包 `metrics`(import `github.com/yi-nology/ekit-metrics`)— Registry、Init(service)、默认注册
  • `resilience/` — 熔断/重试共享指标族(httpx/nns/kitex 共用,契约 §5.5)

依赖关系

子项目矩阵与依赖方向(自底向上,19 仓):

ekit-core            文本/数据工具 + governance/lifecycle/version      (零内部依赖)
ekit-concurrency     并发与流控原语                                     (零内部依赖)
ekit-logx            日志门面 + shutdown                                (零内部依赖)
ekit-istio           Istio Proxyless                                    (零内部依赖)
  ekit-metrics       Prometheus 门面                          → core
  ekit-tracing       OTel + traceid                           → logx
  ekit-registry      注册发现 + 配置                          → core, logx
    ekit-data        缓存抽象 + 定时任务                      → core, logx, concurrency
      ekit-gorm      SQL/GORM 全家                            → core, logx
      ekit-redis     Redis 全家                               → data, core, logx
    ekit-httpx       HTTP 客户端 + 负载均衡                   → core, concurrency, logx, metrics, tracing, registry
      ekit-nns       服务治理客户端                           → httpx, registry, core, logx, tracing, metrics
        ekit-bootstrap 应用引导
    ekit-middleware  Hertz 中间件栈                           → core, concurrency, logx, metrics, tracing, registry
    ekit-hertz       Hertz 运行时/适配/instrumentation        → middleware, gorm, nns, registry, metrics, tracing, logx, core
    ekit-kitex       Kitex 生态                               → nns, registry, core, concurrency, logx, metrics
  ekit-workflow      流程编排                                 → concurrency, logx

注:ekit-observability 已在 Round-2 深拆中解散——logx 迁 [ekit-logx]、metrics 迁 [ekit-metrics]、 tracing/traceid 迁 [ekit-tracing]、shutdown 迁 [ekit-logx];原仓库已归档。

依赖:ekit-core(version)。框架专属 instrumentation(kitexprom)在 ekit-kitex。

与 ekit 单体仓库的关系

  • 代码与 ekit v0.34.0 等价,module 路径 git.enjoye.top/enjoydream/ekit/... → github.com/yi-nology/ekit-metrics/...
  • 各子仓库独立发版(SemVer);开发规范沿用 ekit AGENTS.md 约定

License

见 LICENSE

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-observability"
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",
	})
)

Registry 是自定义的 Prometheus 注册器。 传入 ekit/middleware 的 PrometheusMetrics 时使用,避免全局 Registry 冲突。

Functions

func Init

func Init(serviceName string)

Init 注册所有通用指标并设置初始值。 参数 serviceName 用于 app_build_info 的 service 标签,如 "fuxi_engine"、"moyu-server"。 必须在 main.go 中调用,且在启动 Hertz server 之前。 多次调用是安全的,只有第一次调用会注册指标。

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)。

Jump to

Keyboard shortcuts

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