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