rcrowley/go-metrics

GitHub: rcrowley/go-metrics

Coda Hale Metrics 的 Go 移植版,为 Go 应用提供计数器、仪表盘、直方图、计时器等指标采集与多后端上报能力,现已被归档。

Stars: 3463 | Forks: 492

# go-metrics ![travis 构建状态](https://travis-ci.org/rcrowley/go-metrics.svg?branch=master) Coda Hale 的 Metrics 库的 Go 移植版:。 文档:。 ## 自 2025 年 4 月 1 日起归档 此仓库不再维护。作者建议您探索以下更新的、被更广泛采用的库来 满足您的 Go 监控需求: * [OpenTelemetry Go SDK](https://opentelemetry.io/docs/languages/go/instrumentation/#metrics) * [Prometheus Go 客户端库](https://pkg.go.dev/github.com/prometheus/client_golang/prometheus) ## 用法 创建并更新指标: ``` c := metrics.NewCounter() metrics.Register("foo", c) c.Inc(47) g := metrics.NewGauge() metrics.Register("bar", g) g.Update(47) r := NewRegistry() g := metrics.NewRegisteredFunctionalGauge("cache-evictions", r, func() int64 { return cache.getEvictionsCount() }) s := metrics.NewExpDecaySample(1028, 0.015) // or metrics.NewUniformSample(1028) h := metrics.NewHistogram(s) metrics.Register("baz", h) h.Update(47) m := metrics.NewMeter() metrics.Register("quux", m) m.Mark(47) t := metrics.NewTimer() metrics.Register("bang", t) t.Time(func() {}) t.Update(47) ``` Register() 不是线程安全的。对于线程安全的指标注册,请使用 GetOrRegister: ``` t := metrics.GetOrRegisterTimer("account.create.latency", nil) t.Time(func() {}) t.Update(47) ``` **注意:** 务必注销短生命周期的 meter 和 timer,否则它们会导致 内存泄漏: ``` // Will call Stop() on the Meter to allow for garbage collection metrics.Unregister("quux") // Or similarly for a Timer that embeds a Meter metrics.Unregister("bang") ``` 定期以人类可读的形式将每个指标记录到标准错误输出: ``` go metrics.Log(metrics.DefaultRegistry, 5 * time.Second, log.New(os.Stderr, "metrics: ", log.Lmicroseconds)) ``` 定期以更易于解析的形式将每个指标记录到 syslog: ``` w, _ := syslog.Dial("unixgram", "/dev/log", syslog.LOG_INFO, "metrics") go metrics.Syslog(metrics.DefaultRegistry, 60e9, w) ``` 使用 [Graphite 客户端](https://github.com/cyberdelia/go-metrics-graphite) 定期将每个指标发送到 Graphite: ``` import "github.com/cyberdelia/go-metrics-graphite" addr, _ := net.ResolveTCPAddr("tcp", "127.0.0.1:2003") go graphite.Graphite(metrics.DefaultRegistry, 10e9, "metrics", addr) ``` 定期将每个指标发送到 InfluxDB: **注意:** 由于 InfluxDB API 的不断变动,此功能已从库中移除。实际上,所有的客户端库都在逐步淘汰。请查看 issue [#121](https://github.com/rcrowley/go-metrics/issues/121) 和 [#124](https://github.com/rcrowley/go-metrics/issues/124) 以了解进展和详细信息。 ``` import "github.com/vrischmann/go-metrics-influxdb" go influxdb.InfluxDB(metrics.DefaultRegistry, 10e9, "127.0.0.1:8086", "database-name", "username", "password" ) ``` 使用 [Librato 客户端](https://github.com/mihasya/go-metrics-librato) 定期将每个指标上传到 Librato: **注意**:此仓库中 `librato` 包下包含的客户端 已被弃用,并移至上面链接的仓库。 ``` import "github.com/mihasya/go-metrics-librato" go librato.Librato(metrics.DefaultRegistry, 10e9, // interval "example@example.com", // account owner email address "token", // Librato API token "hostname", // source []float64{0.95}, // percentiles to send time.Millisecond, // time unit ) ``` 定期将每个指标发送到 StatHat: ``` import "github.com/rcrowley/go-metrics/stathat" go stathat.Stathat(metrics.DefaultRegistry, 10e9, "example@example.com") ``` 将所有指标连同 expvars 一起维护在 `/debug/metrics`: 这使用了与[官方 expvar](http://golang.org/pkg/expvar/)相同的机制, 但暴露在 `/debug/metrics` 下,它会以 JSON 格式显示您所有常规的 expvars 以及所有的 go-metrics。 ``` import "github.com/rcrowley/go-metrics/exp" exp.Exp(metrics.DefaultRegistry) ``` ## 安装 ``` go get github.com/rcrowley/go-metrics ``` StatHat 支持还需要它们的 Go 客户端: ``` go get github.com/stathat/go ``` ## 发布指标 以下目标提供了相应的客户端: * AppOptics - https://github.com/ysamlan/go-metrics-appoptics * Librato - https://github.com/mihasya/go-metrics-librato * Graphite - https://github.com/cyberdelia/go-metrics-graphite * InfluxDB - https://github.com/vrischmann/go-metrics-influxdb * Ganglia - https://github.com/appscode/metlia * Prometheus - https://github.com/deathowl/go-metrics-prometheus * DataDog - https://github.com/syntaqx/go-metrics-datadog * SignalFX - https://github.com/pascallouisperez/go-metrics-signalfx * Honeycomb - https://github.com/getspine/go-metrics-honeycomb * Wavefront - https://github.com/wavefrontHQ/go-metrics-wavefront * Open-Falcon - https://github.com/g4zhuj/go-metrics-falcon * AWS CloudWatch - [https://github.com/savaki/cloudmetrics](https://github.com/savaki/cloudmetrics)
标签:API集成, EVTX分析, Go, Ruby工具, SOC Prime, 可观测性, 开发工具, 性能监控, 指标度量, 日志审计