瀏覽代碼

Friendlier CaptureRuntimeMemStats.

Richard Crowley 13 年之前
父節點
當前提交
25d7c25adf
共有 1 個文件被更改,包括 53 次插入41 次删除
  1. 53 41
      runtime.go

+ 53 - 41
runtime.go

@@ -1,46 +1,17 @@
 package metrics
 
-import "runtime"
-
-// Register metrics for the Go runtime statistics exported in
-// runtime.MemStats.  The metrics are named by their fully-qualified
-// Go symbols, i.e. runtime.MemStatsAlloc.  In addition to
-// runtime.MemStats, register the return value of runtime.Goroutines()
-// as runtime.Goroutines.
-func RegisterRuntimeMemStats(r Registry) {
-
-	r.Register("runtime.MemStats.Alloc", NewGauge())
-	r.Register("runtime.MemStats.TotalAlloc", NewGauge())
-	r.Register("runtime.MemStats.Sys", NewGauge())
-	r.Register("runtime.MemStats.Lookups", NewGauge())
-	r.Register("runtime.MemStats.Mallocs", NewGauge())
-	r.Register("runtime.MemStats.Frees", NewGauge())
-
-	r.Register("runtime.MemStats.HeapAlloc", NewGauge())
-	r.Register("runtime.MemStats.HeapSys", NewGauge())
-	r.Register("runtime.MemStats.HeapIdle", NewGauge())
-	r.Register("runtime.MemStats.HeapInuse", NewGauge())
-	r.Register("runtime.MemStats.HeapObjects", NewGauge())
-
-	r.Register("runtime.MemStats.StackInuse", NewGauge())
-	r.Register("runtime.MemStats.StackSys", NewGauge())
-	r.Register("runtime.MemStats.MSpanInuse", NewGauge())
-	r.Register("runtime.MemStats.MSpanSys", NewGauge())
-	r.Register("runtime.MemStats.MCacheInuse", NewGauge())
-	r.Register("runtime.MemStats.MCacheSys", NewGauge())
-	r.Register("runtime.MemStats.BuckHashSys", NewGauge())
-
-	r.Register("runtime.MemStats.NextGC", NewGauge())
-	r.Register("runtime.MemStats.PauseTotalNs", NewGauge())
-	r.Register("runtime.MemStats.PauseNs",
-		NewHistogram(NewExpDecaySample(1028, 0.015)))
-	r.Register("runtime.MemStats.NumGC", NewGauge())
-	r.Register("runtime.MemStats.EnableGC", NewGauge())
-	r.Register("runtime.MemStats.DebugGC", NewGauge())
-
-	r.Register("runtime.NumCgoCall", NewGauge())
-	r.Register("runtime.NumGoroutine", NewGauge())
+import (
+	"runtime"
+	"time"
+)
 
+// Capture new values for the Go runtime statistics exported in
+// runtime.MemStats.  This is designed to be called as a goroutine.
+func CaptureRuntimeMemStats(r Registry, interval int64, readMemStats bool) {
+	for {
+		CaptureRuntimeMemStatsOnce(r, readMemStats)
+		time.Sleep(time.Duration(int64(1e9) * int64(interval)))
+	}
 }
 
 // Capture new values for the Go runtime statistics exported in
@@ -49,7 +20,7 @@ func RegisterRuntimeMemStats(r Registry) {
 // RegisterRuntimeMemStats will panic.  If the second parameter is
 // false, the counters will be left to the lazy updates provided by
 // the runtime.
-func CaptureRuntimeMemStats(r Registry, readMemStats bool) {
+func CaptureRuntimeMemStatsOnce(r Registry, readMemStats bool) {
 	var m runtime.MemStats
 	if readMemStats {
 		runtime.ReadMemStats(&m)
@@ -95,3 +66,44 @@ func CaptureRuntimeMemStats(r Registry, readMemStats bool) {
 	r.Get("runtime.NumGoroutine").(Gauge).Update(int64(runtime.NumGoroutine()))
 
 }
+
+// Register metrics for the Go runtime statistics exported in
+// runtime.MemStats.  The metrics are named by their fully-qualified
+// Go symbols, i.e. runtime.MemStatsAlloc.  In addition to
+// runtime.MemStats, register the return value of runtime.Goroutines()
+// as runtime.Goroutines.
+func RegisterRuntimeMemStats(r Registry) {
+
+	r.Register("runtime.MemStats.Alloc", NewGauge())
+	r.Register("runtime.MemStats.TotalAlloc", NewGauge())
+	r.Register("runtime.MemStats.Sys", NewGauge())
+	r.Register("runtime.MemStats.Lookups", NewGauge())
+	r.Register("runtime.MemStats.Mallocs", NewGauge())
+	r.Register("runtime.MemStats.Frees", NewGauge())
+
+	r.Register("runtime.MemStats.HeapAlloc", NewGauge())
+	r.Register("runtime.MemStats.HeapSys", NewGauge())
+	r.Register("runtime.MemStats.HeapIdle", NewGauge())
+	r.Register("runtime.MemStats.HeapInuse", NewGauge())
+	r.Register("runtime.MemStats.HeapObjects", NewGauge())
+
+	r.Register("runtime.MemStats.StackInuse", NewGauge())
+	r.Register("runtime.MemStats.StackSys", NewGauge())
+	r.Register("runtime.MemStats.MSpanInuse", NewGauge())
+	r.Register("runtime.MemStats.MSpanSys", NewGauge())
+	r.Register("runtime.MemStats.MCacheInuse", NewGauge())
+	r.Register("runtime.MemStats.MCacheSys", NewGauge())
+	r.Register("runtime.MemStats.BuckHashSys", NewGauge())
+
+	r.Register("runtime.MemStats.NextGC", NewGauge())
+	r.Register("runtime.MemStats.PauseTotalNs", NewGauge())
+	r.Register("runtime.MemStats.PauseNs",
+		NewHistogram(NewExpDecaySample(1028, 0.015)))
+	r.Register("runtime.MemStats.NumGC", NewGauge())
+	r.Register("runtime.MemStats.EnableGC", NewGauge())
+	r.Register("runtime.MemStats.DebugGC", NewGauge())
+
+	r.Register("runtime.NumCgoCall", NewGauge())
+	r.Register("runtime.NumGoroutine", NewGauge())
+
+}