|
| 1 | +package monitoring |
| 2 | + |
| 3 | +import ( |
| 4 | + "hash/maphash" |
| 5 | + "math/rand" |
| 6 | + "sort" |
| 7 | + "time" |
| 8 | +) |
| 9 | + |
| 10 | +type bucketsReservoirSampleType map[key]*metricReservoirSample |
| 11 | + |
| 12 | +type metricReservoirSample struct { |
| 13 | + metric Metric |
| 14 | + reservoir reservoirSampling |
| 15 | +} |
| 16 | + |
| 17 | +type MetricsReservoirSampling struct { |
| 18 | + hash maphash.Hash |
| 19 | + metricsBuckets bucketsReservoirSampleType |
| 20 | +} |
| 21 | + |
| 22 | +func NewMetricsReservoirSampling() *MetricsReservoirSampling { |
| 23 | + metrics := &MetricsReservoirSampling{} |
| 24 | + metrics.metricsBuckets = make(bucketsReservoirSampleType) |
| 25 | + return metrics |
| 26 | +} |
| 27 | + |
| 28 | +func metricValueToFloat64(value interface{}) float64 { |
| 29 | + var asserted float64 |
| 30 | + switch v := value.(type) { |
| 31 | + case int64: |
| 32 | + asserted = float64(v) |
| 33 | + case uint64: |
| 34 | + asserted = float64(v) |
| 35 | + case float64: |
| 36 | + asserted = v |
| 37 | + } |
| 38 | + return asserted |
| 39 | +} |
| 40 | + |
| 41 | +func (this *MetricsReservoirSampling) AddMetric(metric *Metric) { |
| 42 | + for valueKey, value := range metric.Values { |
| 43 | + metricNameTagsToHash(&this.hash, metric) |
| 44 | + this.hash.WriteString(valueKey) |
| 45 | + k := key{nameTagsHash: hashValueAndReset(&this.hash), timestamp: time.Unix(metric.Timestamp.Unix(), 0)} |
| 46 | + if storedMetric, ok := this.metricsBuckets[k]; !ok { |
| 47 | + newReservoir := newReservoirSampling(valueKey, 1000) |
| 48 | + newReservoir.AddPoint(metricValueToFloat64(value)) |
| 49 | + this.metricsBuckets[k] = &metricReservoirSample{metric: *metric, reservoir: newReservoir} |
| 50 | + } else { |
| 51 | + storedMetric.reservoir.AddPoint(metricValueToFloat64(value)) |
| 52 | + } |
| 53 | + } |
| 54 | +} |
| 55 | + |
| 56 | +func (this *MetricsReservoirSampling) Clear() { |
| 57 | + this.hash.Reset() |
| 58 | + clear(this.metricsBuckets) |
| 59 | +} |
| 60 | + |
| 61 | +func (this *MetricsReservoirSampling) GetMetrics() []Metric { |
| 62 | + var result []Metric |
| 63 | + for key, reservoirMetric := range this.metricsBuckets { |
| 64 | + m := Metric{Name: reservoirMetric.metric.Name, Tags: reservoirMetric.metric.Tags, Timestamp: key.timestamp} |
| 65 | + |
| 66 | + mean, median, min, p10, p30, p70, p90, max, count, poolSize := reservoirMetric.reservoir.GetStats() |
| 67 | + |
| 68 | + m.AddValueFloat64(reservoirMetric.reservoir.name+"_mean", mean) |
| 69 | + m.AddValueFloat64(reservoirMetric.reservoir.name+"_median", median) |
| 70 | + m.AddValueFloat64(reservoirMetric.reservoir.name+"_min", min) |
| 71 | + m.AddValueFloat64(reservoirMetric.reservoir.name+"_p10", p10) |
| 72 | + m.AddValueFloat64(reservoirMetric.reservoir.name+"_p30", p30) |
| 73 | + m.AddValueFloat64(reservoirMetric.reservoir.name+"_p70", p70) |
| 74 | + m.AddValueFloat64(reservoirMetric.reservoir.name+"_p90", p90) |
| 75 | + m.AddValueFloat64(reservoirMetric.reservoir.name+"_max", max) |
| 76 | + m.AddValueUInt64(reservoirMetric.reservoir.name+"_count", count) |
| 77 | + m.AddValueUInt64(reservoirMetric.reservoir.name+"_poolsize", poolSize) |
| 78 | + |
| 79 | + result = append(result, m) |
| 80 | + } |
| 81 | + return result |
| 82 | +} |
| 83 | + |
| 84 | +type reservoirSampling struct { |
| 85 | + samples []float64 |
| 86 | + samplesLimit uint64 |
| 87 | + name string |
| 88 | + countSinceReset uint64 |
| 89 | +} |
| 90 | + |
| 91 | +func newReservoirSampling(name string, limit uint64) reservoirSampling { |
| 92 | + return reservoirSampling{ |
| 93 | + samples: make([]float64, 0, limit), |
| 94 | + samplesLimit: limit, |
| 95 | + name: name, |
| 96 | + countSinceReset: 0, |
| 97 | + } |
| 98 | +} |
| 99 | + |
| 100 | +// https://en.wikipedia.org/wiki/Reservoir_sampling |
| 101 | +func (this *reservoirSampling) AddPoint(val float64) { |
| 102 | + this.countSinceReset += 1 |
| 103 | + if len(this.samples) < int(this.samplesLimit) { |
| 104 | + this.samples = append(this.samples, val) |
| 105 | + } else { |
| 106 | + if j := rand.Int63n(int64(this.countSinceReset)); j < int64(len(this.samples)) { |
| 107 | + this.samples[j] = val |
| 108 | + } |
| 109 | + } |
| 110 | +} |
| 111 | + |
| 112 | +func (this *reservoirSampling) indexForPercentile(percentile int) int { |
| 113 | + return int(float64(len(this.samples)) * 0.01 * float64(percentile)) |
| 114 | +} |
| 115 | + |
| 116 | +func (this *reservoirSampling) Reset() { |
| 117 | + this.samples = this.samples[:0] |
| 118 | + this.countSinceReset = 0 |
| 119 | +} |
| 120 | + |
| 121 | +func (this *reservoirSampling) GetStats() (mean float64, median float64, min float64, percentile10 float64, percentile30 float64, percentile70 float64, percentile90 float64, max float64, count uint64, poolSize uint64) { |
| 122 | + sort.Slice(this.samples, func(i, j int) bool { return this.samples[i] < this.samples[j] }) |
| 123 | + |
| 124 | + samplesCount := len(this.samples) |
| 125 | + if samplesCount == 0 { |
| 126 | + return 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 |
| 127 | + } |
| 128 | + |
| 129 | + var sum float64 |
| 130 | + for _, val := range this.samples { |
| 131 | + sum += float64(val) |
| 132 | + } |
| 133 | + |
| 134 | + return sum / float64(samplesCount), |
| 135 | + this.samples[this.indexForPercentile(50)], |
| 136 | + this.samples[0], |
| 137 | + this.samples[this.indexForPercentile(10)], |
| 138 | + this.samples[this.indexForPercentile(30)], |
| 139 | + this.samples[this.indexForPercentile(70)], |
| 140 | + this.samples[this.indexForPercentile(90)], |
| 141 | + this.samples[len(this.samples)-1], |
| 142 | + this.countSinceReset, |
| 143 | + uint64(len(this.samples)) |
| 144 | +} |
0 commit comments