meter.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. package metrics
  2. import "time"
  3. // Meters count events to produce exponentially-weighted moving average rates
  4. // at one-, five-, and fifteen-minutes and a mean rate.
  5. //
  6. // This is an interface so as to encourage other structs to implement
  7. // the Meter API as appropriate.
  8. type Meter interface {
  9. Count() int64
  10. Mark(int64)
  11. Rate1() float64
  12. Rate5() float64
  13. Rate15() float64
  14. RateMean() float64
  15. }
  16. // Create a new Meter. Create the communication channels and start the
  17. // synchronizing goroutine.
  18. func NewMeter() Meter {
  19. if UseNilMetrics {
  20. return NilMeter{}
  21. }
  22. m := &StandardMeter{
  23. make(chan int64),
  24. make(chan meterV),
  25. time.NewTicker(5e9),
  26. }
  27. go m.arbiter()
  28. return m
  29. }
  30. // No-op Meter.
  31. type NilMeter struct{}
  32. // Force the compiler to check that NilMeter implements Meter.
  33. var _ Meter = NilMeter{}
  34. // No-op.
  35. func (m NilMeter) Count() int64 { return 0 }
  36. // No-op.
  37. func (m NilMeter) Mark(n int64) {}
  38. // No-op.
  39. func (m NilMeter) Rate1() float64 { return 0.0 }
  40. // No-op.
  41. func (m NilMeter) Rate5() float64 { return 0.0 }
  42. // No-op.
  43. func (m NilMeter) Rate15() float64 { return 0.0 }
  44. // No-op.
  45. func (m NilMeter) RateMean() float64 { return 0.0 }
  46. // The standard implementation of a Meter uses a goroutine to synchronize
  47. // its calculations and another goroutine (via time.Ticker) to produce
  48. // clock ticks.
  49. type StandardMeter struct {
  50. in chan int64
  51. out chan meterV
  52. ticker *time.Ticker
  53. }
  54. // Force the compiler to check that StandardMeter implements Meter.
  55. var _ Meter = &StandardMeter{}
  56. // Return the count of events seen.
  57. func (m *StandardMeter) Count() int64 {
  58. return (<-m.out).count
  59. }
  60. // Mark the occurance of n events.
  61. func (m *StandardMeter) Mark(n int64) {
  62. m.in <- n
  63. }
  64. // Return the meter's one-minute moving average rate of events.
  65. func (m *StandardMeter) Rate1() float64 {
  66. return (<-m.out).rate1
  67. }
  68. // Return the meter's five-minute moving average rate of events.
  69. func (m *StandardMeter) Rate5() float64 {
  70. return (<-m.out).rate5
  71. }
  72. // Return the meter's fifteen-minute moving average rate of events.
  73. func (m *StandardMeter) Rate15() float64 {
  74. return (<-m.out).rate15
  75. }
  76. // Return the meter's mean rate of events.
  77. func (m *StandardMeter) RateMean() float64 {
  78. return (<-m.out).rateMean
  79. }
  80. // Receive inputs and send outputs. Count each input and update the various
  81. // moving averages and the mean rate of events. Send a copy of the meterV
  82. // as output.
  83. func (m *StandardMeter) arbiter() {
  84. var mv meterV
  85. a1 := NewEWMA1()
  86. a5 := NewEWMA5()
  87. a15 := NewEWMA15()
  88. t := time.Now()
  89. for {
  90. select {
  91. case n := <-m.in:
  92. mv.count += n
  93. a1.Update(n)
  94. mv.rate1 = a1.Rate()
  95. a5.Update(n)
  96. mv.rate5 = a5.Rate()
  97. a15.Update(n)
  98. mv.rate15 = a15.Rate()
  99. mv.rateMean = float64(1e9*mv.count) / float64(time.Since(t))
  100. case m.out <- mv:
  101. case <-m.ticker.C:
  102. a1.Tick()
  103. a5.Tick()
  104. a15.Tick()
  105. }
  106. }
  107. }
  108. // A meterV contains all the values that would need to be passed back
  109. // from the synchronizing goroutine.
  110. type meterV struct {
  111. count int64
  112. rate1, rate5, rate15, rateMean float64
  113. }