Skip to content

Commit eeba20a

Browse files
committed
Allowing to create Not/Regex/NotRegex matchers
1 parent 8b48fe2 commit eeba20a

File tree

2 files changed

+45
-4
lines changed

2 files changed

+45
-4
lines changed

walk.go

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -460,12 +460,45 @@ func (s *PromQLSmith) walkLabelMatchers() []*labels.Matcher {
460460
lbls = append(lbls, l)
461461
})
462462

463+
valF := func(v string) string {
464+
val := s.rnd.Float64()
465+
switch {
466+
case val > 0.95:
467+
return ""
468+
case val > 0.90:
469+
return ".*"
470+
case val > 0.85:
471+
return ".+"
472+
case val > 0.75:
473+
return fmt.Sprintf(".*%v", v[len(v)/2:])
474+
default:
475+
return fmt.Sprintf("%v.*", v[:len(v)/2])
476+
}
477+
}
478+
463479
for i := 0; i < items; i++ {
464480

481+
var matcher *labels.Matcher
482+
465483
if lbls[orders[i]].Name == labels.MetricName {
466484
containsName = true
485+
matcher = labels.MustNewMatcher(labels.MatchEqual, lbls[orders[i]].Name, lbls[orders[i]].Value)
486+
} else {
487+
res := s.rnd.Intn(4)
488+
matchType := labels.MatchType(res)
489+
switch matchType {
490+
case labels.MatchEqual:
491+
matcher = labels.MustNewMatcher(labels.MatchEqual, lbls[orders[i]].Name, lbls[orders[i]].Value)
492+
case labels.MatchNotEqual:
493+
matcher = labels.MustNewMatcher(labels.MatchNotEqual, lbls[orders[i]].Name, lbls[orders[i]].Value)
494+
case labels.MatchRegexp:
495+
matcher = labels.MustNewMatcher(labels.MatchRegexp, lbls[orders[i]].Name, valF(lbls[orders[i]].Value))
496+
case labels.MatchNotRegexp:
497+
matcher = labels.MustNewMatcher(labels.MatchNotRegexp, lbls[orders[i]].Name, valF(lbls[orders[i]].Value))
498+
}
467499
}
468-
matchers = append(matchers, labels.MustNewMatcher(labels.MatchEqual, lbls[orders[i]].Name, lbls[orders[i]].Value))
500+
501+
matchers = append(matchers, matcher)
469502
}
470503

471504
if !containsName {

walk_test.go

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -497,7 +497,7 @@ func TestWalkVectorSelector(t *testing.T) {
497497
require.True(t, ok)
498498
containsMetricName := false
499499
for _, matcher := range vs.LabelMatchers {
500-
require.Equal(t, labels.MatchEqual, matcher.Type)
500+
require.LessOrEqual(t, matcher.Type, 4)
501501
if matcher.Name == labels.MetricName {
502502
containsMetricName = true
503503
}
@@ -509,7 +509,8 @@ func TestWalkLabelMatchers(t *testing.T) {
509509
rnd := rand.New(rand.NewSource(time.Now().Unix()))
510510
opts := []Option{WithEnableOffset(true), WithEnableAtModifier(true)}
511511
for i, tc := range []struct {
512-
ss []labels.Labels
512+
expectedMatchers int
513+
ss []labels.Labels
513514
}{
514515
{
515516
ss: nil,
@@ -526,9 +527,16 @@ func TestWalkLabelMatchers(t *testing.T) {
526527
} {
527528
t.Run(fmt.Sprintf("test_case_%d", i), func(t *testing.T) {
528529
p := New(rnd, tc.ss, opts...)
530+
labelNames := make(map[string]struct{})
531+
for _, s := range tc.ss {
532+
s.Range(func(l labels.Label) {
533+
labelNames[l.Name] = struct{}{}
534+
})
535+
}
529536
matchers := p.walkLabelMatchers()
530537
for _, matcher := range matchers {
531-
require.Equal(t, labels.MatchEqual, matcher.Type)
538+
require.LessOrEqual(t, matcher.Type, 4)
539+
require.Contains(t, labelNames, matcher.Name)
532540
}
533541
})
534542
}

0 commit comments

Comments
 (0)