Skip to content
This repository has been archived by the owner on Aug 23, 2023. It is now read-only.

report accurate mean instead of an approximation #744

Merged
merged 1 commit into from
Nov 22, 2017
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion stats/latencyhistogram15s32.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package stats

import (
"sync/atomic"
"time"

"github.com/Dieterbe/artisanalhistogram/hist15s"
Expand All @@ -10,6 +11,7 @@ import (
type LatencyHistogram15s32 struct {
hist hist15s.Hist15s
since time.Time
sum uint64 // in micros. to generate more accurate mean
}

func NewLatencyHistogram15s32(name string) *LatencyHistogram15s32 {
Expand All @@ -21,6 +23,7 @@ func NewLatencyHistogram15s32(name string) *LatencyHistogram15s32 {
}

func (l *LatencyHistogram15s32) Value(t time.Duration) {
atomic.AddUint64(&l.sum, uint64(t.Nanoseconds()/1000))
l.hist.AddDuration(t)
}

Expand All @@ -30,8 +33,9 @@ func (l *LatencyHistogram15s32) ReportGraphite(prefix, buf []byte, now time.Time
// for now, only report the summaries :(
r, ok := l.hist.Report(snap)
if ok {
sum := atomic.SwapUint64(&l.sum, 0)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you are reseting sum to 0 on every flush, but not count.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

r.Count comes from the report which comes from the histogram snapshot. the histogram is reset when snapshot is taken

buf = WriteUint32(buf, prefix, []byte("latency.min.gauge32"), r.Min/1000, now)
buf = WriteUint32(buf, prefix, []byte("latency.mean.gauge32"), r.Mean/1000, now)
buf = WriteUint32(buf, prefix, []byte("latency.mean.gauge32"), uint32((sum / uint64(r.Count) / 1000)), now)
buf = WriteUint32(buf, prefix, []byte("latency.median.gauge32"), r.Median/1000, now)
buf = WriteUint32(buf, prefix, []byte("latency.p75.gauge32"), r.P75/1000, now)
buf = WriteUint32(buf, prefix, []byte("latency.p90.gauge32"), r.P90/1000, now)
Expand Down