-
Notifications
You must be signed in to change notification settings - Fork 12
/
histogram.go
130 lines (104 loc) · 3.36 KB
/
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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
package rollup
import (
"strconv"
"sync"
"github.com/prometheus/client_golang/prometheus"
dto "github.com/prometheus/client_model/go"
"github.com/cloudfoundry/metric-store-release/src/pkg/logger"
"github.com/cloudfoundry/metric-store-release/src/pkg/persistence/transform"
"github.com/cloudfoundry/metric-store-release/src/pkg/rpc"
)
type histogramRollup struct {
log *logger.Logger
nodeIndex string
rollupTags []string
histogramsInInterval map[string]struct{}
histograms map[string]prometheus.Histogram
mu sync.Mutex
}
func NewHistogramRollup(log *logger.Logger, nodeIndex string, rollupTags []string) *histogramRollup {
return &histogramRollup{
log: log,
nodeIndex: nodeIndex,
rollupTags: rollupTags,
histogramsInInterval: make(map[string]struct{}),
histograms: make(map[string]prometheus.Histogram),
}
}
func (r *histogramRollup) Record(sourceId string, tags map[string]string, value int64) {
key := keyFromTags(r.rollupTags, sourceId, tags)
r.mu.Lock()
defer r.mu.Unlock()
_, found := r.histograms[key]
if !found {
r.histograms[key] = prometheus.NewHistogram(prometheus.HistogramOpts{
Name: GorouterHttpMetricName + "_duration_seconds",
})
}
r.histograms[key].Observe(transform.NanosecondsToSeconds(value))
r.histogramsInInterval[key] = struct{}{}
}
func (r *histogramRollup) Rollup(timestamp int64) []*PointsBatch {
var batches []*PointsBatch
r.mu.Lock()
defer r.mu.Unlock()
for k := range r.histogramsInInterval {
var points []*rpc.Point
var size int
labels, err := labelsFromKey(k, r.nodeIndex, r.rollupTags, r.log)
if err != nil {
continue
}
m := &dto.Metric{}
_ = r.histograms[k].Write(m)
histogram := m.GetHistogram()
for _, bucket := range histogram.Bucket {
bucketLabels := make(map[string]string)
for key, value := range labels {
bucketLabels[key] = value
}
bucketLabels["le"] = strconv.FormatFloat(*bucket.UpperBound, 'f', -1, 64)
bucketPoint := &rpc.Point{
Name: GorouterHttpMetricName + "_duration_seconds_bucket",
Timestamp: timestamp,
Value: float64(*bucket.CumulativeCount),
Labels: bucketLabels,
}
points = append(points, bucketPoint)
size += bucketPoint.EstimatePointSize()
}
bucketLabels := make(map[string]string)
for key, value := range labels {
bucketLabels[key] = value
}
bucketLabels["le"] = "+Inf"
bucketInfPoint := &rpc.Point{
Name: GorouterHttpMetricName + "_duration_seconds_bucket",
Timestamp: timestamp,
Value: float64(*histogram.SampleCount),
Labels: bucketLabels,
}
histogramCountPoint := &rpc.Point{
Name: GorouterHttpMetricName + "_duration_seconds_count",
Timestamp: timestamp,
Value: float64(*histogram.SampleCount),
Labels: labels,
}
histogramSumPoint := &rpc.Point{
Name: GorouterHttpMetricName + "_duration_seconds_sum",
Timestamp: timestamp,
Value: float64(*histogram.SampleSum),
Labels: labels,
}
points = append(points, bucketInfPoint, histogramCountPoint, histogramSumPoint)
size += bucketInfPoint.EstimatePointSize()
size += histogramCountPoint.EstimatePointSize()
size += histogramSumPoint.EstimatePointSize()
batches = append(batches, &PointsBatch{
Points: points,
Size: size,
})
}
r.histogramsInInterval = make(map[string]struct{})
return batches
}