metrics.go 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. // Go port of Coda Hale's Metrics library
  2. //
  3. // <https://github.com/rcrowley/go-metrics>
  4. //
  5. // Coda Hale's original work: <https://github.com/codahale/metrics>
  6. package metrics
  7. import (
  8. "os"
  9. "runtime"
  10. "strings"
  11. "time"
  12. "github.com/ethereum/go-ethereum/log"
  13. )
  14. // Enabled is checked by the constructor functions for all of the
  15. // standard metrics. If it is true, the metric returned is a stub.
  16. //
  17. // This global kill-switch helps quantify the observer effect and makes
  18. // for less cluttered pprof profiles.
  19. var Enabled = false
  20. // EnabledExpensive is a soft-flag meant for external packages to check if costly
  21. // metrics gathering is allowed or not. The goal is to separate standard metrics
  22. // for health monitoring and debug metrics that might impact runtime performance.
  23. var EnabledExpensive = false
  24. // enablerFlags is the CLI flag names to use to enable metrics collections.
  25. var enablerFlags = []string{"metrics", "dashboard"}
  26. // expensiveEnablerFlags is the CLI flag names to use to enable metrics collections.
  27. var expensiveEnablerFlags = []string{"metrics.expensive"}
  28. // Init enables or disables the metrics system. Since we need this to run before
  29. // any other code gets to create meters and timers, we'll actually do an ugly hack
  30. // and peek into the command line args for the metrics flag.
  31. func init() {
  32. for _, arg := range os.Args {
  33. flag := strings.TrimLeft(arg, "-")
  34. for _, enabler := range enablerFlags {
  35. if !Enabled && flag == enabler {
  36. log.Info("Enabling metrics collection")
  37. Enabled = true
  38. }
  39. }
  40. for _, enabler := range expensiveEnablerFlags {
  41. if !EnabledExpensive && flag == enabler {
  42. log.Info("Enabling expensive metrics collection")
  43. EnabledExpensive = true
  44. }
  45. }
  46. }
  47. }
  48. // CollectProcessMetrics periodically collects various metrics about the running
  49. // process.
  50. func CollectProcessMetrics(refresh time.Duration) {
  51. // Short circuit if the metrics system is disabled
  52. if !Enabled {
  53. return
  54. }
  55. // Create the various data collectors
  56. memstats := make([]*runtime.MemStats, 2)
  57. diskstats := make([]*DiskStats, 2)
  58. for i := 0; i < len(memstats); i++ {
  59. memstats[i] = new(runtime.MemStats)
  60. diskstats[i] = new(DiskStats)
  61. }
  62. // Define the various metrics to collect
  63. memAllocs := GetOrRegisterMeter("system/memory/allocs", DefaultRegistry)
  64. memFrees := GetOrRegisterMeter("system/memory/frees", DefaultRegistry)
  65. memInuse := GetOrRegisterMeter("system/memory/inuse", DefaultRegistry)
  66. memPauses := GetOrRegisterMeter("system/memory/pauses", DefaultRegistry)
  67. var diskReads, diskReadBytes, diskWrites, diskWriteBytes Meter
  68. var diskReadBytesCounter, diskWriteBytesCounter Counter
  69. if err := ReadDiskStats(diskstats[0]); err == nil {
  70. diskReads = GetOrRegisterMeter("system/disk/readcount", DefaultRegistry)
  71. diskReadBytes = GetOrRegisterMeter("system/disk/readdata", DefaultRegistry)
  72. diskReadBytesCounter = GetOrRegisterCounter("system/disk/readbytes", DefaultRegistry)
  73. diskWrites = GetOrRegisterMeter("system/disk/writecount", DefaultRegistry)
  74. diskWriteBytes = GetOrRegisterMeter("system/disk/writedata", DefaultRegistry)
  75. diskWriteBytesCounter = GetOrRegisterCounter("system/disk/writebytes", DefaultRegistry)
  76. } else {
  77. log.Debug("Failed to read disk metrics", "err", err)
  78. }
  79. // Iterate loading the different stats and updating the meters
  80. for i := 1; ; i++ {
  81. location1 := i % 2
  82. location2 := (i - 1) % 2
  83. runtime.ReadMemStats(memstats[location1])
  84. memAllocs.Mark(int64(memstats[location1].Mallocs - memstats[location2].Mallocs))
  85. memFrees.Mark(int64(memstats[location1].Frees - memstats[location2].Frees))
  86. memInuse.Mark(int64(memstats[location1].Alloc - memstats[location2].Alloc))
  87. memPauses.Mark(int64(memstats[location1].PauseTotalNs - memstats[location2].PauseTotalNs))
  88. if ReadDiskStats(diskstats[location1]) == nil {
  89. diskReads.Mark(diskstats[location1].ReadCount - diskstats[location2].ReadCount)
  90. diskReadBytes.Mark(diskstats[location1].ReadBytes - diskstats[location2].ReadBytes)
  91. diskWrites.Mark(diskstats[location1].WriteCount - diskstats[location2].WriteCount)
  92. diskWriteBytes.Mark(diskstats[location1].WriteBytes - diskstats[location2].WriteBytes)
  93. diskReadBytesCounter.Inc(diskstats[location1].ReadBytes - diskstats[location2].ReadBytes)
  94. diskWriteBytesCounter.Inc(diskstats[location1].WriteBytes - diskstats[location2].WriteBytes)
  95. }
  96. time.Sleep(refresh)
  97. }
  98. }