histogram.go 5.7 KB

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