Skip to content

Commit eaf3e57

Browse files
authored
Allowing to create Not/Regex/NotRegex matchers (#142)
* Allowing to create Not/Regex/NotRegex matchers Signed-off-by: alanprot <[email protected]> * fix lint Signed-off-by: alanprot <[email protected]> * Adding != case Signed-off-by: alanprot <[email protected]> * using ceil when discovering how many extra matcher will be added on walkLabelMatchers function Signed-off-by: alanprot <[email protected]> --------- Signed-off-by: alanprot <[email protected]>
1 parent 8b48fe2 commit eaf3e57

File tree

3 files changed

+55
-18
lines changed

3 files changed

+55
-18
lines changed

.github/workflows/ci.yml

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,5 @@ jobs:
3131
with:
3232
go-version-file: .go-version
3333
cache: true
34-
- name: Lint
35-
uses: golangci/[email protected]
36-
with:
37-
version: v1.59.0
34+
- name: Golangci-lint
35+
uses: golangci/[email protected]

walk.go

Lines changed: 42 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package promqlsmith
22

33
import (
44
"fmt"
5+
"math"
56
"math/rand"
67
"sort"
78
"strings"
@@ -452,20 +453,57 @@ func (s *PromQLSmith) walkLabelMatchers() []*labels.Matcher {
452453
}
453454
series := s.seriesSet[s.rnd.Intn(len(s.seriesSet))]
454455
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)))
456457
matchers := make([]*labels.Matcher, 0, items)
457458
containsName := false
458459
lbls := make([]labels.Label, 0, series.Len())
459460
series.Range(func(l labels.Label) {
460461
lbls = append(lbls, l)
461462
})
462463

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+
463480
for i := 0; i < items; i++ {
464481

482+
var matcher *labels.Matcher
483+
465484
if lbls[orders[i]].Name == labels.MetricName {
466485
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+
}
467504
}
468-
matchers = append(matchers, labels.MustNewMatcher(labels.MatchEqual, lbls[orders[i]].Name, lbls[orders[i]].Value))
505+
506+
matchers = append(matchers, matcher)
469507
}
470508

471509
if !containsName {
@@ -482,8 +520,8 @@ func (s *PromQLSmith) walkLabelMatchers() []*labels.Matcher {
482520
return matchers
483521
}
484522

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
487525
func (s *PromQLSmith) walkSelectors() []*labels.Matcher {
488526
if len(s.seriesSet) == 0 {
489527
return nil
@@ -687,13 +725,6 @@ func keepValueTypes(input []parser.ValueType, keep []parser.ValueType) []parser.
687725
return out
688726
}
689727

690-
func min(a, b int) int {
691-
if a > b {
692-
return b
693-
}
694-
return a
695-
}
696-
697728
// generate a non-zero float64 value randomly.
698729
func getNonZeroFloat64(rnd *rand.Rand) float64 {
699730
for {

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)