Skip to content
Draft
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
19 changes: 16 additions & 3 deletions pkg/labels/matcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"regexp"
"strconv"
"strings"
"sync"
"unicode"

"github.com/prometheus/common/model"
Expand Down Expand Up @@ -55,7 +56,8 @@ type Matcher struct {
Name string
Value string

re *regexp.Regexp
re *regexp.Regexp
cache *sync.Map
}

// NewMatcher returns a matcher object.
Expand All @@ -64,6 +66,7 @@ func NewMatcher(t MatchType, n, v string) (*Matcher, error) {
Type: t,
Name: n,
Value: v,
cache: new(sync.Map),
}
if t == MatchRegexp || t == MatchNotRegexp {
re, err := regexp.Compile("^(?:" + v + ")$")
Expand All @@ -90,13 +93,23 @@ func (m *Matcher) Matches(s string) bool {
case MatchNotEqual:
return s != m.Value
case MatchRegexp:
return m.re.MatchString(s)
return m.matchString(s)
case MatchNotRegexp:
return !m.re.MatchString(s)
return !m.matchString(s)
}
panic("labels.Matcher.Matches: invalid match type")
}

func (m *Matcher) matchString(s string) bool {
if matched, ok := m.cache.Load(s); ok {
return matched.(bool)
}

match := m.re.MatchString(s)
m.cache.Store(s, match)
return match
}

type apiV1Matcher struct {
Name string `json:"name"`
Value string `json:"value"`
Expand Down