mirror of
https://github.com/prometheus/prometheus.git
synced 2024-11-14 17:44:06 -08:00
Expose Matcher.Prefix()
Sometimes label matchers know that they match values with a specific prefix. This information can be valuable in some downstream storage implementations. Signed-off-by: Oleg Zaytsev <mail@olegzaytsev.com>
This commit is contained in:
parent
44904a663c
commit
2512c019d3
|
@ -128,3 +128,12 @@ func (m *Matcher) SetMatches() []string {
|
||||||
}
|
}
|
||||||
return m.re.setMatches
|
return m.re.setMatches
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Prefix returns the required prefix of the value to match, if possible.
|
||||||
|
// It will be empty if it's an equality matcher or if the prefix can't be determined.
|
||||||
|
func (m *Matcher) Prefix() string {
|
||||||
|
if m.re == nil {
|
||||||
|
return ""
|
||||||
|
}
|
||||||
|
return m.re.prefix
|
||||||
|
}
|
||||||
|
|
|
@ -14,13 +14,14 @@
|
||||||
package labels
|
package labels
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"fmt"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"github.com/stretchr/testify/require"
|
"github.com/stretchr/testify/require"
|
||||||
)
|
)
|
||||||
|
|
||||||
func mustNewMatcher(t *testing.T, mType MatchType, value string) *Matcher {
|
func mustNewMatcher(t *testing.T, mType MatchType, value string) *Matcher {
|
||||||
m, err := NewMatcher(mType, "", value)
|
m, err := NewMatcher(mType, "test_label_name", value)
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
return m
|
return m
|
||||||
}
|
}
|
||||||
|
@ -133,6 +134,58 @@ func TestInverse(t *testing.T) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestPrefix(t *testing.T) {
|
||||||
|
for i, tc := range []struct {
|
||||||
|
matcher *Matcher
|
||||||
|
prefix string
|
||||||
|
}{
|
||||||
|
{
|
||||||
|
matcher: mustNewMatcher(t, MatchEqual, "abc"),
|
||||||
|
prefix: "",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
matcher: mustNewMatcher(t, MatchNotEqual, "abc"),
|
||||||
|
prefix: "",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
matcher: mustNewMatcher(t, MatchRegexp, "abc.+"),
|
||||||
|
prefix: "abc",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
matcher: mustNewMatcher(t, MatchRegexp, "abcd|abc.+"),
|
||||||
|
prefix: "abc",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
matcher: mustNewMatcher(t, MatchNotRegexp, "abcd|abc.+"),
|
||||||
|
prefix: "abc",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
matcher: mustNewMatcher(t, MatchRegexp, "abc(def|ghj)|ab|a."),
|
||||||
|
prefix: "a",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
matcher: mustNewMatcher(t, MatchRegexp, "foo.+bar|foo.*baz"),
|
||||||
|
prefix: "foo",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
matcher: mustNewMatcher(t, MatchRegexp, "abc|.*"),
|
||||||
|
prefix: "",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
matcher: mustNewMatcher(t, MatchRegexp, "abc|def"),
|
||||||
|
prefix: "",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
matcher: mustNewMatcher(t, MatchRegexp, ".+def"),
|
||||||
|
prefix: "",
|
||||||
|
},
|
||||||
|
} {
|
||||||
|
t.Run(fmt.Sprintf("%d: %s", i, tc.matcher), func(t *testing.T) {
|
||||||
|
require.Equal(t, tc.prefix, tc.matcher.Prefix())
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func BenchmarkMatchType_String(b *testing.B) {
|
func BenchmarkMatchType_String(b *testing.B) {
|
||||||
for i := 0; i <= b.N; i++ {
|
for i := 0; i <= b.N; i++ {
|
||||||
_ = MatchType(i % int(MatchNotRegexp+1)).String()
|
_ = MatchType(i % int(MatchNotRegexp+1)).String()
|
||||||
|
|
Loading…
Reference in a new issue