This repository has been archived by the owner on Aug 23, 2023. It is now read-only.
/
latencyhistogram12h32.go
52 lines (45 loc) · 1.83 KB
/
latencyhistogram12h32.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
package stats
import (
"time"
"github.com/Dieterbe/artisanalhistogram/hist12h"
)
// tracks latency measurements in a given range as 32 bit counters
type LatencyHistogram12h32 struct {
hist hist12h.Hist12h
since time.Time
name []byte
tags []byte
}
func NewLatencyHistogram12h32(name string) *LatencyHistogram12h32 {
return NewLatencyHistogram12h32WithTags(name, "")
}
func NewLatencyHistogram12h32WithTags(name, tags string) *LatencyHistogram12h32 {
return registry.getOrAdd(name+tags, &LatencyHistogram12h32{
hist: hist12h.New(),
since: time.Now(),
name: []byte(name),
tags: []byte(tags),
},
).(*LatencyHistogram12h32)
}
func (l *LatencyHistogram12h32) Value(t time.Duration) {
l.hist.AddDuration(t)
}
func (l *LatencyHistogram12h32) WriteGraphiteLine(buf, prefix []byte, now time.Time) []byte {
snap := l.hist.Snapshot()
// TODO: once we can actually do cool stuff (e.g. visualize) histogram bucket data, report it
// for now, only report the summaries :(
r, ok := l.hist.Report(snap)
if ok {
buf = WriteUint32(buf, prefix, l.name, []byte(".latency.min.gauge32"), l.tags, r.Min/1000, now)
buf = WriteUint32(buf, prefix, l.name, []byte(".latency.mean.gauge32"), l.tags, r.Mean/1000, now)
buf = WriteUint32(buf, prefix, l.name, []byte(".latency.median.gauge32"), l.tags, r.Median/1000, now)
buf = WriteUint32(buf, prefix, l.name, []byte(".latency.p75.gauge32"), l.tags, r.P75/1000, now)
buf = WriteUint32(buf, prefix, l.name, []byte(".latency.p90.gauge32"), l.tags, r.P90/1000, now)
buf = WriteUint32(buf, prefix, l.name, []byte(".latency.max.gauge32"), l.tags, r.Max/1000, now)
}
buf = WriteUint32(buf, prefix, l.name, []byte(".values.count32"), l.tags, r.Count, now)
buf = WriteFloat64(buf, prefix, l.name, []byte(".values.rate32"), l.tags, float64(r.Count)/now.Sub(l.since).Seconds(), now)
l.since = now
return buf
}