-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhistogram.go
More file actions
236 lines (184 loc) · 4.83 KB
/
Copy pathhistogram.go
File metadata and controls
236 lines (184 loc) · 4.83 KB
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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
package stats
import (
"fmt"
"math"
"sync"
)
var defaultCutoffs = []float64{
.005,
.01,
.025,
.05,
.1,
.25,
.5,
1.,
2.5,
5.,
10.,
math.Inf(0),
}
// HistogramValue represents a snapshot of a histogram's state including
// its tags, total count, sum, and bucket counts.
type HistogramValue struct {
Tags map[string]string
Count int64
Sum float64
Buckets []Bucket
}
// HistogramVectorGetter provides read access to histogram vectors for collectors.
type HistogramVectorGetter interface {
// Labels returns the label names for this histogram vector.
Labels() []string
// Cutoffs returns the upper bounds for each bucket.
Cutoffs() []float64
// Get returns all histogram values with their label combinations.
Get() []*HistogramValue
}
// HistogramVector is a multi-dimensional histogram that creates histogram instances
// with specific label values.
type HistogramVector interface {
// WithLabels returns a Histogram with the specified label values.
// The number of values must match the number of labels defined for this vector.
WithLabels(...string) Histogram
}
type histogramVector struct {
labels []string
cutoffs []float64
mu sync.RWMutex
hs map[uint64]*histogram
marshaler labelMarshaler
}
func (hv *histogramVector) Labels() []string { return hv.labels }
func (hv *histogramVector) Cutoffs() []float64 { return hv.cutoffs }
func (hv *histogramVector) buildTags(key uint64) map[string]string {
var tags = make(map[string]string, len(hv.labels))
for i, val := range hv.marshaler.unmarshal(key, len(hv.labels)) {
tags[hv.labels[i]] = val
}
return tags
}
func (hv *histogramVector) Get() []*HistogramValue {
var res = make([]*HistogramValue, 0, len(hv.hs))
for k, h := range hv.hs {
res = append(
res,
&HistogramValue{
Tags: hv.buildTags(k),
Count: h.Count(),
Sum: h.Sum(),
Buckets: h.Buckets(),
},
)
}
return res
}
func (hv *histogramVector) WithLabels(ls ...string) Histogram {
if len(ls) != len(hv.labels) {
panic(
fmt.Sprintf(
"Not the correct number of labels: labels: %v, values: %v",
hv.labels,
ls,
),
)
}
k := hv.marshaler.marshal(ls)
hv.mu.RLock()
h, ok := hv.hs[k]
hv.mu.RUnlock()
if ok {
return h
}
hv.mu.Lock()
h = &histogram{
cutoffs: hv.cutoffs,
counts: make([]atomicInt64, len(hv.cutoffs)),
}
hv.hs[k] = h
hv.mu.Unlock()
return h
}
// Bucket represents a single histogram bucket with its count and upper bound.
type Bucket struct {
Count int64
UpperBound float64
}
// Histogram tracks the distribution of values across predefined buckets.
// Histograms are useful for measuring request durations, response sizes, etc.
type Histogram interface {
// Record adds a single observation to the histogram.
Record(float64)
// Count returns the total number of observations.
Count() int64
// Sum returns the sum of all observed values.
Sum() float64
// Buckets returns the current state of all buckets.
Buckets() []Bucket
}
type histogram struct {
cutoffs []float64
sum atomicFloat64
counts []atomicInt64
}
func (h *histogram) Record(v float64) {
for i, c := range h.cutoffs {
if v <= c {
h.counts[i].Inc()
h.sum.Add(v)
break
}
}
}
func (h *histogram) Sum() float64 { return h.sum.Get() }
func (h *histogram) Count() int64 {
var res int64
for _, c := range h.counts {
res += c.Get()
}
return res
}
func (h *histogram) Buckets() []Bucket {
var bs = make([]Bucket, len(h.cutoffs))
for i, cutoff := range h.cutoffs {
bs[i].UpperBound = cutoff
bs[i].Count = h.counts[i].Get()
}
return bs
}
// StaticBuckets creates a HistogramOption that configures custom bucket boundaries.
// An infinity bucket is automatically appended to catch all values above the highest cutoff.
func StaticBuckets(cutoffs []float64) HistogramOption {
return func(hv *histogramVector) {
hv.cutoffs = append(cutoffs, math.Inf(0))
}
}
type partialHistogramVector struct {
hv HistogramVector
vs []string
}
func (phv partialHistogramVector) WithLabels(labels ...string) Histogram {
return phv.hv.WithLabels(append(phv.vs, labels...)...)
}
type reorderHistogramVector struct {
hv HistogramVector
labelOrderer
}
func (rhv reorderHistogramVector) WithLabels(ls ...string) Histogram {
return rhv.hv.WithLabels(rhv.order(ls)...)
}
var (
// NoopHistogram is a histogram that discards all operations.
NoopHistogram Histogram = noopHistogram{}
// NoopHistogramVector is a histogram vector that returns noop histograms.
NoopHistogramVector HistogramVector = noopHistogramVector{}
)
type noopHistogram struct{}
func (noopHistogram) Record(float64) {}
func (noopHistogram) Count() int64 { return 0 }
func (noopHistogram) Sum() float64 { return 0 }
func (noopHistogram) Buckets() []Bucket { return nil }
type noopHistogramVector struct{}
func (noopHistogramVector) WithLabels(...string) Histogram {
return noopHistogram{}
}