Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Optimize regular initialization #12926

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 8 additions & 6 deletions pkg/querier/querier.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,15 @@ const (
// before checking if a new entry is available (to avoid spinning the CPU in a continuous
// check loop)
tailerWaitEntryThrottle = time.Second / 2

idPattern = `^(?:(?:[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12})|(?:(?:\{)?[0-9a-fA-F]{8}(?:-?[0-9a-fA-F]{4}){3}-?[0-9a-fA-F]{12}(?:\})?)|(\d+(?:\.\d+)?))$`
)

var nowFunc = func() time.Time { return time.Now() }
var (
nowFunc = func() time.Time { return time.Now() }

idRegexp = regexp.MustCompile(idPattern)
)

type interval struct {
start, end time.Time
Expand Down Expand Up @@ -1046,12 +1052,8 @@ func (q *SingleTenantQuerier) isLabelRelevant(label string, values []string, sta

// containsAllIDTypes filters out all UUID, GUID and numeric types. Returns false if even one value is not of the type
func containsAllIDTypes(values []string) bool {
pattern := `^(?:(?:[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12})|(?:(?:\{)?[0-9a-fA-F]{8}(?:-?[0-9a-fA-F]{4}){3}-?[0-9a-fA-F]{12}(?:\})?)|(\d+(?:\.\d+)?))$`

re := regexp.MustCompile(pattern)

for _, v := range values {
if !re.MatchString(v) {
if !idRegexp.MatchString(v) {
return false
}
}
Expand Down
29 changes: 29 additions & 0 deletions pkg/querier/querier_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1425,6 +1425,35 @@ func TestQuerier_isLabelRelevant(t *testing.T) {
}
}

func TestQuerier_containsAllIDTypes(t *testing.T) {
for _, tc := range []struct {
name string
values []string
expected bool
}{
{
name: "all uuidv4 values are valid",
values: []string{"751e8ee6-b377-4b2e-b7b5-5508fbe980ef", "6b7e2663-8ecb-42e1-8bdc-0c5de70185b3", "2e1e67ff-be4f-47b8-aee1-5d67ff1ddabf", "c95b2d62-74ed-4ed7-a8a1-eb72fc67946e"},
expected: true,
},
{
name: "one uuidv4 values are invalid",
values: []string{"w", "5076e837-cd8d-4dd7-95ff-fecb087dccf6", "2e2a6554-1744-4399-b89a-88ae79c27096", "d3c31248-ec0c-4bc4-b11c-8fb1cfb42e62"},
expected: false,
},
{
name: "all uuidv4 values are invalid",
values: []string{"w", "x", "y", "z"},
expected: false,
},
} {
t.Run(tc.name, func(t *testing.T) {
assert.Equal(t, tc.expected, containsAllIDTypes(tc.values))
})

}
}

func TestQuerier_DetectedLabels(t *testing.T) {
manyValues := []string{}
now := time.Now()
Expand Down
7 changes: 5 additions & 2 deletions pkg/ruler/base/notifier.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,11 @@ import (

// TODO: Instead of using the same metrics for all notifiers,
// should we have separate metrics for each discovery.NewManager?
var sdMetrics map[string]discovery.DiscovererMetrics
var (
sdMetrics map[string]discovery.DiscovererMetrics

srvDNSregexp = regexp.MustCompile(`^_.+._.+`)
)

func init() {
var err error
Expand Down Expand Up @@ -112,7 +116,6 @@ func buildNotifierConfig(amConfig *ruler_config.AlertManagerConfig, externalLabe
amURLs := strings.Split(amConfig.AlertmanagerURL, ",")
validURLs := make([]*url.URL, 0, len(amURLs))

srvDNSregexp := regexp.MustCompile(`^_.+._.+`)
for _, h := range amURLs {
url, err := url.Parse(h)
if err != nil {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package resultscache

import (
"context"

"github.com/grafana/loki/v3/pkg/util/httpreq"
)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@ package resultscache

import (
"context"
"github.com/grafana/loki/v3/pkg/util/httpreq"
"github.com/stretchr/testify/require"
"testing"

"github.com/stretchr/testify/require"

"github.com/grafana/loki/v3/pkg/util/httpreq"
)

func TestPipelineWrapperKeygen(t *testing.T) {
Expand Down
Loading