diff --git a/lib/promrelabel/config.go b/lib/promrelabel/config.go index 26bfe2cde..4dbf903b8 100644 --- a/lib/promrelabel/config.go +++ b/lib/promrelabel/config.go @@ -435,7 +435,7 @@ func parseRelabelConfig(rc *RelabelConfig) (*parsedRelabelConfig, error) { } func isDefaultRegex(expr string) bool { - prefix, suffix := regexutil.Simplify(expr) + prefix, suffix := regexutil.SimplifyPromRegex(expr) if prefix != "" { return false } diff --git a/lib/regexutil/promregex.go b/lib/regexutil/promregex.go index 11313b6d5..60ffe3f2f 100644 --- a/lib/regexutil/promregex.go +++ b/lib/regexutil/promregex.go @@ -46,8 +46,8 @@ func NewPromRegex(expr string) (*PromRegex, error) { if _, err := regexp.Compile(expr); err != nil { return nil, err } - prefix, suffix := Simplify(expr) - orValues := GetOrValues(suffix) + prefix, suffix := SimplifyPromRegex(expr) + orValues := GetOrValuesPromRegex(suffix) substrDotStar := getSubstringLiteral(suffix, ".*") substrDotPlus := getSubstringLiteral(suffix, ".+") // It is expected that Optimize returns valid regexp in suffix, so use MustCompile here. @@ -130,7 +130,7 @@ func getSubstringLiteral(expr, prefixSuffix string) string { return "" } expr = expr[:len(expr)-len(prefixSuffix)] - prefix, suffix := Simplify(expr) + prefix, suffix := SimplifyPromRegex(expr) if suffix != "" { return "" } diff --git a/lib/regexutil/regexutil.go b/lib/regexutil/regexutil.go index 2a28be255..64480c2d7 100644 --- a/lib/regexutil/regexutil.go +++ b/lib/regexutil/regexutil.go @@ -18,16 +18,16 @@ func RemoveStartEndAnchors(expr string) string { return expr } -// GetOrValues returns "or" values from the given regexp expr. +// GetOrValuesPromRegex returns "or" values from the given Prometheus-like regexp expr. // // It ignores start and end anchors ('^') and ('$') at the start and the end of expr. // It returns ["foo", "bar"] for "foo|bar" regexp. // It returns ["foo"] for "foo" regexp. // It returns [""] for "" regexp. // It returns an empty list if it is impossible to extract "or" values from the regexp. -func GetOrValues(expr string) []string { +func GetOrValuesPromRegex(expr string) []string { expr = RemoveStartEndAnchors(expr) - prefix, tailExpr := Simplify(expr) + prefix, tailExpr := SimplifyPromRegex(expr) if tailExpr == "" { return []string{prefix} } @@ -132,7 +132,7 @@ func isLiteral(sre *syntax.Regexp) bool { const maxOrValues = 100 -// Simplify simplifies the given expr. +// SimplifyPromRegex simplifies the given Prometheus-like expr. // // It returns plaintext prefix and the remaining regular expression // with dropped '^' and '$' anchors at the beginning and the end @@ -140,7 +140,7 @@ const maxOrValues = 100 // // The function removes capturing parens from the expr, // so it cannot be used when capturing parens are necessary. -func Simplify(expr string) (string, string) { +func SimplifyPromRegex(expr string) (string, string) { sre, err := syntax.Parse(expr, syntax.Perl) if err != nil { // Cannot parse the regexp. Return it all as prefix. diff --git a/lib/regexutil/regexutil_test.go b/lib/regexutil/regexutil_test.go index 4695c16a5..51ca2a481 100644 --- a/lib/regexutil/regexutil_test.go +++ b/lib/regexutil/regexutil_test.go @@ -5,10 +5,10 @@ import ( "testing" ) -func TestGetOrValues(t *testing.T) { +func TestGetOrValuesPromRegex(t *testing.T) { f := func(s string, valuesExpected []string) { t.Helper() - values := GetOrValues(s) + values := GetOrValuesPromRegex(s) if !reflect.DeepEqual(values, valuesExpected) { t.Fatalf("unexpected values for s=%q; got %q; want %q", s, values, valuesExpected) } @@ -46,10 +46,10 @@ func TestGetOrValues(t *testing.T) { f("^a(^foo|bar$)z$", nil) } -func TestSimplify(t *testing.T) { +func TestSimplifyPromRegex(t *testing.T) { f := func(s, expectedPrefix, expectedSuffix string) { t.Helper() - prefix, suffix := Simplify(s) + prefix, suffix := SimplifyPromRegex(s) if prefix != expectedPrefix { t.Fatalf("unexpected prefix for s=%q; got %q; want %q", s, prefix, expectedPrefix) } diff --git a/lib/storage/tag_filters.go b/lib/storage/tag_filters.go index a860b4dea..db7298f28 100644 --- a/lib/storage/tag_filters.go +++ b/lib/storage/tag_filters.go @@ -548,7 +548,7 @@ func getRegexpFromCache(expr string) (*regexpCacheValue, error) { } sExpr := expr - orValues := regexutil.GetOrValues(sExpr) + orValues := regexutil.GetOrValuesPromRegex(sExpr) var reMatch func(b []byte) bool var reCost uint64 var literalSuffix string @@ -881,7 +881,7 @@ func simplifyRegexp(expr string) (string, string) { // Make a copy of expr before using it, // since it may be constructed via bytesutil.ToUnsafeString() expr = string(append([]byte{}, expr...)) - prefix, suffix := regexutil.Simplify(expr) + prefix, suffix := regexutil.SimplifyPromRegex(expr) // Put the prefix and the suffix to the cache. ps := &prefixSuffix{