runtime.go 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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) {
  9. for {
  10. CaptureRuntimeMemStatsOnce(r)
  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.
  18. func CaptureRuntimeMemStatsOnce(r Registry) {
  19. var m runtime.MemStats
  20. runtime.ReadMemStats(&m)
  21. r.Get("runtime.MemStats.Alloc").(Gauge).Update(int64(m.Alloc))
  22. r.Get("runtime.MemStats.TotalAlloc").(Gauge).Update(int64(m.TotalAlloc))
  23. r.Get("runtime.MemStats.Sys").(Gauge).Update(int64(m.Sys))
  24. r.Get("runtime.MemStats.Lookups").(Gauge).Update(int64(m.Lookups))
  25. r.Get("runtime.MemStats.Mallocs").(Gauge).Update(int64(m.Mallocs))
  26. r.Get("runtime.MemStats.Frees").(Gauge).Update(int64(m.Frees))
  27. r.Get("runtime.MemStats.HeapAlloc").(Gauge).Update(int64(m.HeapAlloc))
  28. r.Get("runtime.MemStats.HeapSys").(Gauge).Update(int64(m.HeapSys))
  29. r.Get("runtime.MemStats.HeapIdle").(Gauge).Update(int64(m.HeapIdle))
  30. r.Get("runtime.MemStats.HeapInuse").(Gauge).Update(int64(m.HeapInuse))
  31. r.Get("runtime.MemStats.HeapReleased").(Gauge).Update(int64(m.HeapReleased))
  32. r.Get("runtime.MemStats.HeapObjects").(Gauge).Update(int64(m.HeapObjects))
  33. r.Get("runtime.MemStats.StackInuse").(Gauge).Update(int64(m.StackInuse))
  34. r.Get("runtime.MemStats.StackSys").(Gauge).Update(int64(m.StackSys))
  35. r.Get("runtime.MemStats.MSpanInuse").(Gauge).Update(int64(m.MSpanInuse))
  36. r.Get("runtime.MemStats.MSpanSys").(Gauge).Update(int64(m.MSpanSys))
  37. r.Get("runtime.MemStats.MCacheInuse").(Gauge).Update(int64(m.MCacheInuse))
  38. r.Get("runtime.MemStats.MCacheSys").(Gauge).Update(int64(m.MCacheSys))
  39. r.Get("runtime.MemStats.BuckHashSys").(Gauge).Update(int64(m.BuckHashSys))
  40. r.Get("runtime.MemStats.NextGC").(Gauge).Update(int64(m.NextGC))
  41. r.Get("runtime.MemStats.LastGC").(Gauge).Update(int64(m.LastGC))
  42. r.Get("runtime.MemStats.PauseTotalNs").(Gauge).Update(int64(m.PauseTotalNs))
  43. r.Get("runtime.MemStats.PauseNs").(Histogram).Update(int64(m.PauseNs[0]))
  44. r.Get("runtime.MemStats.NumGC").(Gauge).Update(int64(m.NumGC))
  45. if m.EnableGC {
  46. r.Get("runtime.MemStats.EnableGC").(Gauge).Update(1)
  47. } else {
  48. r.Get("runtime.MemStats.EnableGC").(Gauge).Update(0)
  49. }
  50. if m.EnableGC {
  51. r.Get("runtime.MemStats.DebugGC").(Gauge).Update(1)
  52. } else {
  53. r.Get("runtime.MemStats.DebugGC").(Gauge).Update(0)
  54. }
  55. r.Get("runtime.NumCgoCall").(Gauge).Update(int64(runtime.NumCgoCall()))
  56. r.Get("runtime.NumGoroutine").(Gauge).Update(int64(runtime.NumGoroutine()))
  57. }
  58. // Register metrics for the Go runtime statistics exported in
  59. // runtime.MemStats. The metrics are named by their fully-qualified
  60. // Go symbols, i.e. runtime.MemStatsAlloc. In addition to
  61. // runtime.MemStats, register the return value of runtime.Goroutines()
  62. // as runtime.Goroutines.
  63. func RegisterRuntimeMemStats(r Registry) {
  64. r.Register("runtime.MemStats.Alloc", NewGauge())
  65. r.Register("runtime.MemStats.TotalAlloc", NewGauge())
  66. r.Register("runtime.MemStats.Sys", NewGauge())
  67. r.Register("runtime.MemStats.Lookups", NewGauge())
  68. r.Register("runtime.MemStats.Mallocs", NewGauge())
  69. r.Register("runtime.MemStats.Frees", NewGauge())
  70. r.Register("runtime.MemStats.HeapAlloc", NewGauge())
  71. r.Register("runtime.MemStats.HeapSys", NewGauge())
  72. r.Register("runtime.MemStats.HeapIdle", NewGauge())
  73. r.Register("runtime.MemStats.HeapInuse", NewGauge())
  74. r.Register("runtime.MemStats.HeapReleased", NewGauge())
  75. r.Register("runtime.MemStats.HeapObjects", NewGauge())
  76. r.Register("runtime.MemStats.StackInuse", NewGauge())
  77. r.Register("runtime.MemStats.StackSys", NewGauge())
  78. r.Register("runtime.MemStats.MSpanInuse", NewGauge())
  79. r.Register("runtime.MemStats.MSpanSys", NewGauge())
  80. r.Register("runtime.MemStats.MCacheInuse", NewGauge())
  81. r.Register("runtime.MemStats.MCacheSys", NewGauge())
  82. r.Register("runtime.MemStats.BuckHashSys", NewGauge())
  83. r.Register("runtime.MemStats.NextGC", NewGauge())
  84. r.Register("runtime.MemStats.LastGC", NewGauge())
  85. r.Register("runtime.MemStats.PauseTotalNs", NewGauge())
  86. r.Register("runtime.MemStats.PauseNs", NewHistogram(NewExpDecaySample(1028, 0.015)))
  87. r.Register("runtime.MemStats.NumGC", NewGauge())
  88. r.Register("runtime.MemStats.EnableGC", NewGauge())
  89. r.Register("runtime.MemStats.DebugGC", NewGauge())
  90. r.Register("runtime.NumCgoCall", NewGauge())
  91. r.Register("runtime.NumGoroutine", NewGauge())
  92. }