debug.go 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. package metrics
  2. import (
  3. "runtime/debug"
  4. "time"
  5. )
  6. var (
  7. debugMetrics struct {
  8. GCStats struct {
  9. LastGC Gauge
  10. NumGC Gauge
  11. Pause Histogram
  12. //PauseQuantiles Histogram
  13. PauseTotal Gauge
  14. }
  15. ReadGCStats Timer
  16. }
  17. gcStats debug.GCStats
  18. )
  19. // Capture new values for the Go garbage collector statistics exported in
  20. // debug.GCStats. This is designed to be called as a goroutine.
  21. func CaptureDebugGCStats(r Registry, d time.Duration) {
  22. for {
  23. CaptureDebugGCStatsOnce(r)
  24. time.Sleep(d)
  25. }
  26. }
  27. // Capture new values for the Go garbage collector statistics exported in
  28. // debug.GCStats. This is designed to be called in a background goroutine.
  29. // Giving a registry which has not been given to RegisterDebugGCStats will
  30. // panic.
  31. func CaptureDebugGCStatsOnce(r Registry) {
  32. lastGC := gcStats.LastGC
  33. t := time.Now()
  34. debug.ReadGCStats(&gcStats)
  35. debugMetrics.ReadGCStats.UpdateSince(t)
  36. debugMetrics.GCStats.LastGC.Update(int64(gcStats.LastGC.UnixNano()))
  37. debugMetrics.GCStats.NumGC.Update(int64(gcStats.NumGC))
  38. if lastGC != gcStats.LastGC && 0 < len(gcStats.Pause) {
  39. debugMetrics.GCStats.Pause.Update(int64(gcStats.Pause[0]))
  40. }
  41. //debugMetrics.GCStats.PauseQuantiles.Update(gcStats.PauseQuantiles)
  42. debugMetrics.GCStats.PauseTotal.Update(int64(gcStats.PauseTotal))
  43. }
  44. // Register metrics for the Go garbage collector statistics exported in
  45. // debug.GCStats. The metrics are named by their fully-qualified Go symbols,
  46. // i.e. debug.GCStats.PauseTotal.
  47. func RegisterDebugGCStats(r Registry) {
  48. debugMetrics.GCStats.LastGC = NewGauge()
  49. debugMetrics.GCStats.NumGC = NewGauge()
  50. debugMetrics.GCStats.Pause = NewHistogram(NewExpDecaySample(1028, 0.015))
  51. //debugMetrics.GCStats.PauseQuantiles = NewHistogram(NewExpDecaySample(1028, 0.015))
  52. debugMetrics.GCStats.PauseTotal = NewGauge()
  53. debugMetrics.ReadGCStats = NewTimer()
  54. r.Register("debug.GCStats.LastGC", debugMetrics.GCStats.LastGC)
  55. r.Register("debug.GCStats.NumGC", debugMetrics.GCStats.NumGC)
  56. r.Register("debug.GCStats.Pause", debugMetrics.GCStats.Pause)
  57. //r.Register("debug.GCStats.PauseQuantiles", debugMetrics.GCStats.PauseQuantiles)
  58. r.Register("debug.GCStats.PauseTotal", debugMetrics.GCStats.PauseTotal)
  59. r.Register("debug.ReadGCStats", debugMetrics.ReadGCStats)
  60. }