-
Notifications
You must be signed in to change notification settings - Fork 5
/
walk.go
766 lines (712 loc) · 21.3 KB
/
walk.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
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
package promqlsmith
import (
"fmt"
"math/rand"
"sort"
"strings"
"time"
"github.com/prometheus/prometheus/model/labels"
"github.com/prometheus/prometheus/promql/parser"
"github.com/prometheus/prometheus/storage"
"golang.org/x/exp/slices"
)
const (
// max number of grouping labels in either by or without clause.
maxGroupingLabels = 5
// Destination label used in functions like label_replace and label_join.
destinationLabel = "__promqlsmith_dst_label__"
)
// walkExpr generates the given expression type with one of the required value type.
// valueTypes is only used for expressions that could have multiple possible return value types.
func (s *PromQLSmith) walkExpr(e ExprType, valueTypes ...parser.ValueType) (parser.Expr, error) {
switch e {
case AggregateExpr:
return s.walkAggregateExpr(), nil
case BinaryExpr:
// Wrap binary expression with paren for readability.
return wrapParenExpr(s.walkBinaryExpr(valueTypes...)), nil
case SubQueryExpr:
return s.walkSubQueryExpr(), nil
case MatrixSelector:
return s.walkMatrixSelector(), nil
case VectorSelector:
return s.walkVectorSelector(), nil
case CallExpr:
return s.walkCall(valueTypes...), nil
case NumberLiteral:
return s.walkNumberLiteral(), nil
case UnaryExpr:
return s.walkUnaryExpr(valueTypes...), nil
default:
return nil, fmt.Errorf("unsupported ExprType %d", e)
}
}
func (s *PromQLSmith) walkAggregateExpr() parser.Expr {
expr := &parser.AggregateExpr{
Op: s.supportedAggrs[s.rnd.Intn(len(s.supportedAggrs))],
Without: s.rnd.Int()%2 == 0,
Expr: s.Walk(parser.ValueTypeVector),
Grouping: s.walkGrouping(),
}
if expr.Op.IsAggregatorWithParam() {
expr.Param = s.walkAggregateParam(expr.Op)
}
return expr
}
// walkGrouping randomly generates grouping labels by picking from series label names.
// TODO(yeya24): can we reduce the label sets by picking from labels of selected series?
func (s *PromQLSmith) walkGrouping() []string {
if len(s.labelNames) == 0 {
return nil
}
orders := s.rnd.Perm(len(s.labelNames))
items := s.rnd.Intn(min(len(s.labelNames), maxGroupingLabels))
grouping := make([]string, items)
for i := 0; i < items; i++ {
grouping[i] = s.labelNames[orders[i]]
}
return grouping
}
func (s *PromQLSmith) walkAggregateParam(op parser.ItemType) parser.Expr {
switch op {
case parser.TOPK, parser.BOTTOMK:
return s.Walk(parser.ValueTypeScalar)
case parser.QUANTILE:
return s.Walk(parser.ValueTypeScalar)
case parser.COUNT_VALUES:
return &parser.StringLiteral{Val: "value"}
}
return nil
}
// Can only do binary expression between vector and scalar. So any expression
// that returns matrix doesn't work like matrix selector, subquery
// or function that returns matrix.
func (s *PromQLSmith) walkBinaryExpr(valueTypes ...parser.ValueType) parser.Expr {
valueTypes = keepValueTypes(valueTypes, vectorAndScalarValueTypes)
expr := &parser.BinaryExpr{
Op: s.walkBinaryOp(!slices.Contains(valueTypes, parser.ValueTypeVector)),
VectorMatching: &parser.VectorMatching{
Card: parser.CardOneToOne,
},
}
// If it is a set operator then only vectors are allowed.
if expr.Op.IsSetOperator() {
valueTypes = []parser.ValueType{parser.ValueTypeVector}
expr.VectorMatching.Card = parser.CardManyToMany
}
expr.LHS = wrapParenExpr(s.Walk(valueTypes...))
expr.RHS = wrapParenExpr(s.Walk(valueTypes...))
lvt := expr.LHS.Type()
rvt := expr.RHS.Type()
// ReturnBool can only be set for comparison operator. It is
// required to set to true if both expressions are scalar type.
if expr.Op.IsComparisonOperator() {
if lvt == parser.ValueTypeScalar && rvt == parser.ValueTypeScalar || s.rnd.Intn(2) == 0 {
expr.ReturnBool = true
}
}
if !expr.Op.IsSetOperator() && s.enableVectorMatching && lvt == parser.ValueTypeVector &&
rvt == parser.ValueTypeVector && s.rnd.Intn(2) == 0 {
leftSeriesSet, stop := getOutputSeries(expr.LHS)
if stop {
return expr
}
rightSeriesSet, stop := getOutputSeries(expr.RHS)
if stop {
return expr
}
s.walkVectorMatching(expr, leftSeriesSet, rightSeriesSet, s.rnd.Intn(4) == 0)
}
return expr
}
func (s *PromQLSmith) walkVectorMatching(expr *parser.BinaryExpr, seriesSetA []labels.Labels, seriesSetB []labels.Labels, includeLabels bool) {
sa := make(map[string]struct{})
for _, series := range seriesSetA {
series.Range(func(lbl labels.Label) {
if lbl.Name == labels.MetricName {
return
}
sa[lbl.Name] = struct{}{}
})
}
sb := make(map[string]struct{})
for _, series := range seriesSetB {
series.Range(func(lbl labels.Label) {
if lbl.Name == labels.MetricName {
return
}
sb[lbl.Name] = struct{}{}
})
}
expr.VectorMatching.On = true
matchedLabels := make([]string, 0)
for key := range sb {
if _, ok := sa[key]; ok {
matchedLabels = append(matchedLabels, key)
}
}
// We are doing a very naive approach of guessing side cardinalities
// by checking number of series each side.
oneSideLabelsSet := sa
if len(seriesSetA) > len(seriesSetB) {
expr.VectorMatching.MatchingLabels = matchedLabels
expr.VectorMatching.Card = parser.CardManyToOne
oneSideLabelsSet = sb
} else if len(seriesSetA) < len(seriesSetB) {
expr.VectorMatching.MatchingLabels = matchedLabels
expr.VectorMatching.Card = parser.CardOneToMany
}
// Otherwise we do 1:1 match.
// For simplicity, we always include all labels on the one side.
if expr.VectorMatching.Card != parser.CardOneToOne && includeLabels {
includeLabels := getIncludeLabels(oneSideLabelsSet, matchedLabels)
expr.VectorMatching.Include = includeLabels
}
}
func getIncludeLabels(labelNameSet map[string]struct{}, matchedLabels []string) []string {
output := make([]string, 0)
OUTER:
for lbl := range labelNameSet {
for _, matchedLabel := range matchedLabels {
if lbl == matchedLabel {
continue OUTER
}
}
output = append(output, lbl)
}
sort.Strings(output)
return output
}
// Walk binary op based on whether vector value type is allowed or not.
// Since Set operator only works with vector so if vector is disallowed
// we will choose comparison operator that works both for scalar and vector.
func (s *PromQLSmith) walkBinaryOp(disallowVector bool) parser.ItemType {
binops := s.supportedBinops
if disallowVector {
binops = make([]parser.ItemType, 0)
for _, binop := range s.supportedBinops {
// Set operator can only be used with vector operator.
if binop.IsSetOperator() {
continue
}
binops = append(binops, binop)
}
}
return binops[s.rnd.Intn(len(binops))]
}
func (s *PromQLSmith) walkSubQueryExpr() parser.Expr {
expr := &parser.SubqueryExpr{
Range: time.Hour,
Step: time.Minute,
Expr: s.walkVectorSelector(),
}
if s.enableOffset && s.rnd.Int()%2 == 0 {
negativeOffset := s.rnd.Intn(2) == 0
expr.OriginalOffset = time.Duration(s.rnd.Intn(300)) * time.Second
if negativeOffset {
expr.OriginalOffset = -expr.OriginalOffset
}
}
if s.enableAtModifier && s.rnd.Float64() > 0.7 {
expr.Timestamp, expr.StartOrEnd = s.walkAtModifier()
}
return expr
}
func (s *PromQLSmith) walkCall(valueTypes ...parser.ValueType) parser.Expr {
expr := &parser.Call{}
funcs := s.supportedFuncs
if len(valueTypes) > 0 {
funcs = make([]*parser.Function, 0)
valueTypeSet := make(map[parser.ValueType]struct{})
for _, vt := range valueTypes {
valueTypeSet[vt] = struct{}{}
}
for _, f := range s.supportedFuncs {
if _, ok := valueTypeSet[f.ReturnType]; ok {
funcs = append(funcs, f)
}
}
}
sort.Slice(funcs, func(i, j int) bool { return strings.Compare(funcs[i].Name, funcs[j].Name) < 0 })
expr.Func = funcs[s.rnd.Intn(len(funcs))]
s.walkFunctions(expr)
return expr
}
func (s *PromQLSmith) walkFunctions(expr *parser.Call) {
switch expr.Func.Name {
case "label_join":
s.walkLabelJoin(expr)
return
default:
}
expr.Args = make([]parser.Expr, len(expr.Func.ArgTypes))
if expr.Func.Name == "holt_winters" {
s.walkHoltWinters(expr)
return
} else if expr.Func.Name == "label_replace" {
s.walkLabelReplace(expr)
return
}
if expr.Func.Variadic != 0 {
s.walkVariadicFunctions(expr)
return
}
for i, arg := range expr.Func.ArgTypes {
expr.Args[i] = s.Walk(arg)
}
}
func (s *PromQLSmith) walkHoltWinters(expr *parser.Call) {
expr.Args[0] = s.Walk(expr.Func.ArgTypes[0])
expr.Args[1] = &parser.NumberLiteral{Val: getNonZeroFloat64(s.rnd)}
expr.Args[2] = &parser.NumberLiteral{Val: getNonZeroFloat64(s.rnd)}
}
func (s *PromQLSmith) walkLabelReplace(expr *parser.Call) {
expr.Args[0] = s.Walk(expr.Func.ArgTypes[0])
expr.Args[1] = &parser.StringLiteral{Val: destinationLabel}
expr.Args[2] = &parser.StringLiteral{Val: "$1"}
seriesSet, _ := getOutputSeries(expr.Args[0])
var srcLabel string
if len(seriesSet) > 0 {
lbls := seriesSet[0]
if lbls.Len() > 0 {
idx := s.rnd.Intn(lbls.Len())
cnt := 0
lbls.Range(func(lbl labels.Label) {
if cnt == idx {
srcLabel = lbl.Name
}
cnt++
})
}
}
if srcLabel != "" {
// It is possible that the vector selector match nothing. In this case, it doesn't matter which label
// we pick. Just pick something from all series labels.
idx := s.rnd.Intn(len(s.labelNames))
srcLabel = s.labelNames[idx]
}
expr.Args[3] = &parser.StringLiteral{Val: srcLabel}
// Just copy the label we picked.
expr.Args[4] = &parser.StringLiteral{Val: "(.*)"}
}
func (s *PromQLSmith) walkLabelJoin(expr *parser.Call) {
expr.Args = make([]parser.Expr, 0, len(expr.Func.ArgTypes))
expr.Args = append(expr.Args, s.Walk(expr.Func.ArgTypes[0]))
seriesSet, _ := getOutputSeries(expr.Args[0])
expr.Args = append(expr.Args, &parser.StringLiteral{Val: destinationLabel})
expr.Args = append(expr.Args, &parser.StringLiteral{Val: ","})
// Let's try to not join more than 2 labels for simplicity.
cnt := 0
if len(seriesSet) > 0 {
seriesSet[0].Range(func(lbl labels.Label) {
if cnt < 2 {
if s.rnd.Int()%2 == 0 {
expr.Args = append(expr.Args, &parser.StringLiteral{Val: lbl.Name})
cnt++
}
}
})
return
}
// It is possible that the vector selector match nothing. In this case, it doesn't matter which label
// we pick. Just pick something from all series labels.
for _, name := range s.labelNames {
if cnt < 2 {
if s.rnd.Int()%2 == 0 {
expr.Args = append(expr.Args, &parser.StringLiteral{Val: name})
cnt++
}
}
}
}
// Supported variadic functions include:
// days_in_month, day_of_month, day_of_week, day_of_year, year,
// hour, minute, month, round.
// Unsupported variadic functions include:
// label_join, sort_by_label_desc, sort_by_label
func (s *PromQLSmith) walkVariadicFunctions(expr *parser.Call) {
switch expr.Func.Name {
case "round":
expr.Args[0] = s.Walk(expr.Func.ArgTypes[0])
expr.Args[1] = &parser.NumberLiteral{Val: float64(s.rnd.Intn(10))}
default:
// Rest of supported functions have either 0 or 1 function argument.
// If not specified it uses current timestamp instead of the vector timestamp.
// To reduce test flakiness we always use vector timestamp.
expr.Args[0] = s.Walk(expr.Func.ArgTypes[0])
}
}
func (s *PromQLSmith) walkVectorSelector() parser.Expr {
expr := &parser.VectorSelector{}
expr.LabelMatchers = s.walkLabelMatchers()
s.populateSeries(expr)
if s.enableOffset && s.rnd.Int()%2 == 0 {
negativeOffset := s.rnd.Intn(2) == 0
expr.OriginalOffset = time.Duration(s.rnd.Intn(300)) * time.Second
if negativeOffset {
expr.OriginalOffset = -expr.OriginalOffset
}
}
if s.enableAtModifier && s.rnd.Float64() > 0.7 {
expr.Timestamp, expr.StartOrEnd = s.walkAtModifier()
}
return expr
}
func (s *PromQLSmith) populateSeries(expr *parser.VectorSelector) {
expr.Series = make([]storage.Series, 0)
OUTER:
for _, series := range s.seriesSet {
for _, matcher := range expr.LabelMatchers {
m := matcher
if !m.Matches(series.Get(m.Name)) {
continue OUTER
}
}
expr.Series = append(expr.Series, &storage.SeriesEntry{Lset: series})
}
}
func (s *PromQLSmith) walkLabelMatchers() []*labels.Matcher {
if len(s.seriesSet) == 0 {
return nil
}
series := s.seriesSet[s.rnd.Intn(len(s.seriesSet))]
orders := s.rnd.Perm(series.Len())
items := s.rnd.Intn((series.Len() + 1) / 2)
matchers := make([]*labels.Matcher, 0, items)
containsName := false
lbls := make([]labels.Label, 0, series.Len())
series.Range(func(l labels.Label) {
lbls = append(lbls, l)
})
for i := 0; i < items; i++ {
if lbls[orders[i]].Name == labels.MetricName {
containsName = true
}
matchers = append(matchers, labels.MustNewMatcher(labels.MatchEqual, lbls[orders[i]].Name, lbls[orders[i]].Value))
}
if !containsName {
// Metric name is always included in the matcher to avoid
// too high cardinality and potential grouping errors.
// Ignore if metric name label doesn't exist.
metricName := series.Get(labels.MetricName)
if metricName != "" {
matchers = append(matchers, labels.MustNewMatcher(labels.MatchEqual, labels.MetricName, metricName))
}
}
matchers = append(matchers, s.enforceMatchers...)
return matchers
}
// walkSelectors is similar to walkLabelMatchers, but used for generating various
// types of matchers more than simple equal matcher.
func (s *PromQLSmith) walkSelectors() []*labels.Matcher {
if len(s.seriesSet) == 0 {
return nil
}
orders := s.rnd.Perm(len(s.labelNames))
items := randRange((len(s.labelNames)+1)/2, len(s.labelNames))
matchers := make([]*labels.Matcher, 0, items)
var (
value string
repeat bool
)
for i := 0; i < items; {
res := s.rnd.Intn(4)
name := s.labelNames[orders[i]]
matchType := labels.MatchType(res)
switch matchType {
case labels.MatchEqual:
val := s.rnd.Float64()
if val > 0.95 {
value = ""
} else if val > 0.9 {
value = "not_exist_value"
} else if val > 0.8 {
// TODO: randomize the non existent value using random UTF8 runes.
value = "."
} else {
idx := s.rnd.Intn(len(s.labelValues[name]))
value = s.labelValues[name][idx]
}
case labels.MatchNotEqual:
switch s.rnd.Intn(4) {
case 0:
value = ""
case 1:
value = "not_exist_value"
case 2:
// TODO: randomize the non existent value using random UTF8 runes.
value = "."
default:
idx := s.rnd.Intn(len(s.labelValues[name]))
value = s.labelValues[name][idx]
}
case labels.MatchRegexp:
val := s.rnd.Float64()
if val > 0.95 {
value = ""
} else if val > 0.9 {
value = "not_exist_value"
} else if val > 0.85 {
// TODO: randomize the non existent value using random UTF8 runes.
value = "."
} else if val > 0.8 {
value = ".*"
} else if val > 0.7 {
value = ".+"
} else if val > 0.5 {
// Prefix
idx := s.rnd.Intn(len(s.labelValues[name]))
value = s.labelValues[name][idx][:len(s.labelValues[name][idx])/2] + ".*"
} else {
valueOrders := s.rnd.Perm(len(s.labelValues[name]))
valueItems := s.rnd.Intn(len(s.labelValues[name]))
var sb strings.Builder
for j := 0; j < valueItems; j++ {
sb.WriteString(s.labelValues[name][valueOrders[j]])
if j < valueItems-1 {
sb.WriteString("|")
}
}
// Randomly attach a non-existent value.
if s.rnd.Intn(2) == 1 {
sb.WriteString("|not_exist_value")
}
}
case labels.MatchNotRegexp:
val := s.rnd.Float64()
if val > 0.8 {
value = ""
} else if val > 0.7 {
value = "not_exist_value"
} else if val > 0.6 {
// TODO: randomize the non existent value using random UTF8 runes.
value = "."
} else if val > 0.4 {
// Prefix
idx := s.rnd.Intn(len(s.labelValues[name]))
value = s.labelValues[name][idx][:len(s.labelValues[name][idx])/2] + ".*"
} else {
valueOrders := s.rnd.Perm(len(s.labelValues[name]))
valueItems := s.rnd.Intn(len(s.labelValues[name]))
var sb strings.Builder
for j := 0; j < valueItems; j++ {
sb.WriteString(s.labelValues[name][valueOrders[j]])
if j < valueItems-1 {
sb.WriteString("|")
}
}
// Randomly attach a non-existent value.
if s.rnd.Intn(2) == 1 {
sb.WriteString("|not_exist_value")
}
}
default:
panic("unsupported label matcher type")
}
matchers = append(matchers, labels.MustNewMatcher(matchType, name, value))
if !repeat && s.rnd.Intn(3) == 0 {
repeat = true
} else {
i++
}
}
matchers = append(matchers, s.enforceMatchers...)
return matchers
}
func (s *PromQLSmith) walkAtModifier() (ts *int64, op parser.ItemType) {
res := s.rnd.Intn(3)
switch res {
case 0:
op = parser.START
case 1:
op = parser.END
case 2:
t := s.rnd.Int63n(s.atModifierMaxTimestamp)
ts = &t
}
return
}
func (s *PromQLSmith) walkMatrixSelector() parser.Expr {
return &parser.MatrixSelector{
// Make sure the time range is > 0s.
Range: time.Duration(s.rnd.Intn(5)+1) * time.Minute,
VectorSelector: s.walkVectorSelector(),
}
}
// Only vector and scalar result is allowed.
func (s *PromQLSmith) walkUnaryExpr(valueTypes ...parser.ValueType) parser.Expr {
expr := &parser.UnaryExpr{
Op: parser.SUB,
}
valueTypes = keepValueTypes(valueTypes, vectorAndScalarValueTypes)
expr.Expr = s.Walk(valueTypes...)
return expr
}
func (s *PromQLSmith) walkNumberLiteral() parser.Expr {
return &parser.NumberLiteral{Val: s.rnd.Float64()}
}
func exprsFromValueTypes(valueTypes []parser.ValueType) []ExprType {
set := make(map[ExprType]struct{})
res := make([]ExprType, 0)
for _, vt := range valueTypes {
exprs, ok := valueTypeToExprsMap[vt]
if !ok {
continue
}
for _, expr := range exprs {
set[expr] = struct{}{}
}
}
for expr := range set {
res = append(res, expr)
}
sort.Slice(res, func(i, j int) bool { return res[i] < res[j] })
return res
}
// wrapParenExpr makes binary expr in a paren expr for better readability.
func wrapParenExpr(expr parser.Expr) parser.Expr {
if _, ok := expr.(*parser.BinaryExpr); ok {
return &parser.ParenExpr{Expr: expr}
}
return expr
}
// keepValueTypes picks value types that we should keep from the input.
// input shouldn't contain duplicate value types.
// If no input value types are provided, use value types to keep as result.
func keepValueTypes(input []parser.ValueType, keep []parser.ValueType) []parser.ValueType {
if len(input) == 0 {
return keep
}
out := make([]parser.ValueType, 0, len(keep))
s := make(map[parser.ValueType]struct{})
for _, vt := range keep {
s[vt] = struct{}{}
}
for _, vt := range input {
if _, ok := s[vt]; ok {
out = append(out, vt)
}
}
sort.Slice(out, func(i, j int) bool { return out[i] < out[j] })
return out
}
func min(a, b int) int {
if a > b {
return b
}
return a
}
// generate a non-zero float64 value randomly.
func getNonZeroFloat64(rnd *rand.Rand) float64 {
for {
res := rnd.Float64()
if res == 0 {
continue
}
return res
}
}
// Get output series for the expr using best-effort guess. This can be used in fuzzing
// vector matching. A bool value will also be returned alongside with the output series.
// This is used to determine whether the expression is suitable to do vector matching or not.
func getOutputSeries(expr parser.Expr) ([]labels.Labels, bool) {
stop := false
var lbls []labels.Labels
switch node := (expr).(type) {
case *parser.VectorSelector:
lbls := make([]labels.Labels, len(node.Series))
for i, s := range node.Series {
lbls[i] = s.Labels()
}
return lbls, len(lbls) == 0
case *parser.StepInvariantExpr:
return getOutputSeries(node.Expr)
case *parser.MatrixSelector:
return getOutputSeries(node.VectorSelector)
case *parser.ParenExpr:
return getOutputSeries(node.Expr)
case *parser.UnaryExpr:
return getOutputSeries(node.Expr)
case *parser.NumberLiteral:
return nil, false
case *parser.StringLiteral:
return nil, false
case *parser.AggregateExpr:
lbls, stop = getOutputSeries(node.Expr)
if stop {
return nil, true
}
m := make(map[uint64]labels.Labels)
b := make([]byte, 1024)
output := make([]labels.Labels, 0)
lb := labels.NewBuilder(labels.EmptyLabels())
if !node.Without {
for _, lbl := range lbls {
for _, groupLabel := range node.Grouping {
if val := lbl.Get(groupLabel); val != "" {
lb.Set(groupLabel, val)
}
}
newLbl := lb.Labels()
h, _ := newLbl.HashForLabels(b, node.Grouping...)
if _, ok := m[h]; !ok {
m[h] = newLbl
}
}
} else {
set := make(map[string]struct{})
for _, g := range node.Grouping {
set[g] = struct{}{}
}
for _, lbl := range lbls {
lbl.Range(func(l labels.Label) {
if l.Name == labels.MetricName {
return
}
if _, ok := set[l.Name]; !ok {
val := lbl.Get(l.Name)
if val == "" {
return
}
lb.Set(l.Name, val)
}
})
newLbl := lb.Labels()
h, _ := newLbl.HashWithoutLabels(b, node.Grouping...)
if _, ok := m[h]; !ok {
m[h] = newLbl
}
}
}
for _, v := range m {
output = append(output, v)
}
sort.Slice(output, func(i, j int) bool {
return labels.Compare(output[i], output[j]) < 0
})
return output, false
case *parser.SubqueryExpr:
return getOutputSeries(node.Expr)
case *parser.BinaryExpr:
// Stop introducing complexity if there is a binary expr already.
return nil, true
case *parser.Call:
// For function, we ignore `absent` and `absent_over_time`. And we continue
// traversal by checking the first matrix or vector argument.
if node.Func.Name == "absent" || node.Func.Name == "absent_over_time" {
return nil, true
}
for i, arg := range node.Func.ArgTypes {
// Find first matrix or vector type parameter, and we only
// check series from it.
if arg == parser.ValueTypeMatrix || arg == parser.ValueTypeVector {
return getOutputSeries(node.Args[i])
}
}
return nil, false
}
return lbls, stop
}
func randRange(min, max int) int {
return rand.Intn(max-min) + min
}