go-metrics
Coda Hale 度量库的 Go 移植版:https://github.com/dropwizard/metrics。
文档:http://godoc.org/github.com/rcrowley/go-metrics
用法
创建和更新指标:
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)
注意:请务必注销 short-lived metric和计时器,否则它们会泄漏内存:
// 将调用 Meter 上的 Stop() 以允许垃圾收集 metrics.Unregister("quux") // 或者类似的嵌入 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客户机定期向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 和 issue 124。
import "github.com/vrischmann/go-metrics-influxdb" go influxdb.InfluxDB(metrics.DefaultRegistry, 10e9, "127.0.0.1:8086", "database-name", "username", "password" )
定期使用 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")
在 /debug/metrics 维护所有指标以及 expvars: 这使用与官方expvar相同的机制,但暴露在 /debug/metrics,其中显示了所有常见的exparo json 表示以及你所有的指标。
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
发布指标
客户端可用于以下 destinations:
- 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