go-metrics

Coda Hale 度量库的 Go 移植版。「Go port of Coda Hale's Metrics library」

Github星跟踪图

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:

主要指标

概览
名称与所有者rcrowley/go-metrics
主编程语言Go
编程语言Go (语言数: 2)
平台Linux, Mac, Windows
许可证Other
所有者活动
创建于2011-11-21 21:35:09
推送于2025-04-01 21:45:20
最后一次提交2025-04-01 14:45:20
发布数0
用户参与
星数3.5k
关注者数84
派生数497
提交数412
已启用问题?
问题数116
打开的问题数49
拉请求数101
打开的拉请求数31
关闭的拉请求数53
项目设置
已启用Wiki?
已存档?
是复刻?
已锁定?
是镜像?
是私有?

go-metrics

travis build status

Go port of Coda Hale's Metrics library: https://github.com/dropwizard/metrics.

Documentation: http://godoc.org/github.com/rcrowley/go-metrics.

Usage

Create and update 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() is not threadsafe. For threadsafe metric registration use
GetOrRegister:

t := metrics.GetOrRegisterTimer("account.create.latency", nil)
t.Time(func() {})
t.Update(47)

NOTE: Be sure to unregister short-lived meters and timers otherwise they will
leak memory:

// 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")

Periodically log every metric in human-readable form to standard error:

go metrics.Log(metrics.DefaultRegistry, 5 * time.Second, log.New(os.Stderr, "metrics: ", log.Lmicroseconds))

Periodically log every metric in slightly-more-parseable form to syslog:

w, _ := syslog.Dial("unixgram", "/dev/log", syslog.LOG_INFO, "metrics")
go metrics.Syslog(metrics.DefaultRegistry, 60e9, w)

Periodically emit every metric to Graphite using the Graphite client:


import "github.com/cyberdelia/go-metrics-graphite"

addr, _ := net.ResolveTCPAddr("tcp", "127.0.0.1:2003")
go graphite.Graphite(metrics.DefaultRegistry, 10e9, "metrics", addr)

Periodically emit every metric into InfluxDB:

NOTE: this has been pulled out of the library due to constant fluctuations
in the InfluxDB API. In fact, all client libraries are on their way out. see
issues #121 and
#124 for progress and details.

import "github.com/vrischmann/go-metrics-influxdb"

go influxdb.InfluxDB(metrics.DefaultRegistry,
  10e9, 
  "127.0.0.1:8086", 
  "database-name", 
  "username", 
  "password"
)

Periodically upload every metric to Librato using the Librato client:

Note: the client included with this repository under the librato package
has been deprecated and moved to the repository linked above.

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
)

Periodically emit every metric to StatHat:

import "github.com/rcrowley/go-metrics/stathat"

go stathat.Stathat(metrics.DefaultRegistry, 10e9, "example@example.com")

Maintain all metrics along with expvars at /debug/metrics:

This uses the same mechanism as the official expvar
but exposed under /debug/metrics, which shows a json representation of all your usual expvars
as well as all your go-metrics.

import "github.com/rcrowley/go-metrics/exp"

exp.Exp(metrics.DefaultRegistry)

Installation

go get github.com/rcrowley/go-metrics

StatHat support additionally requires their Go client:

go get github.com/stathat/go

Publishing Metrics

Clients are available for the following destinations: