From b7a4650ab0eb2440bf5c791aa0849c1a8fff8063 Mon Sep 17 00:00:00 2001 From: Aliaksandr Valialkin Date: Mon, 9 Jan 2023 21:43:04 -0800 Subject: [PATCH] all: use metricsql.CompileRegexp instead of regexp.Compile for compiling regexps used in graphite queries This should speed up repeated queries, since metricsql.CompileRegexp returns regexps from the cache on subsequent calls for the same input regexp. --- app/vmselect/graphite/metrics_api.go | 3 ++- app/vmselect/netstorage/netstorage.go | 4 ++-- lib/storage/storage.go | 3 ++- 3 files changed, 6 insertions(+), 4 deletions(-) diff --git a/app/vmselect/graphite/metrics_api.go b/app/vmselect/graphite/metrics_api.go index 498a95ce88..6034b872d4 100644 --- a/app/vmselect/graphite/metrics_api.go +++ b/app/vmselect/graphite/metrics_api.go @@ -17,6 +17,7 @@ import ( "github.com/VictoriaMetrics/VictoriaMetrics/lib/logger" "github.com/VictoriaMetrics/VictoriaMetrics/lib/storage" "github.com/VictoriaMetrics/metrics" + "github.com/VictoriaMetrics/metricsql" ) var maxTagValueSuffixes = flag.Int("search.maxTagValueSuffixesPerSearch", 100e3, "The maximum number of tag value suffixes returned from /metrics/find") @@ -361,7 +362,7 @@ func getRegexpForQuery(query string, delimiter byte) (*regexp.Regexp, error) { if len(tail) > 0 { return nil, fmt.Errorf("unexpected tail left after parsing query %q; tail: %q", query, tail) } - re, err := regexp.Compile(rs) + re, err := metricsql.CompileRegexp(rs) regexpCache[k] = ®expCacheEntry{ re: re, err: err, diff --git a/app/vmselect/netstorage/netstorage.go b/app/vmselect/netstorage/netstorage.go index dc517b9f07..6b05caca73 100644 --- a/app/vmselect/netstorage/netstorage.go +++ b/app/vmselect/netstorage/netstorage.go @@ -10,7 +10,6 @@ import ( "net" "net/http" "os" - "regexp" "sort" "strings" "sync" @@ -30,6 +29,7 @@ import ( "github.com/VictoriaMetrics/VictoriaMetrics/lib/querytracer" "github.com/VictoriaMetrics/VictoriaMetrics/lib/storage" "github.com/VictoriaMetrics/metrics" + "github.com/VictoriaMetrics/metricsql" "github.com/cespare/xxhash/v2" "github.com/valyala/fastrand" ) @@ -2674,7 +2674,7 @@ func applyGraphiteRegexpFilter(filter string, ss []string) ([]string, error) { // Anchor filter regexp to the beginning of the string as Graphite does. // See https://github.com/graphite-project/graphite-web/blob/3ad279df5cb90b211953e39161df416e54a84948/webapp/graphite/tags/localdatabase.py#L157 filter = "^(?:" + filter + ")" - re, err := regexp.Compile(filter) + re, err := metricsql.CompileRegexp(filter) if err != nil { return nil, fmt.Errorf("cannot parse regexp filter=%q: %w", filter, err) } diff --git a/lib/storage/storage.go b/lib/storage/storage.go index 3ef6678ad0..6dcb696a3a 100644 --- a/lib/storage/storage.go +++ b/lib/storage/storage.go @@ -30,6 +30,7 @@ import ( "github.com/VictoriaMetrics/VictoriaMetrics/lib/uint64set" "github.com/VictoriaMetrics/VictoriaMetrics/lib/workingsetcache" "github.com/VictoriaMetrics/fastcache" + "github.com/VictoriaMetrics/metricsql" ) const ( @@ -1430,7 +1431,7 @@ func getRegexpForGraphiteQuery(q string) (*regexp.Regexp, error) { return nil, fmt.Errorf("unexpected tail left after parsing %q: %q", q, tail) } reStr := "^" + strings.Join(parts, "") + "$" - return regexp.Compile(reStr) + return metricsql.CompileRegexp(reStr) } func getRegexpPartsForGraphiteQuery(q string) ([]string, string) {