counter.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. package metrics
  2. import "sync/atomic"
  3. // Counters hold an int64 value that can be incremented and decremented.
  4. type Counter interface {
  5. Clear()
  6. Count() int64
  7. Dec(int64)
  8. Inc(int64)
  9. Snapshot() Counter
  10. }
  11. // GetOrRegisterCounter returns an existing Counter or constructs and registers
  12. // a new StandardCounter.
  13. func GetOrRegisterCounter(name string, r Registry) Counter {
  14. if nil == r {
  15. r = DefaultRegistry
  16. }
  17. return r.GetOrRegister(name, NewCounter).(Counter)
  18. }
  19. // NewCounter constructs a new StandardCounter.
  20. func NewCounter() Counter {
  21. if UseNilMetrics {
  22. return NilCounter{}
  23. }
  24. return &StandardCounter{0}
  25. }
  26. // NewRegisteredCounter constructs and registers a new StandardCounter.
  27. func NewRegisteredCounter(name string, r Registry) Counter {
  28. c := NewCounter()
  29. if nil == r {
  30. r = DefaultRegistry
  31. }
  32. r.Register(name, c)
  33. return c
  34. }
  35. // CounterSnapshot is a read-only copy of another Counter.
  36. type CounterSnapshot int64
  37. // Clear panics.
  38. func (CounterSnapshot) Clear() {
  39. panic("Clear called on a CounterSnapshot")
  40. }
  41. // Count returns the count at the time the snapshot was taken.
  42. func (c CounterSnapshot) Count() int64 { return int64(c) }
  43. // Dec panics.
  44. func (CounterSnapshot) Dec(int64) {
  45. panic("Dec called on a CounterSnapshot")
  46. }
  47. // Inc panics.
  48. func (CounterSnapshot) Inc(int64) {
  49. panic("Inc called on a CounterSnapshot")
  50. }
  51. // Snapshot returns the snapshot.
  52. func (c CounterSnapshot) Snapshot() Counter { return c }
  53. // NilCounter is a no-op Counter.
  54. type NilCounter struct{}
  55. // Clear is a no-op.
  56. func (NilCounter) Clear() {}
  57. // Count is a no-op.
  58. func (NilCounter) Count() int64 { return 0 }
  59. // Dec is a no-op.
  60. func (NilCounter) Dec(i int64) {}
  61. // Inc is a no-op.
  62. func (NilCounter) Inc(i int64) {}
  63. // Snapshot is a no-op.
  64. func (NilCounter) Snapshot() Counter { return NilCounter{} }
  65. // StandardCounter is the standard implementation of a Counter and uses the
  66. // sync/atomic package to manage a single int64 value.
  67. type StandardCounter struct {
  68. count int64
  69. }
  70. // Clear sets the counter to zero.
  71. func (c *StandardCounter) Clear() {
  72. atomic.StoreInt64(&c.count, 0)
  73. }
  74. // Count returns the current count.
  75. func (c *StandardCounter) Count() int64 {
  76. return atomic.LoadInt64(&c.count)
  77. }
  78. // Dec decrements the counter by the given amount.
  79. func (c *StandardCounter) Dec(i int64) {
  80. atomic.AddInt64(&c.count, -i)
  81. }
  82. // Inc increments the counter by the given amount.
  83. func (c *StandardCounter) Inc(i int64) {
  84. atomic.AddInt64(&c.count, i)
  85. }
  86. // Snapshot returns a read-only copy of the counter.
  87. func (c *StandardCounter) Snapshot() Counter {
  88. return CounterSnapshot(c.Count())
  89. }