debug.go 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  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. lastGC := gcStats.LastGC
  21. debug.ReadGCStats(&gcStats)
  22. r.Get("debug.GCStats.LastGC").(Gauge).Update(int64(gcStats.LastGC.UnixNano()))
  23. r.Get("debug.GCStats.NumGC").(Gauge).Update(int64(gcStats.NumGC))
  24. r.Get("debug.GCStats.PauseTotal").(Gauge).Update(int64(gcStats.PauseTotal))
  25. if lastGC != gcStats.LastGC && 0 < len(gcStats.Pause) {
  26. r.Get("debug.GCStats.Pause").(Histogram).Update(int64(gcStats.Pause[0]))
  27. }
  28. //r.Get("debug.GCStats.PauseQuantiles").(Histogram).Update(gcStats.PauseQuantiles)
  29. }
  30. // Register metrics for the Go garbage collector statistics exported in
  31. // debug.GCStats. The metrics are named by their fully-qualified Go symbols,
  32. // i.e. debug.GCStats.PauseTotal.
  33. func RegisterDebugGCStats(r Registry) {
  34. r.Register("debug.GCStats.LastGC", NewGauge())
  35. r.Register("debug.GCStats.NumGC", NewGauge())
  36. r.Register("debug.GCStats.PauseTotal", NewGauge())
  37. r.Register("debug.GCStats.Pause", NewHistogram(NewExpDecaySample(1028, 0.015)))
  38. //r.Register("debug.GCStats.PauseQuantiles", NewHistogram(NewExpDecaySample(1028, 0.015)))
  39. }