| 1 |
package metrics |
| 2 |
|
| 3 |
|
| 4 |
type Histogram interface { |
| 5 |
Clear() |
| 6 |
Count() int64 |
| 7 |
Max() int64 |
| 8 |
Mean() float64 |
| 9 |
Min() int64 |
| 10 |
Percentile(float64) float64 |
| 11 |
Percentiles([]float64) []float64 |
| 12 |
Sample() Sample |
| 13 |
Snapshot() Histogram |
| 14 |
StdDev() float64 |
| 15 |
Sum() int64 |
| 16 |
Update(int64) |
| 17 |
Variance() float64 |
| 18 |
} |
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
func GetOrRegisterHistogram(name string, r Registry, s Sample) Histogram { |
| 23 |
if nil == r { |
| 24 |
r = DefaultRegistry |
| 25 |
} |
| 26 |
return r.GetOrRegister(name, func() Histogram { return NewHistogram(s) }).(Histogram) |
| 27 |
} |
| 28 |
|
| 29 |
|
| 30 |
func NewHistogram(s Sample) Histogram { |
| 31 |
if UseNilMetrics { |
| 32 |
return NilHistogram{} |
| 33 |
} |
| 34 |
return &StandardHistogram{sample: s} |
| 35 |
} |
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
func NewRegisteredHistogram(name string, r Registry, s Sample) Histogram { |
| 40 |
c := NewHistogram(s) |
| 41 |
if nil == r { |
| 42 |
r = DefaultRegistry |
| 43 |
} |
| 44 |
r.Register(name, c) |
| 45 |
return c |
| 46 |
} |
| 47 |
|
| 48 |
|
| 49 |
type HistogramSnapshot struct { |
| 50 |
sample *SampleSnapshot |
| 51 |
} |
| 52 |
|
| 53 |
|
| 54 |
func (*HistogramSnapshot) Clear() { |
| 55 |
panic("Clear called on a HistogramSnapshot") |
| 56 |
} |
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
func (h *HistogramSnapshot) Count() int64 { return h.sample.Count() } |
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
func (h *HistogramSnapshot) Max() int64 { return h.sample.Max() } |
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
func (h *HistogramSnapshot) Mean() float64 { return h.sample.Mean() } |
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
func (h *HistogramSnapshot) Min() int64 { return h.sample.Min() } |
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
func (h *HistogramSnapshot) Percentile(p float64) float64 { |
| 77 |
return h.sample.Percentile(p) |
| 78 |
} |
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
func (h *HistogramSnapshot) Percentiles(ps []float64) []float64 { |
| 83 |
return h.sample.Percentiles(ps) |
| 84 |
} |
| 85 |
|
| 86 |
|
| 87 |
func (h *HistogramSnapshot) Sample() Sample { return h.sample } |
| 88 |
|
| 89 |
|
| 90 |
func (h *HistogramSnapshot) Snapshot() Histogram { return h } |
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
func (h *HistogramSnapshot) StdDev() float64 { return h.sample.StdDev() } |
| 95 |
|
| 96 |
|
| 97 |
func (h *HistogramSnapshot) Sum() int64 { return h.sample.Sum() } |
| 98 |
|
| 99 |
|
| 100 |
func (*HistogramSnapshot) Update(int64) { |
| 101 |
panic("Update called on a HistogramSnapshot") |
| 102 |
} |
| 103 |
|
| 104 |
|
| 105 |
func (h *HistogramSnapshot) Variance() float64 { return h.sample.Variance() } |
| 106 |
|
| 107 |
|
| 108 |
type NilHistogram struct{} |
| 109 |
|
| 110 |
|
| 111 |
func (NilHistogram) Clear() {} |
| 112 |
|
| 113 |
|
| 114 |
func (NilHistogram) Count() int64 { return 0 } |
| 115 |
|
| 116 |
|
| 117 |
func (NilHistogram) Max() int64 { return 0 } |
| 118 |
|
| 119 |
|
| 120 |
func (NilHistogram) Mean() float64 { return 0.0 } |
| 121 |
|
| 122 |
|
| 123 |
func (NilHistogram) Min() int64 { return 0 } |
| 124 |
|
| 125 |
|
| 126 |
func (NilHistogram) Percentile(p float64) float64 { return 0.0 } |
| 127 |
|
| 128 |
|
| 129 |
func (NilHistogram) Percentiles(ps []float64) []float64 { |
| 130 |
return make([]float64, len(ps)) |
| 131 |
} |
| 132 |
|
| 133 |
|
| 134 |
func (NilHistogram) Sample() Sample { return NilSample{} } |
| 135 |
|
| 136 |
|
| 137 |
func (NilHistogram) Snapshot() Histogram { return NilHistogram{} } |
| 138 |
|
| 139 |
|
| 140 |
func (NilHistogram) StdDev() float64 { return 0.0 } |
| 141 |
|
| 142 |
|
| 143 |
func (NilHistogram) Sum() int64 { return 0 } |
| 144 |
|
| 145 |
|
| 146 |
func (NilHistogram) Update(v int64) {} |
| 147 |
|
| 148 |
|
| 149 |
func (NilHistogram) Variance() float64 { return 0.0 } |
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
type StandardHistogram struct { |
| 154 |
sample Sample |
| 155 |
} |
| 156 |
|
| 157 |
|
| 158 |
func (h *StandardHistogram) Clear() { h.sample.Clear() } |
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
func (h *StandardHistogram) Count() int64 { return h.sample.Count() } |
| 163 |
|
| 164 |
|
| 165 |
func (h *StandardHistogram) Max() int64 { return h.sample.Max() } |
| 166 |
|
| 167 |
|
| 168 |
func (h *StandardHistogram) Mean() float64 { return h.sample.Mean() } |
| 169 |
|
| 170 |
|
| 171 |
func (h *StandardHistogram) Min() int64 { return h.sample.Min() } |
| 172 |
|
| 173 |
|
| 174 |
func (h *StandardHistogram) Percentile(p float64) float64 { |
| 175 |
return h.sample.Percentile(p) |
| 176 |
} |
| 177 |
|
| 178 |
|
| 179 |
|
| 180 |
func (h *StandardHistogram) Percentiles(ps []float64) []float64 { |
| 181 |
return h.sample.Percentiles(ps) |
| 182 |
} |
| 183 |
|
| 184 |
|
| 185 |
func (h *StandardHistogram) Sample() Sample { return h.sample } |
| 186 |
|
| 187 |
|
| 188 |
func (h *StandardHistogram) Snapshot() Histogram { |
| 189 |
return &HistogramSnapshot{sample: h.sample.Snapshot().(*SampleSnapshot)} |
| 190 |
} |
| 191 |
|
| 192 |
|
| 193 |
func (h *StandardHistogram) StdDev() float64 { return h.sample.StdDev() } |
| 194 |
|
| 195 |
|
| 196 |
func (h *StandardHistogram) Sum() int64 { return h.sample.Sum() } |
| 197 |
|
| 198 |
|
| 199 |
func (h *StandardHistogram) Update(v int64) { h.sample.Update(v) } |
| 200 |
|
| 201 |
|
| 202 |
func (h *StandardHistogram) Variance() float64 { return h.sample.Variance() } |