-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathsamplingcachefunctions.go
More file actions
209 lines (187 loc) · 4.8 KB
/
samplingcachefunctions.go
File metadata and controls
209 lines (187 loc) · 4.8 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
package promsketch
import (
"context"
"math"
"sort"
)
type SamplingFunctionCall func(ctx context.Context, values []float64, c float64) Vector
// FunctionCalls is a list of all functions supported by PromQL, including their types.
var SamplingFunctionCalls = map[string]SamplingFunctionCall{
"avg_over_time": funcSamplingAvgOverTime,
"count_over_time": funcSamplingCountOverTime,
"entropy_over_time": funcSamplingEntropyOverTime,
"max_over_time": funcSamplingMaxOverTime,
"min_over_time": funcSamplingMinOverTime,
"stddev_over_time": funcSamplingStddevOverTime,
"stdvar_over_time": funcSamplingStdvarOverTime,
"sum_over_time": funcSamplingSumOverTime,
"sum2_over_time": funcSamplingSum2OverTime,
"distinct_over_time": funcSamplingCardOverTime,
"l1_over_time": funcSamplingL1OverTime,
"l2_over_time": funcSamplingL2OverTime,
"quantile_over_time": funcSamplingQuantileOverTime,
}
func funcSamplingAvgOverTime(ctx context.Context, values []float64, c float64) Vector {
var sum float64 = 0
for _, v := range values {
sum += v
}
avg := float64(sum) / float64(len(values))
return Vector{Sample{
F: avg,
}}
}
func funcSamplingSumOverTime(ctx context.Context, values []float64, c float64) Vector {
var sum float64 = 0
for _, v := range values {
sum += v
}
return Vector{Sample{
F: sum / c, // c is sampling rate
}}
}
func funcSamplingSum2OverTime(ctx context.Context, values []float64, c float64) Vector {
var sum2 float64 = 0
for _, v := range values {
sum2 += v * v
}
return Vector{Sample{
F: sum2 / c,
}}
}
func funcSamplingCountOverTime(ctx context.Context, values []float64, c float64) Vector {
return Vector{Sample{
F: float64(len(values)) / c,
}}
}
func funcSamplingStddevOverTime(ctx context.Context, values []float64, c float64) Vector {
count := float64(len(values))
var sum2 float64 = 0
for _, v := range values {
sum2 += v * v
}
var sum float64 = 0
for _, v := range values {
sum += v
}
stddev := math.Sqrt(sum2/count - math.Pow(sum/count, 2))
return Vector{Sample{
F: float64(stddev),
}}
}
func funcSamplingStdvarOverTime(ctx context.Context, values []float64, c float64) Vector {
count := float64(len(values))
var sum2 float64 = 0
for _, v := range values {
sum2 += v * v
}
var sum float64 = 0
for _, v := range values {
sum += v
}
stdvar := sum2/count - math.Pow(sum/count, 2)
return Vector{Sample{
F: stdvar,
}}
}
func funcSamplingEntropyOverTime(ctx context.Context, values []float64, c float64) Vector {
m := make(map[float64]int)
for _, v := range values {
if _, ok := m[v]; !ok {
m[v] = 1
} else {
m[v] += 1
}
}
var entropy float64 = 0
for _, v := range m {
entropy += float64(v) * math.Log2(float64(v))
}
return Vector{Sample{
F: math.Log2(float64(len(m))) - entropy/float64(len(m)),
}}
}
func funcSamplingCardOverTime(ctx context.Context, values []float64, c float64) Vector {
m := make(map[float64]int)
for _, v := range values {
m[v] = 1
}
return Vector{Sample{
F: float64(len(m)),
}}
}
func funcSamplingL1OverTime(ctx context.Context, values []float64, c float64) Vector {
m := make(map[float64]int)
for _, v := range values {
if _, ok := m[v]; !ok {
m[v] = 1
} else {
m[v] += 1
}
}
var l1 float64 = 0
for _, v := range m {
l1 += float64(v)
}
return Vector{Sample{
F: l1,
}}
}
func funcSamplingL2OverTime(ctx context.Context, values []float64, c float64) Vector {
m := make(map[float64]int)
for _, v := range values {
if _, ok := m[v]; !ok {
m[v] = 1
} else {
m[v] += 1
}
}
var l2 float64 = 0
for _, v := range m {
l2 += float64(v * v)
}
return Vector{Sample{
F: math.Sqrt(l2),
}}
}
func samplingquantile(q float64, values []float64) float64 {
if len(values) == 0 || math.IsNaN(q) {
return math.NaN()
}
if q < 0 {
return math.Inf(-1)
}
if q > 1 {
return math.Inf(+1)
}
sort.Float64s(values)
n := float64(len(values))
// When the quantile lies between two samples,
// we use a weighted average of the two samples.
rank := q * (n - 1)
lowerIndex := math.Max(0, math.Floor(rank))
upperIndex := math.Min(n-1, lowerIndex+1)
weight := rank - math.Floor(rank)
return values[int(lowerIndex)]*(1-weight) + values[int(upperIndex)]*weight
}
func funcSamplingQuantileOverTime(ctx context.Context, values []float64, phi float64) Vector {
return Vector{Sample{F: samplingquantile(phi, values)}}
}
func funcSamplingMinOverTime(ctx context.Context, values []float64, c float64) Vector {
var min float64 = values[0]
for i := 1; i < len(values); i++ {
if values[i] < min {
min = values[i]
}
}
return Vector{Sample{F: min}}
}
func funcSamplingMaxOverTime(ctx context.Context, values []float64, c float64) Vector {
var max float64 = values[0]
for i := 1; i < len(values); i++ {
if values[i] > max {
max = values[i]
}
}
return Vector{Sample{F: max}}
}