Skip to content

Commit

Permalink
Allowing to create Not/Regex/NotRegex matchers
Browse files Browse the repository at this point in the history
Signed-off-by: alanprot <[email protected]>
  • Loading branch information
alanprot committed Jan 27, 2025
1 parent 8b48fe2 commit 1c75f4f
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 10 deletions.
6 changes: 2 additions & 4 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,5 @@ jobs:
with:
go-version-file: .go-version
cache: true
- name: Lint
uses: golangci/[email protected]
with:
version: v1.59.0
- name: Golangci-lint
uses: golangci/[email protected]
39 changes: 36 additions & 3 deletions walk.go
Original file line number Diff line number Diff line change
Expand Up @@ -460,12 +460,45 @@ func (s *PromQLSmith) walkLabelMatchers() []*labels.Matcher {
lbls = append(lbls, l)
})

valF := func(v string) string {
val := s.rnd.Float64()
switch {
case val > 0.95:
return ""
case val > 0.90:
return ".*"
case val > 0.85:
return ".+"
case val > 0.75:
return fmt.Sprintf(".*%v", v[len(v)/2:])
default:
return fmt.Sprintf("%v.*", v[:len(v)/2])
}
}

for i := 0; i < items; i++ {

var matcher *labels.Matcher

if lbls[orders[i]].Name == labels.MetricName {
containsName = true
matcher = labels.MustNewMatcher(labels.MatchEqual, lbls[orders[i]].Name, lbls[orders[i]].Value)
} else {
res := s.rnd.Intn(4)
matchType := labels.MatchType(res)
switch matchType {
case labels.MatchEqual:
matcher = labels.MustNewMatcher(labels.MatchEqual, lbls[orders[i]].Name, lbls[orders[i]].Value)
case labels.MatchNotEqual:
matcher = labels.MustNewMatcher(labels.MatchNotEqual, lbls[orders[i]].Name, lbls[orders[i]].Value)
case labels.MatchRegexp:
matcher = labels.MustNewMatcher(labels.MatchRegexp, lbls[orders[i]].Name, valF(lbls[orders[i]].Value))
case labels.MatchNotRegexp:
matcher = labels.MustNewMatcher(labels.MatchNotRegexp, lbls[orders[i]].Name, valF(lbls[orders[i]].Value))
}
}
matchers = append(matchers, labels.MustNewMatcher(labels.MatchEqual, lbls[orders[i]].Name, lbls[orders[i]].Value))

matchers = append(matchers, matcher)
}

if !containsName {
Expand All @@ -482,8 +515,8 @@ func (s *PromQLSmith) walkLabelMatchers() []*labels.Matcher {
return matchers
}

// walkSelectors is similar to walkLabelMatchers, but used for generating various
// types of matchers more than simple equal matcher.
// walkSelectors is similar to walkLabelMatchers, but does not guarantee the equal
// matcher on the metric name
func (s *PromQLSmith) walkSelectors() []*labels.Matcher {
if len(s.seriesSet) == 0 {
return nil
Expand Down
14 changes: 11 additions & 3 deletions walk_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -497,7 +497,7 @@ func TestWalkVectorSelector(t *testing.T) {
require.True(t, ok)
containsMetricName := false
for _, matcher := range vs.LabelMatchers {
require.Equal(t, labels.MatchEqual, matcher.Type)
require.LessOrEqual(t, matcher.Type, 4)
if matcher.Name == labels.MetricName {
containsMetricName = true
}
Expand All @@ -509,7 +509,8 @@ func TestWalkLabelMatchers(t *testing.T) {
rnd := rand.New(rand.NewSource(time.Now().Unix()))
opts := []Option{WithEnableOffset(true), WithEnableAtModifier(true)}
for i, tc := range []struct {
ss []labels.Labels
expectedMatchers int
ss []labels.Labels
}{
{
ss: nil,
Expand All @@ -526,9 +527,16 @@ func TestWalkLabelMatchers(t *testing.T) {
} {
t.Run(fmt.Sprintf("test_case_%d", i), func(t *testing.T) {
p := New(rnd, tc.ss, opts...)
labelNames := make(map[string]struct{})
for _, s := range tc.ss {
s.Range(func(l labels.Label) {
labelNames[l.Name] = struct{}{}
})
}
matchers := p.walkLabelMatchers()
for _, matcher := range matchers {
require.Equal(t, labels.MatchEqual, matcher.Type)
require.LessOrEqual(t, matcher.Type, 4)
require.Contains(t, labelNames, matcher.Name)
}
})
}
Expand Down

0 comments on commit 1c75f4f

Please sign in to comment.