metrics.go 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. // Package metrics provides interface collecting performance metrics.
  2. package metrics
  3. import (
  4. "context"
  5. "time"
  6. )
  7. // Gauge tracks single float64 value.
  8. type Gauge interface {
  9. Set(value float64)
  10. Add(value float64)
  11. }
  12. // FuncGauge is Gauge with value provided by callback function.
  13. type FuncGauge interface {
  14. Function() func() float64
  15. }
  16. // IntGauge tracks single int64 value.
  17. type IntGauge interface {
  18. Set(value int64)
  19. Add(value int64)
  20. }
  21. // FuncIntGauge is IntGauge with value provided by callback function.
  22. type FuncIntGauge interface {
  23. Function() func() int64
  24. }
  25. // Counter tracks monotonically increasing value.
  26. type Counter interface {
  27. // Inc increments counter by 1.
  28. Inc()
  29. // Add adds delta to the counter. Delta must be >=0.
  30. Add(delta int64)
  31. }
  32. // FuncCounter is Counter with value provided by callback function.
  33. type FuncCounter interface {
  34. Function() func() int64
  35. }
  36. // Histogram tracks distribution of value.
  37. type Histogram interface {
  38. RecordValue(value float64)
  39. }
  40. // Timer measures durations.
  41. type Timer interface {
  42. RecordDuration(value time.Duration)
  43. }
  44. // DurationBuckets defines buckets of the duration histogram.
  45. type DurationBuckets interface {
  46. // Size returns number of buckets.
  47. Size() int
  48. // MapDuration returns index of the bucket.
  49. //
  50. // index is integer in range [0, Size()).
  51. MapDuration(d time.Duration) int
  52. // UpperBound of the last bucket is always +Inf.
  53. //
  54. // bucketIndex is integer in range [0, Size()-1).
  55. UpperBound(bucketIndex int) time.Duration
  56. }
  57. // Buckets defines intervals of the regular histogram.
  58. type Buckets interface {
  59. // Size returns number of buckets.
  60. Size() int
  61. // MapValue returns index of the bucket.
  62. //
  63. // Index is integer in range [0, Size()).
  64. MapValue(v float64) int
  65. // UpperBound of the last bucket is always +Inf.
  66. //
  67. // bucketIndex is integer in range [0, Size()-1).
  68. UpperBound(bucketIndex int) float64
  69. }
  70. // GaugeVec stores multiple dynamically created gauges.
  71. type GaugeVec interface {
  72. With(map[string]string) Gauge
  73. // Reset deletes all metrics in vector.
  74. Reset()
  75. }
  76. // IntGaugeVec stores multiple dynamically created gauges.
  77. type IntGaugeVec interface {
  78. With(map[string]string) IntGauge
  79. // Reset deletes all metrics in vector.
  80. Reset()
  81. }
  82. // CounterVec stores multiple dynamically created counters.
  83. type CounterVec interface {
  84. With(map[string]string) Counter
  85. // Reset deletes all metrics in vector.
  86. Reset()
  87. }
  88. // TimerVec stores multiple dynamically created timers.
  89. type TimerVec interface {
  90. With(map[string]string) Timer
  91. // Reset deletes all metrics in vector.
  92. Reset()
  93. }
  94. // HistogramVec stores multiple dynamically created histograms.
  95. type HistogramVec interface {
  96. With(map[string]string) Histogram
  97. // Reset deletes all metrics in vector.
  98. Reset()
  99. }
  100. // Registry creates profiling metrics.
  101. type Registry interface {
  102. // WithTags creates new sub-scope, where each metric has tags attached to it.
  103. WithTags(tags map[string]string) Registry
  104. // WithPrefix creates new sub-scope, where each metric has prefix added to it name.
  105. WithPrefix(prefix string) Registry
  106. ComposeName(parts ...string) string
  107. Counter(name string) Counter
  108. CounterVec(name string, labels []string) CounterVec
  109. FuncCounter(name string, function func() int64) FuncCounter
  110. Gauge(name string) Gauge
  111. GaugeVec(name string, labels []string) GaugeVec
  112. FuncGauge(name string, function func() float64) FuncGauge
  113. IntGauge(name string) IntGauge
  114. IntGaugeVec(name string, labels []string) IntGaugeVec
  115. FuncIntGauge(name string, function func() int64) FuncIntGauge
  116. Timer(name string) Timer
  117. TimerVec(name string, labels []string) TimerVec
  118. Histogram(name string, buckets Buckets) Histogram
  119. HistogramVec(name string, buckets Buckets, labels []string) HistogramVec
  120. DurationHistogram(name string, buckets DurationBuckets) Timer
  121. DurationHistogramVec(name string, buckets DurationBuckets, labels []string) TimerVec
  122. }
  123. // CollectPolicy defines how registered gauge metrics are updated via collect func.
  124. type CollectPolicy interface {
  125. RegisteredCounter(counterFunc func() int64) func() int64
  126. RegisteredGauge(gaugeFunc func() float64) func() float64
  127. AddCollect(collect func(ctx context.Context))
  128. }