meter.go 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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. // The standard implementation of a Meter uses a goroutine to synchronize
  17. // its calculations and another goroutine (via time.Ticker) to produce
  18. // clock ticks.
  19. type StandardMeter struct {
  20. in chan int64
  21. out chan meterV
  22. ticker *time.Ticker
  23. }
  24. // A meterV contains all the values that would need to be passed back
  25. // from the synchronizing goroutine.
  26. type meterV struct {
  27. count int64
  28. rate1, rate5, rate15, rateMean float64
  29. }
  30. // Create a new meter. Create the communication channels and start the
  31. // synchronizing goroutine.
  32. func NewMeter() *StandardMeter {
  33. m := &StandardMeter{
  34. make(chan int64),
  35. make(chan meterV),
  36. time.NewTicker(5e9),
  37. }
  38. go m.arbiter()
  39. return m
  40. }
  41. // Return the count of events seen.
  42. func (m *StandardMeter) Count() int64 {
  43. return (<-m.out).count
  44. }
  45. // Mark the occurance of n events.
  46. func (m *StandardMeter) Mark(n int64) {
  47. m.in <- n
  48. }
  49. // Return the meter's one-minute moving average rate of events.
  50. func (m *StandardMeter) Rate1() float64 {
  51. return (<-m.out).rate1
  52. }
  53. // Return the meter's five-minute moving average rate of events.
  54. func (m *StandardMeter) Rate5() float64 {
  55. return (<-m.out).rate5
  56. }
  57. // Return the meter's fifteen-minute moving average rate of events.
  58. func (m *StandardMeter) Rate15() float64 {
  59. return (<-m.out).rate15
  60. }
  61. // Return the meter's mean rate of events.
  62. func (m *StandardMeter) RateMean() float64 {
  63. return (<-m.out).rateMean
  64. }
  65. // Receive inputs and send outputs. Count each input and update the various
  66. // moving averages and the mean rate of events. Send a copy of the meterV
  67. // as output.
  68. func (m *StandardMeter) arbiter() {
  69. var mv meterV
  70. a1 := NewEWMA1()
  71. a5 := NewEWMA5()
  72. a15 := NewEWMA15()
  73. tsStart := time.Now()
  74. for {
  75. select {
  76. case n := <-m.in:
  77. mv.count += n
  78. a1.Update(n)
  79. mv.rate1 = a1.Rate()
  80. a5.Update(n)
  81. mv.rate5 = a5.Rate()
  82. a15.Update(n)
  83. mv.rate15 = a15.Rate()
  84. mv.rateMean = float64(1e9*mv.count) / float64(
  85. time.Now().Sub(tsStart))
  86. case m.out <- mv:
  87. case <-m.ticker.C:
  88. a1.Tick()
  89. a5.Tick()
  90. a15.Tick()
  91. }
  92. }
  93. }