runtime.go 4.1 KB

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