@@ -2,6 +2,7 @@ package promqlsmith
2
2
3
3
import (
4
4
"fmt"
5
+ "math"
5
6
"math/rand"
6
7
"sort"
7
8
"strings"
@@ -452,20 +453,57 @@ func (s *PromQLSmith) walkLabelMatchers() []*labels.Matcher {
452
453
}
453
454
series := s .seriesSet [s .rnd .Intn (len (s .seriesSet ))]
454
455
orders := s .rnd .Perm (series .Len ())
455
- items := s .rnd .Intn (( series .Len () + 1 ) / 2 )
456
+ items := s .rnd .Intn (int ( math . Ceil ( float64 ( series .Len ()+ 1 ) / 2 )) )
456
457
matchers := make ([]* labels.Matcher , 0 , items )
457
458
containsName := false
458
459
lbls := make ([]labels.Label , 0 , series .Len ())
459
460
series .Range (func (l labels.Label ) {
460
461
lbls = append (lbls , l )
461
462
})
462
463
464
+ valF := func (v string ) string {
465
+ val := s .rnd .Float64 ()
466
+ switch {
467
+ case val > 0.95 :
468
+ return ""
469
+ case val > 0.90 :
470
+ return ".*"
471
+ case val > 0.85 :
472
+ return ".+"
473
+ case val > 0.75 :
474
+ return fmt .Sprintf (".*%v" , v [len (v )/ 2 :])
475
+ default :
476
+ return fmt .Sprintf ("%v.*" , v [:len (v )/ 2 ])
477
+ }
478
+ }
479
+
463
480
for i := 0 ; i < items ; i ++ {
464
481
482
+ var matcher * labels.Matcher
483
+
465
484
if lbls [orders [i ]].Name == labels .MetricName {
466
485
containsName = true
486
+ matcher = labels .MustNewMatcher (labels .MatchEqual , lbls [orders [i ]].Name , lbls [orders [i ]].Value )
487
+ } else {
488
+ res := s .rnd .Intn (4 )
489
+ matchType := labels .MatchType (res )
490
+ switch matchType {
491
+ case labels .MatchEqual :
492
+ matcher = labels .MustNewMatcher (labels .MatchEqual , lbls [orders [i ]].Name , lbls [orders [i ]].Value )
493
+ case labels .MatchNotEqual :
494
+ val := lbls [orders [i ]].Value
495
+ if s .rnd .Float64 () > 0.9 {
496
+ val = ""
497
+ }
498
+ matcher = labels .MustNewMatcher (labels .MatchNotEqual , lbls [orders [i ]].Name , val )
499
+ case labels .MatchRegexp :
500
+ matcher = labels .MustNewMatcher (labels .MatchRegexp , lbls [orders [i ]].Name , valF (lbls [orders [i ]].Value ))
501
+ case labels .MatchNotRegexp :
502
+ matcher = labels .MustNewMatcher (labels .MatchNotRegexp , lbls [orders [i ]].Name , valF (lbls [orders [i ]].Value ))
503
+ }
467
504
}
468
- matchers = append (matchers , labels .MustNewMatcher (labels .MatchEqual , lbls [orders [i ]].Name , lbls [orders [i ]].Value ))
505
+
506
+ matchers = append (matchers , matcher )
469
507
}
470
508
471
509
if ! containsName {
@@ -482,8 +520,8 @@ func (s *PromQLSmith) walkLabelMatchers() []*labels.Matcher {
482
520
return matchers
483
521
}
484
522
485
- // walkSelectors is similar to walkLabelMatchers, but used for generating various
486
- // types of matchers more than simple equal matcher.
523
+ // walkSelectors is similar to walkLabelMatchers, but does not guarantee the equal
524
+ // matcher on the metric name
487
525
func (s * PromQLSmith ) walkSelectors () []* labels.Matcher {
488
526
if len (s .seriesSet ) == 0 {
489
527
return nil
@@ -687,13 +725,6 @@ func keepValueTypes(input []parser.ValueType, keep []parser.ValueType) []parser.
687
725
return out
688
726
}
689
727
690
- func min (a , b int ) int {
691
- if a > b {
692
- return b
693
- }
694
- return a
695
- }
696
-
697
728
// generate a non-zero float64 value randomly.
698
729
func getNonZeroFloat64 (rnd * rand.Rand ) float64 {
699
730
for {
0 commit comments