Add sumOfSquares to histogram

This commit is contained in:
Menghan Li
2016-04-13 20:38:42 -07:00
parent e3d8dfd907
commit d8783294dd

View File

@ -15,6 +15,8 @@ type HistogramValue struct {
Count int64 Count int64
// Sum is the sum of all the values added to the histogram. // Sum is the sum of all the values added to the histogram.
Sum int64 Sum int64
// SumOfSquares is the sum of squares of all values.
SumOfSquares int64
// Min is the minimum of all the values added to the histogram. // Min is the minimum of all the values added to the histogram.
Min int64 Min int64
// Max is the maximum of all the values added to the histogram. // Max is the maximum of all the values added to the histogram.
@ -82,6 +84,7 @@ type Histogram struct {
buckets []bucketInternal buckets []bucketInternal
count *Counter count *Counter
sum *Counter sum *Counter
sumOfSquares *Counter
tracker *Tracker tracker *Tracker
} }
@ -120,6 +123,7 @@ func NewHistogram(opts HistogramOptions) *Histogram {
buckets: make([]bucketInternal, opts.NumBuckets), buckets: make([]bucketInternal, opts.NumBuckets),
count: newCounter(), count: newCounter(),
sum: newCounter(), sum: newCounter(),
sumOfSquares: newCounter(),
tracker: newTracker(), tracker: newTracker(),
} }
low := opts.MinValue low := opts.MinValue
@ -147,6 +151,7 @@ func (h *Histogram) Add(value int64) error {
h.buckets[bucket].count.Incr(1) h.buckets[bucket].count.Incr(1)
h.count.Incr(1) h.count.Incr(1)
h.sum.Incr(value) h.sum.Incr(value)
h.sumOfSquares.Incr(value * value)
h.tracker.Push(value) h.tracker.Push(value)
return nil return nil
} }
@ -169,6 +174,7 @@ func (h *Histogram) Value() HistogramValue {
v := HistogramValue{ v := HistogramValue{
Count: h.count.Value(), Count: h.count.Value(),
Sum: h.sum.Value(), Sum: h.sum.Value(),
SumOfSquares: h.sumOfSquares.Value(),
Min: h.tracker.Min(), Min: h.tracker.Min(),
Max: h.tracker.Max(), Max: h.tracker.Max(),
Buckets: b, Buckets: b,
@ -189,6 +195,7 @@ func (h *Histogram) Delta1h() HistogramValue {
v := HistogramValue{ v := HistogramValue{
Count: h.count.Delta1h(), Count: h.count.Delta1h(),
Sum: h.sum.Delta1h(), Sum: h.sum.Delta1h(),
SumOfSquares: h.sumOfSquares.Delta1h(),
Min: h.tracker.Min1h(), Min: h.tracker.Min1h(),
Max: h.tracker.Max1h(), Max: h.tracker.Max1h(),
Buckets: b, Buckets: b,
@ -209,6 +216,7 @@ func (h *Histogram) Delta10m() HistogramValue {
v := HistogramValue{ v := HistogramValue{
Count: h.count.Delta10m(), Count: h.count.Delta10m(),
Sum: h.sum.Delta10m(), Sum: h.sum.Delta10m(),
SumOfSquares: h.sumOfSquares.Delta10m(),
Min: h.tracker.Min10m(), Min: h.tracker.Min10m(),
Max: h.tracker.Max10m(), Max: h.tracker.Max10m(),
Buckets: b, Buckets: b,
@ -229,6 +237,7 @@ func (h *Histogram) Delta1m() HistogramValue {
v := HistogramValue{ v := HistogramValue{
Count: h.count.Delta1m(), Count: h.count.Delta1m(),
Sum: h.sum.Delta1m(), Sum: h.sum.Delta1m(),
SumOfSquares: h.sumOfSquares.Delta1m(),
Min: h.tracker.Min1m(), Min: h.tracker.Min1m(),
Max: h.tracker.Max1m(), Max: h.tracker.Max1m(),
Buckets: b, Buckets: b,