go-metrics介紹
go-metrics — 對Go應用的某個服務做監控、做統計,應用級監控和測量。
源碼 : https://github.com/rcrowley/go-metrics
文檔:http : //godoc.org/github.com/rcrowley/go-metrics。
Metrics提供5種基本的度量類型:Gauges, Counters, Histograms, Meters和 Timers。
Gauge
Gauge是最簡單的度量類型,只有一個簡單的返回值,
他用來記錄一些對象或者事物的瞬時值。
類似於程序里的常量,是不變的值。
package main import ( "github.com/rcrowley/go-metrics" ) func main(){ g := metrics.NewGauge() metrics.Register("bar", g) g.Update(1) print(g.Value())//1 g.Update(5) print(g.Value())//5 }
Counter
Counter是一個簡單的計數器,可以增加和減少。
可以通過inc()和dec()方法對計數器做修改。
package main import ( "github.com/rcrowley/go-metrics" ) func main(){ c := metrics.NewCounter() metrics.Register("foo", c) c.Inc(45) c.Dec(3) print(c.Count())/42 }
Meter
Meters用來度量某個時間段的平均處理次數(request per second),每1、5、15分鍾的TPS。比如一個service的請求數,通過metrics.meter()實例化一個Meter之后,然后通過meter.mark()方法就能將本次請求記錄下來。統計結果有總的請求數,平均每秒的請求數,以及最近的1、5、15分鍾的平均TPS。
Meters
工具會幫助我們統計系統中某一個事件的速率。比如每秒請求數(TPS),每秒查詢數(QPS)等等。這個指標能反應系統當前的處理能力,幫助我們判斷資源是否已經不足。
Meters
本身是一個自增計數器。
package main import ( "time" "os" "github.com/rcrowley/go-metrics" "log" ) func main(){ m := metrics.NewMeter() metrics.Register("quux", m) m.Mark(1) go metrics.Log(metrics.DefaultRegistry, 1 * time.Second, log.New(os.Stdout, "metrics: ", log.Lmicroseconds)) var j int64 j = 1 for true { time.Sleep(time.Second * 1) j++ m.Mark(j) } }
Histrogram
Histrogram是用來度量流數據中Value的分布情況,Histrogram可以計算最大/小值、平均值,方差,分位數(如中位數,或者95th分位數),如75%,90%,98%,99%的數據在哪個范圍內。
Timer
Timer是Histogram跟Meter的一個組合,比如要統計當前請求的速率和處理時間。