metrics.go 3.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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. // MetricsEnabledFlag is the CLI flag name to use to enable metrics collections.
  21. const MetricsEnabledFlag = "metrics"
  22. const DashboardEnabledFlag = "dashboard"
  23. // Init enables or disables the metrics system. Since we need this to run before
  24. // any other code gets to create meters and timers, we'll actually do an ugly hack
  25. // and peek into the command line args for the metrics flag.
  26. func init() {
  27. for _, arg := range os.Args {
  28. if flag := strings.TrimLeft(arg, "-"); flag == MetricsEnabledFlag || flag == DashboardEnabledFlag {
  29. log.Info("Enabling metrics collection")
  30. Enabled = true
  31. }
  32. }
  33. }
  34. // CollectProcessMetrics periodically collects various metrics about the running
  35. // process.
  36. func CollectProcessMetrics(refresh time.Duration) {
  37. // Short circuit if the metrics system is disabled
  38. if !Enabled {
  39. return
  40. }
  41. // Create the various data collectors
  42. memstats := make([]*runtime.MemStats, 2)
  43. diskstats := make([]*DiskStats, 2)
  44. for i := 0; i < len(memstats); i++ {
  45. memstats[i] = new(runtime.MemStats)
  46. diskstats[i] = new(DiskStats)
  47. }
  48. // Define the various metrics to collect
  49. memAllocs := GetOrRegisterMeter("system/memory/allocs", DefaultRegistry)
  50. memFrees := GetOrRegisterMeter("system/memory/frees", DefaultRegistry)
  51. memInuse := GetOrRegisterMeter("system/memory/inuse", DefaultRegistry)
  52. memPauses := GetOrRegisterMeter("system/memory/pauses", DefaultRegistry)
  53. var diskReads, diskReadBytes, diskWrites, diskWriteBytes Meter
  54. var diskReadBytesCounter, diskWriteBytesCounter Counter
  55. if err := ReadDiskStats(diskstats[0]); err == nil {
  56. diskReads = GetOrRegisterMeter("system/disk/readcount", DefaultRegistry)
  57. diskReadBytes = GetOrRegisterMeter("system/disk/readdata", DefaultRegistry)
  58. diskReadBytesCounter = GetOrRegisterCounter("system/disk/readbytes", DefaultRegistry)
  59. diskWrites = GetOrRegisterMeter("system/disk/writecount", DefaultRegistry)
  60. diskWriteBytes = GetOrRegisterMeter("system/disk/writedata", DefaultRegistry)
  61. diskWriteBytesCounter = GetOrRegisterCounter("system/disk/writebytes", DefaultRegistry)
  62. } else {
  63. log.Debug("Failed to read disk metrics", "err", err)
  64. }
  65. // Iterate loading the different stats and updating the meters
  66. for i := 1; ; i++ {
  67. location1 := i % 2
  68. location2 := (i - 1) % 2
  69. runtime.ReadMemStats(memstats[location1])
  70. memAllocs.Mark(int64(memstats[location1].Mallocs - memstats[location2].Mallocs))
  71. memFrees.Mark(int64(memstats[location1].Frees - memstats[location2].Frees))
  72. memInuse.Mark(int64(memstats[location1].Alloc - memstats[location2].Alloc))
  73. memPauses.Mark(int64(memstats[location1].PauseTotalNs - memstats[location2].PauseTotalNs))
  74. if ReadDiskStats(diskstats[location1]) == nil {
  75. diskReads.Mark(diskstats[location1].ReadCount - diskstats[location2].ReadCount)
  76. diskReadBytes.Mark(diskstats[location1].ReadBytes - diskstats[location2].ReadBytes)
  77. diskWrites.Mark(diskstats[location1].WriteCount - diskstats[location2].WriteCount)
  78. diskWriteBytes.Mark(diskstats[location1].WriteBytes - diskstats[location2].WriteBytes)
  79. diskReadBytesCounter.Inc(diskstats[location1].ReadBytes - diskstats[location2].ReadBytes)
  80. diskWriteBytesCounter.Inc(diskstats[location1].WriteBytes - diskstats[location2].WriteBytes)
  81. }
  82. time.Sleep(refresh)
  83. }
  84. }