metrics.go 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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 bool = 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. if err := ReadDiskStats(diskstats[0]); err == nil {
  55. diskReads = GetOrRegisterMeter("system/disk/readcount", DefaultRegistry)
  56. diskReadBytes = GetOrRegisterMeter("system/disk/readdata", DefaultRegistry)
  57. diskWrites = GetOrRegisterMeter("system/disk/writecount", DefaultRegistry)
  58. diskWriteBytes = GetOrRegisterMeter("system/disk/writedata", DefaultRegistry)
  59. } else {
  60. log.Debug("Failed to read disk metrics", "err", err)
  61. }
  62. // Iterate loading the different stats and updating the meters
  63. for i := 1; ; i++ {
  64. runtime.ReadMemStats(memstats[i%2])
  65. memAllocs.Mark(int64(memstats[i%2].Mallocs - memstats[(i-1)%2].Mallocs))
  66. memFrees.Mark(int64(memstats[i%2].Frees - memstats[(i-1)%2].Frees))
  67. memInuse.Mark(int64(memstats[i%2].Alloc - memstats[(i-1)%2].Alloc))
  68. memPauses.Mark(int64(memstats[i%2].PauseTotalNs - memstats[(i-1)%2].PauseTotalNs))
  69. if ReadDiskStats(diskstats[i%2]) == nil {
  70. diskReads.Mark(diskstats[i%2].ReadCount - diskstats[(i-1)%2].ReadCount)
  71. diskReadBytes.Mark(diskstats[i%2].ReadBytes - diskstats[(i-1)%2].ReadBytes)
  72. diskWrites.Mark(diskstats[i%2].WriteCount - diskstats[(i-1)%2].WriteCount)
  73. diskWriteBytes.Mark(diskstats[i%2].WriteBytes - diskstats[(i-1)%2].WriteBytes)
  74. }
  75. time.Sleep(refresh)
  76. }
  77. }