debug.go 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. package metrics
  2. import (
  3. "runtime/debug"
  4. "time"
  5. )
  6. var gcStats debug.GCStats
  7. // Capture new values for the Go garbage collector statistics exported in
  8. // debug.GCStats. This is designed to be called as a goroutine.
  9. func CaptureDebugGCStats(r Registry, interval int64) {
  10. for {
  11. CaptureDebugGCStatsOnce(r)
  12. time.Sleep(time.Duration(int64(1e9) * int64(interval)))
  13. }
  14. }
  15. // Capture new values for the Go garbage collector statistics exported in
  16. // debug.GCStats. This is designed to be called in a background goroutine.
  17. // Giving a registry which has not been given to RegisterDebugGCStats will
  18. // panic.
  19. func CaptureDebugGCStatsOnce(r Registry) {
  20. debug.ReadGCStats(&gcStats)
  21. r.Get("debug.GCStats.LastGC").(Gauge).Update(int64(gcStats.LastGC.UnixNano()))
  22. r.Get("debug.GCStats.NumGC").(Gauge).Update(int64(gcStats.NumGC))
  23. r.Get("debug.GCStats.PauseTotal").(Gauge).Update(int64(gcStats.PauseTotal))
  24. if 0 < len(gcStats.Pause) {
  25. r.Get("debug.GCStats.Pause").(Histogram).Update(int64(gcStats.Pause[0]))
  26. }
  27. //r.Get("debug.GCStats.PauseQuantiles").(Histogram).Update(gcStats.PauseQuantiles)
  28. }
  29. // Register metrics for the Go garbage collector statistics exported in
  30. // debug.GCStats. The metrics are named by their fully-qualified Go symbols,
  31. // i.e. debug.GCStats.PauseTotal.
  32. func RegisterDebugGCStats(r Registry) {
  33. r.Register("debug.GCStats.LastGC", NewGauge())
  34. r.Register("debug.GCStats.NumGC", NewGauge())
  35. r.Register("debug.GCStats.PauseTotal", NewGauge())
  36. r.Register("debug.GCStats.Pause", NewHistogram(NewExpDecaySample(1028, 0.015)))
  37. //r.Register("debug.GCStats.PauseQuantiles", NewHistogram(NewExpDecaySample(1028, 0.015)))
  38. }