histogram.go 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. package metrics
  2. // Histograms calculate distribution statistics from a series of int64 values.
  3. type Histogram interface {
  4. Clear()
  5. Count() int64
  6. Max() int64
  7. Mean() float64
  8. Min() int64
  9. Percentile(float64) float64
  10. Percentiles([]float64) []float64
  11. Sample() Sample
  12. Snapshot() Histogram
  13. StdDev() float64
  14. Sum() int64
  15. Update(int64)
  16. Variance() float64
  17. }
  18. // GetOrRegisterHistogram returns an existing Histogram or constructs and
  19. // registers a new StandardHistogram.
  20. func GetOrRegisterHistogram(name string, r Registry, s Sample) Histogram {
  21. if nil == r {
  22. r = DefaultRegistry
  23. }
  24. return r.GetOrRegister(name, func() Histogram { return NewHistogram(s) }).(Histogram)
  25. }
  26. // NewHistogram constructs a new StandardHistogram from a Sample.
  27. func NewHistogram(s Sample) Histogram {
  28. if !Enabled {
  29. return NilHistogram{}
  30. }
  31. return &StandardHistogram{sample: s}
  32. }
  33. // NewRegisteredHistogram constructs and registers a new StandardHistogram from
  34. // a Sample.
  35. func NewRegisteredHistogram(name string, r Registry, s Sample) Histogram {
  36. c := NewHistogram(s)
  37. if nil == r {
  38. r = DefaultRegistry
  39. }
  40. r.Register(name, c)
  41. return c
  42. }
  43. // HistogramSnapshot is a read-only copy of another Histogram.
  44. type HistogramSnapshot struct {
  45. sample *SampleSnapshot
  46. }
  47. // Clear panics.
  48. func (*HistogramSnapshot) Clear() {
  49. panic("Clear called on a HistogramSnapshot")
  50. }
  51. // Count returns the number of samples recorded at the time the snapshot was
  52. // taken.
  53. func (h *HistogramSnapshot) Count() int64 { return h.sample.Count() }
  54. // Max returns the maximum value in the sample at the time the snapshot was
  55. // taken.
  56. func (h *HistogramSnapshot) Max() int64 { return h.sample.Max() }
  57. // Mean returns the mean of the values in the sample at the time the snapshot
  58. // was taken.
  59. func (h *HistogramSnapshot) Mean() float64 { return h.sample.Mean() }
  60. // Min returns the minimum value in the sample at the time the snapshot was
  61. // taken.
  62. func (h *HistogramSnapshot) Min() int64 { return h.sample.Min() }
  63. // Percentile returns an arbitrary percentile of values in the sample at the
  64. // time the snapshot was taken.
  65. func (h *HistogramSnapshot) Percentile(p float64) float64 {
  66. return h.sample.Percentile(p)
  67. }
  68. // Percentiles returns a slice of arbitrary percentiles of values in the sample
  69. // at the time the snapshot was taken.
  70. func (h *HistogramSnapshot) Percentiles(ps []float64) []float64 {
  71. return h.sample.Percentiles(ps)
  72. }
  73. // Sample returns the Sample underlying the histogram.
  74. func (h *HistogramSnapshot) Sample() Sample { return h.sample }
  75. // Snapshot returns the snapshot.
  76. func (h *HistogramSnapshot) Snapshot() Histogram { return h }
  77. // StdDev returns the standard deviation of the values in the sample at the
  78. // time the snapshot was taken.
  79. func (h *HistogramSnapshot) StdDev() float64 { return h.sample.StdDev() }
  80. // Sum returns the sum in the sample at the time the snapshot was taken.
  81. func (h *HistogramSnapshot) Sum() int64 { return h.sample.Sum() }
  82. // Update panics.
  83. func (*HistogramSnapshot) Update(int64) {
  84. panic("Update called on a HistogramSnapshot")
  85. }
  86. // Variance returns the variance of inputs at the time the snapshot was taken.
  87. func (h *HistogramSnapshot) Variance() float64 { return h.sample.Variance() }
  88. // NilHistogram is a no-op Histogram.
  89. type NilHistogram struct{}
  90. // Clear is a no-op.
  91. func (NilHistogram) Clear() {}
  92. // Count is a no-op.
  93. func (NilHistogram) Count() int64 { return 0 }
  94. // Max is a no-op.
  95. func (NilHistogram) Max() int64 { return 0 }
  96. // Mean is a no-op.
  97. func (NilHistogram) Mean() float64 { return 0.0 }
  98. // Min is a no-op.
  99. func (NilHistogram) Min() int64 { return 0 }
  100. // Percentile is a no-op.
  101. func (NilHistogram) Percentile(p float64) float64 { return 0.0 }
  102. // Percentiles is a no-op.
  103. func (NilHistogram) Percentiles(ps []float64) []float64 {
  104. return make([]float64, len(ps))
  105. }
  106. // Sample is a no-op.
  107. func (NilHistogram) Sample() Sample { return NilSample{} }
  108. // Snapshot is a no-op.
  109. func (NilHistogram) Snapshot() Histogram { return NilHistogram{} }
  110. // StdDev is a no-op.
  111. func (NilHistogram) StdDev() float64 { return 0.0 }
  112. // Sum is a no-op.
  113. func (NilHistogram) Sum() int64 { return 0 }
  114. // Update is a no-op.
  115. func (NilHistogram) Update(v int64) {}
  116. // Variance is a no-op.
  117. func (NilHistogram) Variance() float64 { return 0.0 }
  118. // StandardHistogram is the standard implementation of a Histogram and uses a
  119. // Sample to bound its memory use.
  120. type StandardHistogram struct {
  121. sample Sample
  122. }
  123. // Clear clears the histogram and its sample.
  124. func (h *StandardHistogram) Clear() { h.sample.Clear() }
  125. // Count returns the number of samples recorded since the histogram was last
  126. // cleared.
  127. func (h *StandardHistogram) Count() int64 { return h.sample.Count() }
  128. // Max returns the maximum value in the sample.
  129. func (h *StandardHistogram) Max() int64 { return h.sample.Max() }
  130. // Mean returns the mean of the values in the sample.
  131. func (h *StandardHistogram) Mean() float64 { return h.sample.Mean() }
  132. // Min returns the minimum value in the sample.
  133. func (h *StandardHistogram) Min() int64 { return h.sample.Min() }
  134. // Percentile returns an arbitrary percentile of the values in the sample.
  135. func (h *StandardHistogram) Percentile(p float64) float64 {
  136. return h.sample.Percentile(p)
  137. }
  138. // Percentiles returns a slice of arbitrary percentiles of the values in the
  139. // sample.
  140. func (h *StandardHistogram) Percentiles(ps []float64) []float64 {
  141. return h.sample.Percentiles(ps)
  142. }
  143. // Sample returns the Sample underlying the histogram.
  144. func (h *StandardHistogram) Sample() Sample { return h.sample }
  145. // Snapshot returns a read-only copy of the histogram.
  146. func (h *StandardHistogram) Snapshot() Histogram {
  147. return &HistogramSnapshot{sample: h.sample.Snapshot().(*SampleSnapshot)}
  148. }
  149. // StdDev returns the standard deviation of the values in the sample.
  150. func (h *StandardHistogram) StdDev() float64 { return h.sample.StdDev() }
  151. // Sum returns the sum in the sample.
  152. func (h *StandardHistogram) Sum() int64 { return h.sample.Sum() }
  153. // Update samples a new value.
  154. func (h *StandardHistogram) Update(v int64) { h.sample.Update(v) }
  155. // Variance returns the variance of the values in the sample.
  156. func (h *StandardHistogram) Variance() float64 { return h.sample.Variance() }