This commit is contained in:
Aliaksandr Valialkin 2024-05-23 14:11:16 +02:00
parent a686c7dd74
commit ceae8a7e08
No known key found for this signature in database
GPG key ID: 52C003EE2BCDB9EB
5 changed files with 15 additions and 15 deletions

View file

@ -435,7 +435,7 @@ func parseRelabelConfig(rc *RelabelConfig) (*parsedRelabelConfig, error) {
} }
func isDefaultRegex(expr string) bool { func isDefaultRegex(expr string) bool {
prefix, suffix := regexutil.Simplify(expr) prefix, suffix := regexutil.SimplifyPromRegex(expr)
if prefix != "" { if prefix != "" {
return false return false
} }

View file

@ -46,8 +46,8 @@ func NewPromRegex(expr string) (*PromRegex, error) {
if _, err := regexp.Compile(expr); err != nil { if _, err := regexp.Compile(expr); err != nil {
return nil, err return nil, err
} }
prefix, suffix := Simplify(expr) prefix, suffix := SimplifyPromRegex(expr)
orValues := GetOrValues(suffix) orValues := GetOrValuesPromRegex(suffix)
substrDotStar := getSubstringLiteral(suffix, ".*") substrDotStar := getSubstringLiteral(suffix, ".*")
substrDotPlus := getSubstringLiteral(suffix, ".+") substrDotPlus := getSubstringLiteral(suffix, ".+")
// It is expected that Optimize returns valid regexp in suffix, so use MustCompile here. // 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 "" return ""
} }
expr = expr[:len(expr)-len(prefixSuffix)] expr = expr[:len(expr)-len(prefixSuffix)]
prefix, suffix := Simplify(expr) prefix, suffix := SimplifyPromRegex(expr)
if suffix != "" { if suffix != "" {
return "" return ""
} }

View file

@ -18,16 +18,16 @@ func RemoveStartEndAnchors(expr string) string {
return expr 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 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", "bar"] for "foo|bar" regexp.
// It returns ["foo"] for "foo" regexp. // It returns ["foo"] for "foo" regexp.
// It returns [""] for "" regexp. // It returns [""] for "" regexp.
// It returns an empty list if it is impossible to extract "or" values from the 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) expr = RemoveStartEndAnchors(expr)
prefix, tailExpr := Simplify(expr) prefix, tailExpr := SimplifyPromRegex(expr)
if tailExpr == "" { if tailExpr == "" {
return []string{prefix} return []string{prefix}
} }
@ -132,7 +132,7 @@ func isLiteral(sre *syntax.Regexp) bool {
const maxOrValues = 100 const maxOrValues = 100
// Simplify simplifies the given expr. // SimplifyPromRegex simplifies the given Prometheus-like expr.
// //
// It returns plaintext prefix and the remaining regular expression // It returns plaintext prefix and the remaining regular expression
// with dropped '^' and '$' anchors at the beginning and the end // 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, // The function removes capturing parens from the expr,
// so it cannot be used when capturing parens are necessary. // 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) sre, err := syntax.Parse(expr, syntax.Perl)
if err != nil { if err != nil {
// Cannot parse the regexp. Return it all as prefix. // Cannot parse the regexp. Return it all as prefix.

View file

@ -5,10 +5,10 @@ import (
"testing" "testing"
) )
func TestGetOrValues(t *testing.T) { func TestGetOrValuesPromRegex(t *testing.T) {
f := func(s string, valuesExpected []string) { f := func(s string, valuesExpected []string) {
t.Helper() t.Helper()
values := GetOrValues(s) values := GetOrValuesPromRegex(s)
if !reflect.DeepEqual(values, valuesExpected) { if !reflect.DeepEqual(values, valuesExpected) {
t.Fatalf("unexpected values for s=%q; got %q; want %q", s, 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) f("^a(^foo|bar$)z$", nil)
} }
func TestSimplify(t *testing.T) { func TestSimplifyPromRegex(t *testing.T) {
f := func(s, expectedPrefix, expectedSuffix string) { f := func(s, expectedPrefix, expectedSuffix string) {
t.Helper() t.Helper()
prefix, suffix := Simplify(s) prefix, suffix := SimplifyPromRegex(s)
if prefix != expectedPrefix { if prefix != expectedPrefix {
t.Fatalf("unexpected prefix for s=%q; got %q; want %q", s, prefix, expectedPrefix) t.Fatalf("unexpected prefix for s=%q; got %q; want %q", s, prefix, expectedPrefix)
} }

View file

@ -548,7 +548,7 @@ func getRegexpFromCache(expr string) (*regexpCacheValue, error) {
} }
sExpr := expr sExpr := expr
orValues := regexutil.GetOrValues(sExpr) orValues := regexutil.GetOrValuesPromRegex(sExpr)
var reMatch func(b []byte) bool var reMatch func(b []byte) bool
var reCost uint64 var reCost uint64
var literalSuffix string var literalSuffix string
@ -881,7 +881,7 @@ func simplifyRegexp(expr string) (string, string) {
// Make a copy of expr before using it, // Make a copy of expr before using it,
// since it may be constructed via bytesutil.ToUnsafeString() // since it may be constructed via bytesutil.ToUnsafeString()
expr = string(append([]byte{}, expr...)) expr = string(append([]byte{}, expr...))
prefix, suffix := regexutil.Simplify(expr) prefix, suffix := regexutil.SimplifyPromRegex(expr)
// Put the prefix and the suffix to the cache. // Put the prefix and the suffix to the cache.
ps := &prefixSuffix{ ps := &prefixSuffix{