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.
This commit is contained in:
Aliaksandr Valialkin 2023-01-09 21:43:04 -08:00
parent 3d2cae9d75
commit b7a4650ab0
No known key found for this signature in database
GPG key ID: A72BEC6CD3D0DED1
3 changed files with 6 additions and 4 deletions

View file

@ -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] = &regexpCacheEntry{
re: re,
err: err,

View file

@ -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)
}

View file

@ -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) {