Bladeren bron

Add debug.GCStats option.

Richard Crowley 12 jaren geleden
bovenliggende
commit
3b835b0772
2 gewijzigde bestanden met toevoegingen van 47 en 1 verwijderingen
  1. 4 1
      cmd/metrics-example/metrics-example.go
  2. 43 0
      debug.go

+ 4 - 1
cmd/metrics-example/metrics-example.go

@@ -110,8 +110,11 @@ func main() {
 		}()
 	}
 
+	metrics.RegisterDebugGCStats(r)
+	go metrics.CaptureDebugGCStats(r, 5)
+
 	metrics.RegisterRuntimeMemStats(r)
-	metrics.CaptureRuntimeMemStats(r, 5)
+	go metrics.CaptureRuntimeMemStats(r, 5)
 
 	metrics.Log(r, 60, log.New(os.Stderr, "metrics: ", log.Lmicroseconds))
 

+ 43 - 0
debug.go

@@ -0,0 +1,43 @@
+package metrics
+
+import (
+	"runtime/debug"
+	"time"
+)
+
+var gcStats debug.GCStats
+
+// Capture new values for the Go garbage collector statistics exported in
+// debug.GCStats.  This is designed to be called as a goroutine.
+func CaptureDebugGCStats(r Registry, interval int64) {
+	for {
+		CaptureDebugGCStatsOnce(r)
+		time.Sleep(time.Duration(int64(1e9) * int64(interval)))
+	}
+}
+
+// Capture new values for the Go garbage collector statistics exported in
+// debug.GCStats.  This is designed to be called in a background goroutine.
+// Giving a registry which has not been given to RegisterDebugGCStats will
+// panic.
+func CaptureDebugGCStatsOnce(r Registry) {
+	debug.ReadGCStats(&gcStats)
+	r.Get("debug.GCStats.LastGC").(Gauge).Update(int64(gcStats.LastGC.UnixNano()))
+	r.Get("debug.GCStats.NumGC").(Gauge).Update(int64(gcStats.NumGC))
+	r.Get("debug.GCStats.PauseTotal").(Gauge).Update(int64(gcStats.PauseTotal))
+	if 0 < len(gcStats.Pause) {
+		r.Get("debug.GCStats.Pause").(Histogram).Update(int64(gcStats.Pause[0]))
+	}
+	//r.Get("debug.GCStats.PauseQuantiles").(Histogram).Update(gcStats.PauseQuantiles)
+}
+
+// Register metrics for the Go garbage collector statistics exported in
+// debug.GCStats.  The metrics are named by their fully-qualified Go symbols,
+// i.e. debug.GCStats.PauseTotal.
+func RegisterDebugGCStats(r Registry) {
+	r.Register("debug.GCStats.LastGC", NewGauge())
+	r.Register("debug.GCStats.NumGC", NewGauge())
+	r.Register("debug.GCStats.PauseTotal", NewGauge())
+	r.Register("debug.GCStats.Pause", NewHistogram(NewExpDecaySample(1028, 0.015)))
+	//r.Register("debug.GCStats.PauseQuantiles", NewHistogram(NewExpDecaySample(1028, 0.015)))
+}