counter.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. /*
  2. Copyright 2019 The Vitess Authors.
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package stats
  14. import (
  15. "strconv"
  16. "git.nspix.com/golang/micro/sync"
  17. )
  18. // logCounterNegative is for throttling adding a negative value to a counter messages in logs
  19. // Counter tracks a cumulative count of a metric.
  20. // For a one-dimensional or multi-dimensional counter, please use
  21. // CountersWithSingleLabel or CountersWithMultiLabels instead.
  22. type Counter struct {
  23. i sync.AtomicInt64
  24. help string
  25. }
  26. // NewCounter returns a new Counter.
  27. func NewCounter(name string, help string) *Counter {
  28. v := &Counter{help: help}
  29. if name != "" {
  30. publish(name, v)
  31. }
  32. return v
  33. }
  34. // Add adds the provided value to the Counter.
  35. func (v *Counter) Add(delta int64) {
  36. v.i.Add(delta)
  37. }
  38. // Reset resets the counter value to 0.
  39. func (v *Counter) Reset() {
  40. v.i.Set(int64(0))
  41. }
  42. // Get returns the value.
  43. func (v *Counter) Get() int64 {
  44. return v.i.Get()
  45. }
  46. // String implements the expvar.Var interface.
  47. func (v *Counter) String() string {
  48. return strconv.FormatInt(v.i.Get(), 10)
  49. }
  50. // Help returns the help string.
  51. func (v *Counter) Help() string {
  52. return v.help
  53. }
  54. // CounterFunc allows to provide the counter value via a custom function.
  55. // For implementations that differentiate between Counters/Gauges,
  56. // CounterFunc's values only go up (or are reset to 0).
  57. type CounterFunc struct {
  58. F func() int64
  59. help string
  60. }
  61. // NewCounterFunc creates a new CounterFunc instance and publishes it if name is
  62. // set.
  63. func NewCounterFunc(name string, help string, f func() int64) *CounterFunc {
  64. c := &CounterFunc{
  65. F: f,
  66. help: help,
  67. }
  68. if name != "" {
  69. publish(name, c)
  70. }
  71. return c
  72. }
  73. // Help returns the help string.
  74. func (cf CounterFunc) Help() string {
  75. return cf.help
  76. }
  77. // Get returns the value.
  78. func (cf CounterFunc) Get() int64 {
  79. return cf.F()
  80. }
  81. // String implements expvar.Var.
  82. func (cf CounterFunc) String() string {
  83. return strconv.FormatInt(cf.F(), 10)
  84. }
  85. // Gauge tracks the current value of an integer metric.
  86. // The emphasis here is on *current* i.e. this is not a cumulative counter.
  87. // For a one-dimensional or multi-dimensional gauge, please use
  88. // GaugeWithSingleLabel or GaugesWithMultiLabels instead.
  89. type Gauge struct {
  90. Counter
  91. }
  92. // NewGauge creates a new Gauge and publishes it if name is set.
  93. func NewGauge(name string, help string) *Gauge {
  94. v := &Gauge{Counter: Counter{help: help}}
  95. if name != "" {
  96. publish(name, v)
  97. }
  98. return v
  99. }
  100. // Set overwrites the current value.
  101. func (v *Gauge) Set(value int64) {
  102. v.Counter.i.Set(value)
  103. }
  104. // Add adds the provided value to the Gauge.
  105. func (v *Gauge) Add(delta int64) {
  106. v.Counter.i.Add(delta)
  107. }
  108. // GaugeFunc is the same as CounterFunc but meant for gauges.
  109. // It's a wrapper around CounterFunc for values that go up/down for
  110. // implementations (like Prometheus) that need to differ between Counters and
  111. // Gauges.
  112. type GaugeFunc struct {
  113. CounterFunc
  114. }
  115. // NewGaugeFunc creates a new GaugeFunc instance and publishes it if name is
  116. // set.
  117. func NewGaugeFunc(name string, help string, f func() int64) *GaugeFunc {
  118. i := &GaugeFunc{
  119. CounterFunc: CounterFunc{
  120. F: f,
  121. help: help,
  122. }}
  123. if name != "" {
  124. publish(name, i)
  125. }
  126. return i
  127. }