forked from stolostron/thanos
-
Notifications
You must be signed in to change notification settings - Fork 0
/
analyzer_test.go
260 lines (247 loc) · 9.45 KB
/
analyzer_test.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
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
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
// Copyright (c) The Thanos Authors.
// Licensed under the Apache License 2.0.
package querysharding
import (
"sort"
"testing"
"github.com/prometheus/common/model"
"github.com/stretchr/testify/require"
)
func TestAnalyzeQuery(t *testing.T) {
type testCase struct {
name string
expression string
shardingLabels []string
}
nonShardable := []testCase{
{
name: "aggregation",
expression: "sum(http_requests_total)",
},
{
name: "outer aggregation with no grouping",
expression: "count(sum by (pod) (http_requests_total))",
},
{
name: "outer aggregation with without grouping",
expression: "count(sum without (pod) (http_requests_total))",
},
{
name: "binary expression",
expression: `http_requests_total{code="400"} / http_requests_total`,
},
{
name: "binary expression with constant",
expression: `http_requests_total{code="400"} / 4`,
},
{
name: "binary expression with empty vector matching",
expression: `http_requests_total{code="400"} / on () http_requests_total`,
},
{
name: "binary aggregation with different grouping labels",
expression: `sum by (pod) (http_requests_total{code="400"}) / sum by (cluster) (http_requests_total)`,
},
{
name: "multiple binary expressions",
expression: `(http_requests_total{code="400"} + http_requests_total{code="500"}) / http_requests_total`,
},
{
name: "multiple binary expressions with empty vector matchers",
expression: `
(http_requests_total{code="400"} + on (cluster, pod) http_requests_total{code="500"})
/ on ()
http_requests_total`,
},
{
name: "aggregate by expression with label_replace, sharding label is dynamic",
expression: `sum by (dst_label) (label_replace(metric, "dst_label", "$1", "src_label", "re"))`,
},
{
name: "aggregate by expression with label_join, sharding label is dynamic",
expression: `sum by (dst_label) (label_join(metric, "dst_label", ",", "src_label"))`,
},
}
shardableByLabels := []testCase{
{
name: "aggregation with grouping",
expression: "sum by (pod) (http_requests_total)",
shardingLabels: []string{"pod"},
},
{
name: "multiple aggregations with grouping",
expression: "max by (pod) (sum by (pod, cluster) (http_requests_total))",
shardingLabels: []string{"pod"},
},
{
name: "binary expression with vector matching",
expression: `http_requests_total{code="400"} / on (pod) http_requests_total`,
shardingLabels: []string{"pod"},
},
{
name: "binary aggregation with same grouping labels",
expression: `sum by (pod) (http_requests_total{code="400"}) / sum by (pod) (http_requests_total)`,
shardingLabels: []string{"pod"},
},
{
name: "binary expression with vector matching and grouping",
expression: `sum by (cluster, pod) (http_requests_total{code="400"}) / on (pod) sum by (cluster, pod) (http_requests_total)`,
shardingLabels: []string{"pod"},
},
{
name: "multiple binary expressions with vector matchers",
expression: `
(http_requests_total{code="400"} + on (cluster, pod) http_requests_total{code="500"})
/ on (pod)
http_requests_total`,
shardingLabels: []string{"pod"},
},
{
name: "multiple binary expressions with grouping",
expression: `
sum by (container) (
(http_requests_total{code="400"} + on (cluster, pod, container) http_requests_total{code="500"})
/ on (pod, container)
http_requests_total
)`,
shardingLabels: []string{"container"},
},
{
name: "multiple binary expressions with grouping",
expression: `(http_requests_total{code="400"} + on (pod) http_requests_total{code="500"}) / on (cluster, pod) http_requests_total`,
shardingLabels: []string{"pod"},
},
{
name: "histogram quantile",
expression: "histogram_quantile(0.95, sum(rate(metric[1m])) by (le, cluster))",
shardingLabels: []string{"cluster"},
},
{
name: "subquery",
expression: "sum(http_requests_total) by (pod, cluster) [1h:1m]",
shardingLabels: []string{"cluster", "pod"},
},
{
name: "subquery with function",
expression: "increase(sum(http_requests_total) by (pod, cluster) [1h:1m])",
shardingLabels: []string{"cluster", "pod"},
},
{
name: "ignore vector matching with 2 aggregations",
expression: `sum(rate(node_cpu_seconds_total[3h])) by (cluster_id, mode) / ignoring(mode) group_left sum(rate(node_cpu_seconds_total[3h])) by (cluster_id)`,
shardingLabels: []string{"cluster_id"},
},
{
name: "aggregate by expression with label_replace, sharding label is not dynamic",
expression: `sum by (pod) (label_replace(metric, "dst_label", "$1", "src_label", "re"))`,
shardingLabels: []string{"pod"},
},
{
name: "aggregate by expression with label_join, sharding label is not dynamic",
expression: `sum by (pod) (label_join(metric, "dst_label", ",", "src_label"))`,
shardingLabels: []string{"pod"},
},
{
name: "label_join and aggregation on multiple labels. Can be sharded by the static one",
expression: `sum by (pod, dst_label) (label_join(metric, "dst_label", ",", "src_label"))`,
shardingLabels: []string{"pod"},
},
{
name: "binary expression with vector matching and label_replace",
expression: `http_requests_total{code="400"} / on (pod) label_replace(metric, "dst_label", "$1", "src_label", "re")`,
shardingLabels: []string{"pod"},
},
{
name: "nested label joins",
expression: `label_join(sum by (pod) (label_join(metric, "dst_label", ",", "src_label")), "dst_label1", ",", "dst_label")`,
shardingLabels: []string{"pod"},
},
{
name: "complex query with label_replace, binary expr and aggregations on dynamic label",
expression: `sum(sum_over_time(container_memory_working_set_bytes{container_name!="POD",container_name!="",namespace="kube-system"}[1d:5m])) by (instance, cluster) / avg(label_replace(sum(sum_over_time(kube_node_status_capacity_memory_bytes[1d:5m])) by (node, cluster), "instance", "$1", "node", "(.*)")) by (instance, cluster)`,
shardingLabels: []string{"cluster"},
},
}
shardableWithoutLabels := []testCase{
{
name: "aggregation without grouping",
expression: "sum without (pod) (http_requests_total)",
shardingLabels: []string{"pod"},
},
{
name: "multiple aggregations with without grouping",
expression: "max without (pod) (sum without (pod, cluster) (http_requests_total))",
shardingLabels: []string{"pod", "cluster"},
},
{
name: "binary expression with without vector matching and grouping",
expression: `sum without (cluster, pod) (http_requests_total{code="400"}) / ignoring (pod) sum without (cluster, pod) (http_requests_total)`,
shardingLabels: []string{"pod", "cluster", model.MetricNameLabel},
},
{
name: "multiple binary expressions with without grouping",
expression: `(http_requests_total{code="400"} + ignoring (pod) http_requests_total{code="500"}) / ignoring (cluster, pod) http_requests_total`,
shardingLabels: []string{"cluster", "pod", model.MetricNameLabel},
},
{
name: "multiple binary expressions with without vector matchers",
expression: `
(http_requests_total{code="400"} + ignoring (cluster, pod) http_requests_total{code="500"})
/ ignoring (pod)
http_requests_total`,
shardingLabels: []string{"cluster", "pod", model.MetricNameLabel},
},
{
name: "histogram quantile",
expression: "histogram_quantile(0.95, sum(rate(metric[1m])) without (le, cluster))",
shardingLabels: []string{"cluster"},
},
{
name: "aggregate without expression with label_replace, sharding label is not dynamic",
expression: `sum without (dst_label) (label_replace(metric, "dst_label", "$1", "src_label", "re"))`,
shardingLabels: []string{"dst_label"},
},
{
name: "aggregate without expression with label_join, sharding label is not dynamic",
expression: `sum without (dst_label) (label_join(metric, "dst_label", ",", "src_label"))`,
shardingLabels: []string{"dst_label"},
},
{
name: "aggregate without expression with label_replace",
expression: `sum without (pod) (label_replace(metric, "dst_label", "$1", "src_label", "re"))`,
shardingLabels: []string{"pod", "dst_label"},
},
}
for _, test := range nonShardable {
t.Run(test.name, func(t *testing.T) {
analyzer := NewQueryAnalyzer()
analysis, err := analyzer.Analyze(test.expression)
require.NoError(t, err)
require.False(t, analysis.IsShardable())
})
}
for _, test := range shardableByLabels {
t.Run(test.name, func(t *testing.T) {
analyzer := NewQueryAnalyzer()
analysis, err := analyzer.Analyze(test.expression)
require.NoError(t, err)
require.True(t, analysis.IsShardable())
require.True(t, analysis.ShardBy())
sort.Strings(test.shardingLabels)
sort.Strings(analysis.ShardingLabels())
require.Equal(t, test.shardingLabels, analysis.ShardingLabels())
})
}
for _, test := range shardableWithoutLabels {
t.Run(test.name, func(t *testing.T) {
analyzer := NewQueryAnalyzer()
analysis, err := analyzer.Analyze(test.expression)
require.NoError(t, err)
require.True(t, analysis.IsShardable())
require.False(t, analysis.ShardBy())
sort.Strings(test.shardingLabels)
sort.Strings(analysis.ShardingLabels())
require.Equal(t, test.shardingLabels, analysis.ShardingLabels())
})
}
}