counter.go 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. package metrics
  2. import "github.com/prometheus/client_golang/prometheus"
  3. // Counter is a metrics that can only increment its current count
  4. type Counter interface {
  5. // Inc adds Sum(vs) to the counter. Sum(vs) must be positive.
  6. //
  7. // If len(vs) == 0, increments the counter by 1.
  8. Inc(vs ...float64)
  9. }
  10. // LabeledCounter is counter that must have labels populated before use.
  11. type LabeledCounter interface {
  12. WithValues(vs ...string) Counter
  13. }
  14. type labeledCounter struct {
  15. pc *prometheus.CounterVec
  16. }
  17. func (lc *labeledCounter) WithValues(vs ...string) Counter {
  18. return &counter{pc: lc.pc.WithLabelValues(vs...)}
  19. }
  20. func (lc *labeledCounter) Describe(ch chan<- *prometheus.Desc) {
  21. lc.pc.Describe(ch)
  22. }
  23. func (lc *labeledCounter) Collect(ch chan<- prometheus.Metric) {
  24. lc.pc.Collect(ch)
  25. }
  26. type counter struct {
  27. pc prometheus.Counter
  28. }
  29. func (c *counter) Inc(vs ...float64) {
  30. if len(vs) == 0 {
  31. c.pc.Inc()
  32. }
  33. c.pc.Add(sumFloat64(vs...))
  34. }
  35. func (c *counter) Describe(ch chan<- *prometheus.Desc) {
  36. c.pc.Describe(ch)
  37. }
  38. func (c *counter) Collect(ch chan<- prometheus.Metric) {
  39. c.pc.Collect(ch)
  40. }