Skip to content

Commit a46d14f

Browse files
fix: Optimize regular initialization (#12926)
1 parent 4d761ac commit a46d14f

File tree

5 files changed

+47
-10
lines changed

5 files changed

+47
-10
lines changed

‎pkg/querier/querier.go

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -50,9 +50,15 @@ const (
5050
// before checking if a new entry is available (to avoid spinning the CPU in a continuous
5151
// check loop)
5252
tailerWaitEntryThrottle = time.Second / 2
53+
54+
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+)?))$`
5355
)
5456

55-
var nowFunc = func() time.Time { return time.Now() }
57+
var (
58+
nowFunc = func() time.Time { return time.Now() }
59+
60+
idRegexp = regexp.MustCompile(idPattern)
61+
)
5662

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

10471053
// containsAllIDTypes filters out all UUID, GUID and numeric types. Returns false if even one value is not of the type
10481054
func containsAllIDTypes(values []string) bool {
1049-
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+)?))$`
1050-
1051-
re := regexp.MustCompile(pattern)
1052-
10531055
for _, v := range values {
1054-
if !re.MatchString(v) {
1056+
if !idRegexp.MatchString(v) {
10551057
return false
10561058
}
10571059
}

‎pkg/querier/querier_test.go

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1425,6 +1425,35 @@ func TestQuerier_isLabelRelevant(t *testing.T) {
14251425
}
14261426
}
14271427

1428+
func TestQuerier_containsAllIDTypes(t *testing.T) {
1429+
for _, tc := range []struct {
1430+
name string
1431+
values []string
1432+
expected bool
1433+
}{
1434+
{
1435+
name: "all uuidv4 values are valid",
1436+
values: []string{"751e8ee6-b377-4b2e-b7b5-5508fbe980ef", "6b7e2663-8ecb-42e1-8bdc-0c5de70185b3", "2e1e67ff-be4f-47b8-aee1-5d67ff1ddabf", "c95b2d62-74ed-4ed7-a8a1-eb72fc67946e"},
1437+
expected: true,
1438+
},
1439+
{
1440+
name: "one uuidv4 values are invalid",
1441+
values: []string{"w", "5076e837-cd8d-4dd7-95ff-fecb087dccf6", "2e2a6554-1744-4399-b89a-88ae79c27096", "d3c31248-ec0c-4bc4-b11c-8fb1cfb42e62"},
1442+
expected: false,
1443+
},
1444+
{
1445+
name: "all uuidv4 values are invalid",
1446+
values: []string{"w", "x", "y", "z"},
1447+
expected: false,
1448+
},
1449+
} {
1450+
t.Run(tc.name, func(t *testing.T) {
1451+
assert.Equal(t, tc.expected, containsAllIDTypes(tc.values))
1452+
})
1453+
1454+
}
1455+
}
1456+
14281457
func TestQuerier_DetectedLabels(t *testing.T) {
14291458
manyValues := []string{}
14301459
now := time.Now()

‎pkg/ruler/base/notifier.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,11 @@ import (
2525

2626
// TODO: Instead of using the same metrics for all notifiers,
2727
// should we have separate metrics for each discovery.NewManager?
28-
var sdMetrics map[string]discovery.DiscovererMetrics
28+
var (
29+
sdMetrics map[string]discovery.DiscovererMetrics
30+
31+
srvDNSregexp = regexp.MustCompile(`^_.+._.+`)
32+
)
2933

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

115-
srvDNSregexp := regexp.MustCompile(`^_.+._.+`)
116119
for _, h := range amURLs {
117120
url, err := url.Parse(h)
118121
if err != nil {

‎pkg/storage/chunk/cache/resultscache/pipelinewrapper_keygen.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package resultscache
22

33
import (
44
"context"
5+
56
"github.com/grafana/loki/v3/pkg/util/httpreq"
67
)
78

‎pkg/storage/chunk/cache/resultscache/pipelinewrapper_keygen_test.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,11 @@ package resultscache
22

33
import (
44
"context"
5-
"github.com/grafana/loki/v3/pkg/util/httpreq"
6-
"github.com/stretchr/testify/require"
75
"testing"
6+
7+
"github.com/stretchr/testify/require"
8+
9+
"github.com/grafana/loki/v3/pkg/util/httpreq"
810
)
911

1012
func TestPipelineWrapperKeygen(t *testing.T) {

0 commit comments

Comments
 (0)