-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathm_histogram.go
88 lines (78 loc) · 1.93 KB
/
m_histogram.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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
package metrics
import (
client "github.com/influxdata/influxdb1-client"
"github.com/rcrowley/go-metrics"
)
// Histogram implements go-metrics.Histogram and possibly adds a bit functionality.
type Histogram interface {
metrics.Histogram
}
func newHistogram(name string, options ...Option) *histogram {
m := newMetric(name, options...)
m.suffix = suffHistogram
// was a metric provided? if not create new one.
mtrx, ok := m.metric.(metrics.Histogram)
if !ok {
mtrx = metrics.NewHistogram(metrics.NewUniformSample(100))
}
t := &histogram{
baseMetric: *m,
Histogram: mtrx,
fieldName: m.name + m.suffix,
percentiles: []float64{0.5, 0.75, 0.95, 0.99, 0.999, 0.9999},
buckets: []string{count, max, mean, min, p50, p75, p95, p99, p999, p9999,
stddev, variance},
}
t.bucketTags = buildBucketTags(t.buckets, t.tags)
t.bucketVals = buildBucketVals(t.buckets, t.fieldName)
return m.register(t).(*histogram)
}
type histogram struct {
metrics.Histogram
baseMetric
fieldName string
percentiles []float64
buckets []string
bucketTags map[string]map[string]string
bucketVals map[string]map[string]interface{}
}
// AddPoints adds points to be written to the db.
func (s *histogram) AddPoints(pts []client.Point) []client.Point {
var (
ms = s.Histogram.Snapshot()
pct = ms.Percentiles(s.percentiles)
)
for _, bucket := range s.buckets {
var val float64
switch bucket {
case count:
val = float64(ms.Count())
case max:
val = float64(ms.Max())
case mean:
val = ms.Mean()
case min:
val = float64(ms.Min())
case stddev:
val = ms.StdDev()
case variance:
val = ms.Variance()
case p50:
val = pct[0]
case p75:
val = pct[1]
case p95:
val = pct[2]
case p99:
val = pct[3]
case p999:
val = pct[4]
case p9999:
val = pct[5]
}
fields := s.bucketVals[bucket]
fields[s.fieldName] = val
pts = append(pts, getPoint(s.measurement, fields, s.bucketTags[bucket]))
}
return pts
}