From 721389b2b2e4f3d3789e0181be76288cb7a4f8f2 Mon Sep 17 00:00:00 2001 From: alanprot Date: Mon, 27 Jan 2025 16:28:46 -0800 Subject: [PATCH] Uupdate Ppromqlsmith Signed-off-by: alanprot --- go.mod | 2 +- go.sum | 4 +- .../cortexproject/promqlsmith/walk.go | 53 +++++++++++++++---- vendor/modules.txt | 2 +- 4 files changed, 46 insertions(+), 15 deletions(-) diff --git a/go.mod b/go.mod index be6de1356eb..a3fe4fdf8bb 100644 --- a/go.mod +++ b/go.mod @@ -9,7 +9,7 @@ require ( github.com/armon/go-metrics v0.4.1 github.com/aws/aws-sdk-go v1.55.5 github.com/bradfitz/gomemcache v0.0.0-20230905024940-24af94b03874 - github.com/cortexproject/promqlsmith v0.0.0-20241121054008-8b48fe2471ef + github.com/cortexproject/promqlsmith v0.0.0-20250128002239-eaf3e57157fc github.com/dustin/go-humanize v1.0.1 github.com/efficientgo/core v1.0.0-rc.3 github.com/facette/natsort v0.0.0-20181210072756-2cd4dd1e2dcb diff --git a/go.sum b/go.sum index 64dfdce3ba3..549b4e367c1 100644 --- a/go.sum +++ b/go.sum @@ -934,8 +934,8 @@ github.com/coreos/go-semver v0.3.0/go.mod h1:nnelYz7RCh+5ahJtPPxZlU+153eP4D4r3Ee github.com/coreos/go-systemd/v22 v22.3.2/go.mod h1:Y58oyj3AT4RCenI/lSvhwexgC+NSVTIJ3seZv2GcEnc= github.com/coreos/go-systemd/v22 v22.5.0 h1:RrqgGjYQKalulkV8NGVIfkXQf6YYmOyiJKk8iXXhfZs= github.com/coreos/go-systemd/v22 v22.5.0/go.mod h1:Y58oyj3AT4RCenI/lSvhwexgC+NSVTIJ3seZv2GcEnc= -github.com/cortexproject/promqlsmith v0.0.0-20241121054008-8b48fe2471ef h1:wR21ZiKkA+wN2KG43qrK33IkFduY9JUa6th6P2KEU0o= -github.com/cortexproject/promqlsmith v0.0.0-20241121054008-8b48fe2471ef/go.mod h1:xbYQa0KX6Eh6YWbTBfZ9kK3N4hRxX+ZPIfVIY2U/y00= +github.com/cortexproject/promqlsmith v0.0.0-20250128002239-eaf3e57157fc h1:paM+YXwcIaz108+bFrm5aCepwVSxBfq48gb3Zc1z/nQ= +github.com/cortexproject/promqlsmith v0.0.0-20250128002239-eaf3e57157fc/go.mod h1:xbYQa0KX6Eh6YWbTBfZ9kK3N4hRxX+ZPIfVIY2U/y00= github.com/cortexproject/weaveworks-common v0.0.0-20241129212437-96019edf21f1 h1:UoSixdl0sBUhfEOMpIGxFnJjp3/y/+nkw6Du7su05FE= github.com/cortexproject/weaveworks-common v0.0.0-20241129212437-96019edf21f1/go.mod h1:7cl8fS/nivXe2DmBUUmr/3UGTJG2jVU2NRaIayR2Zjs= github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E= diff --git a/vendor/github.com/cortexproject/promqlsmith/walk.go b/vendor/github.com/cortexproject/promqlsmith/walk.go index 24268412d53..06d55067b14 100644 --- a/vendor/github.com/cortexproject/promqlsmith/walk.go +++ b/vendor/github.com/cortexproject/promqlsmith/walk.go @@ -2,6 +2,7 @@ package promqlsmith import ( "fmt" + "math" "math/rand" "sort" "strings" @@ -452,7 +453,7 @@ func (s *PromQLSmith) walkLabelMatchers() []*labels.Matcher { } series := s.seriesSet[s.rnd.Intn(len(s.seriesSet))] orders := s.rnd.Perm(series.Len()) - items := s.rnd.Intn((series.Len() + 1) / 2) + items := s.rnd.Intn(int(math.Ceil(float64(series.Len()+1) / 2))) matchers := make([]*labels.Matcher, 0, items) containsName := false lbls := make([]labels.Label, 0, series.Len()) @@ -460,12 +461,49 @@ 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: + val := lbls[orders[i]].Value + if s.rnd.Float64() > 0.9 { + val = "" + } + matcher = labels.MustNewMatcher(labels.MatchNotEqual, lbls[orders[i]].Name, val) + 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 { @@ -482,8 +520,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 @@ -687,13 +725,6 @@ func keepValueTypes(input []parser.ValueType, keep []parser.ValueType) []parser. 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 { diff --git a/vendor/modules.txt b/vendor/modules.txt index e9f5b61da7d..aa96a865c9e 100644 --- a/vendor/modules.txt +++ b/vendor/modules.txt @@ -303,7 +303,7 @@ github.com/coreos/go-semver/semver ## explicit; go 1.12 github.com/coreos/go-systemd/v22/activation github.com/coreos/go-systemd/v22/journal -# github.com/cortexproject/promqlsmith v0.0.0-20241121054008-8b48fe2471ef +# github.com/cortexproject/promqlsmith v0.0.0-20250128002239-eaf3e57157fc ## explicit; go 1.22.0 github.com/cortexproject/promqlsmith # github.com/cristalhq/hedgedhttp v0.9.1