runtime.go 4.3 KB

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