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. //exp.Exp(DefaultRegistry)
  34. }
  35. // CollectProcessMetrics periodically collects various metrics about the running
  36. // process.
  37. func CollectProcessMetrics(refresh time.Duration) {
  38. // Short circuit if the metrics system is disabled
  39. if !Enabled {
  40. return
  41. }
  42. // Create the various data collectors
  43. memstats := make([]*runtime.MemStats, 2)
  44. diskstats := make([]*DiskStats, 2)
  45. for i := 0; i < len(memstats); i++ {
  46. memstats[i] = new(runtime.MemStats)
  47. diskstats[i] = new(DiskStats)
  48. }
  49. // Define the various metrics to collect
  50. memAllocs := GetOrRegisterMeter("system/memory/allocs", DefaultRegistry)
  51. memFrees := GetOrRegisterMeter("system/memory/frees", DefaultRegistry)
  52. memInuse := GetOrRegisterMeter("system/memory/inuse", DefaultRegistry)
  53. memPauses := GetOrRegisterMeter("system/memory/pauses", DefaultRegistry)
  54. var diskReads, diskReadBytes, diskWrites, diskWriteBytes Meter
  55. if err := ReadDiskStats(diskstats[0]); err == nil {
  56. diskReads = GetOrRegisterMeter("system/disk/readcount", DefaultRegistry)
  57. diskReadBytes = GetOrRegisterMeter("system/disk/readdata", DefaultRegistry)
  58. diskWrites = GetOrRegisterMeter("system/disk/writecount", DefaultRegistry)
  59. diskWriteBytes = GetOrRegisterMeter("system/disk/writedata", DefaultRegistry)
  60. } else {
  61. log.Debug("Failed to read disk metrics", "err", err)
  62. }
  63. // Iterate loading the different stats and updating the meters
  64. for i := 1; ; i++ {
  65. runtime.ReadMemStats(memstats[i%2])
  66. memAllocs.Mark(int64(memstats[i%2].Mallocs - memstats[(i-1)%2].Mallocs))
  67. memFrees.Mark(int64(memstats[i%2].Frees - memstats[(i-1)%2].Frees))
  68. memInuse.Mark(int64(memstats[i%2].Alloc - memstats[(i-1)%2].Alloc))
  69. memPauses.Mark(int64(memstats[i%2].PauseTotalNs - memstats[(i-1)%2].PauseTotalNs))
  70. if ReadDiskStats(diskstats[i%2]) == nil {
  71. diskReads.Mark(diskstats[i%2].ReadCount - diskstats[(i-1)%2].ReadCount)
  72. diskReadBytes.Mark(diskstats[i%2].ReadBytes - diskstats[(i-1)%2].ReadBytes)
  73. diskWrites.Mark(diskstats[i%2].WriteCount - diskstats[(i-1)%2].WriteCount)
  74. diskWriteBytes.Mark(diskstats[i%2].WriteBytes - diskstats[(i-1)%2].WriteBytes)
  75. }
  76. time.Sleep(refresh)
  77. }
  78. }