runtime.go 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. package metrics
  2. import (
  3. "runtime"
  4. "time"
  5. )
  6. // Capture new values for the Go runtime statistics exported in
  7. // runtime.MemStats. This is designed to be called as a goroutine.
  8. func CaptureRuntimeMemStats(r Registry, interval int64, readMemStats bool) {
  9. for {
  10. CaptureRuntimeMemStatsOnce(r, readMemStats)
  11. time.Sleep(time.Duration(int64(1e9) * int64(interval)))
  12. }
  13. }
  14. // Capture new values for the Go runtime statistics exported in
  15. // runtime.MemStats. This is designed to be called in a background
  16. // goroutine. Giving a registry which has not been given to
  17. // RegisterRuntimeMemStats will panic. If the second parameter is
  18. // false, the counters will be left to the lazy updates provided by
  19. // the runtime.
  20. func CaptureRuntimeMemStatsOnce(r Registry, readMemStats bool) {
  21. var m runtime.MemStats
  22. if readMemStats {
  23. runtime.ReadMemStats(&m)
  24. }
  25. r.Get("runtime.MemStats.Alloc").(Gauge).Update(int64(m.Alloc))
  26. r.Get("runtime.MemStats.TotalAlloc").(Gauge).Update(int64(m.TotalAlloc))
  27. r.Get("runtime.MemStats.Sys").(Gauge).Update(int64(m.Sys))
  28. r.Get("runtime.MemStats.Lookups").(Gauge).Update(int64(m.Lookups))
  29. r.Get("runtime.MemStats.Mallocs").(Gauge).Update(int64(m.Mallocs))
  30. r.Get("runtime.MemStats.Frees").(Gauge).Update(int64(m.Frees))
  31. r.Get("runtime.MemStats.HeapAlloc").(Gauge).Update(int64(m.HeapAlloc))
  32. r.Get("runtime.MemStats.HeapSys").(Gauge).Update(int64(m.HeapSys))
  33. r.Get("runtime.MemStats.HeapIdle").(Gauge).Update(int64(m.HeapIdle))
  34. r.Get("runtime.MemStats.HeapInuse").(Gauge).Update(int64(m.HeapInuse))
  35. r.Get("runtime.MemStats.HeapObjects").(Gauge).Update(int64(m.HeapObjects))
  36. r.Get("runtime.MemStats.StackInuse").(Gauge).Update(int64(m.StackInuse))
  37. r.Get("runtime.MemStats.StackSys").(Gauge).Update(int64(m.StackSys))
  38. r.Get("runtime.MemStats.MSpanInuse").(Gauge).Update(int64(m.MSpanInuse))
  39. r.Get("runtime.MemStats.MSpanSys").(Gauge).Update(int64(m.MSpanSys))
  40. r.Get("runtime.MemStats.MCacheInuse").(Gauge).Update(int64(m.MCacheInuse))
  41. r.Get("runtime.MemStats.MCacheSys").(Gauge).Update(int64(m.MCacheSys))
  42. r.Get("runtime.MemStats.BuckHashSys").(Gauge).Update(int64(m.BuckHashSys))
  43. r.Get("runtime.MemStats.NextGC").(Gauge).Update(int64(m.NextGC))
  44. r.Get("runtime.MemStats.PauseTotalNs").(Gauge).Update(int64(m.PauseTotalNs))
  45. r.Get("runtime.MemStats.PauseNs").(Histogram).Update(int64(m.PauseNs[0]))
  46. r.Get("runtime.MemStats.NumGC").(Gauge).Update(int64(m.NumGC))
  47. if m.EnableGC {
  48. r.Get("runtime.MemStats.EnableGC").(Gauge).Update(1)
  49. } else {
  50. r.Get("runtime.MemStats.EnableGC").(Gauge).Update(0)
  51. }
  52. if m.EnableGC {
  53. r.Get("runtime.MemStats.DebugGC").(Gauge).Update(1)
  54. } else {
  55. r.Get("runtime.MemStats.DebugGC").(Gauge).Update(0)
  56. }
  57. r.Get("runtime.NumCgoCall").(Gauge).Update(int64(runtime.NumCgoCall()))
  58. r.Get("runtime.NumGoroutine").(Gauge).Update(int64(runtime.NumGoroutine()))
  59. }
  60. // Register metrics for the Go runtime statistics exported in
  61. // runtime.MemStats. The metrics are named by their fully-qualified
  62. // Go symbols, i.e. runtime.MemStatsAlloc. In addition to
  63. // runtime.MemStats, register the return value of runtime.Goroutines()
  64. // as runtime.Goroutines.
  65. func RegisterRuntimeMemStats(r Registry) {
  66. r.Register("runtime.MemStats.Alloc", NewGauge())
  67. r.Register("runtime.MemStats.TotalAlloc", NewGauge())
  68. r.Register("runtime.MemStats.Sys", NewGauge())
  69. r.Register("runtime.MemStats.Lookups", NewGauge())
  70. r.Register("runtime.MemStats.Mallocs", NewGauge())
  71. r.Register("runtime.MemStats.Frees", NewGauge())
  72. r.Register("runtime.MemStats.HeapAlloc", NewGauge())
  73. r.Register("runtime.MemStats.HeapSys", NewGauge())
  74. r.Register("runtime.MemStats.HeapIdle", NewGauge())
  75. r.Register("runtime.MemStats.HeapInuse", NewGauge())
  76. r.Register("runtime.MemStats.HeapObjects", NewGauge())
  77. r.Register("runtime.MemStats.StackInuse", NewGauge())
  78. r.Register("runtime.MemStats.StackSys", NewGauge())
  79. r.Register("runtime.MemStats.MSpanInuse", NewGauge())
  80. r.Register("runtime.MemStats.MSpanSys", NewGauge())
  81. r.Register("runtime.MemStats.MCacheInuse", NewGauge())
  82. r.Register("runtime.MemStats.MCacheSys", NewGauge())
  83. r.Register("runtime.MemStats.BuckHashSys", NewGauge())
  84. r.Register("runtime.MemStats.NextGC", NewGauge())
  85. r.Register("runtime.MemStats.PauseTotalNs", NewGauge())
  86. r.Register("runtime.MemStats.PauseNs",
  87. NewHistogram(NewExpDecaySample(1028, 0.015)))
  88. r.Register("runtime.MemStats.NumGC", NewGauge())
  89. r.Register("runtime.MemStats.EnableGC", NewGauge())
  90. r.Register("runtime.MemStats.DebugGC", NewGauge())
  91. r.Register("runtime.NumCgoCall", NewGauge())
  92. r.Register("runtime.NumGoroutine", NewGauge())
  93. }