From 33c1171b9c50296ce61b86c0c4b2dfc19aea6545 Mon Sep 17 00:00:00 2001 From: Julius Volz Date: Tue, 10 Oct 2017 10:10:21 -0700 Subject: [PATCH] Don't add anchoring to exported `Value` matcher field Instead, just make the anchoring part of the internal regex. This helps because some users will want to read back the `Value` field and expect it to be the same as the input value (e.g. some tests in Cortex), or use the value in another context which is already expected to add its own anchoring, leading to superfluous double anchoring (such as when we translate matchers into remote read request matchers). --- pkg/labels/matcher.go | 3 +-- storage/tsdb/tsdb.go | 4 ++-- 2 files changed, 3 insertions(+), 4 deletions(-) diff --git a/pkg/labels/matcher.go b/pkg/labels/matcher.go index 40ae5f76e..7fa5d947e 100644 --- a/pkg/labels/matcher.go +++ b/pkg/labels/matcher.go @@ -59,8 +59,7 @@ func NewMatcher(t MatchType, n, v string) (*Matcher, error) { Value: v, } if t == MatchRegexp || t == MatchNotRegexp { - m.Value = "^(?:" + v + ")$" - re, err := regexp.Compile(m.Value) + re, err := regexp.Compile("^(?:" + v + ")$") if err != nil { return nil, err } diff --git a/storage/tsdb/tsdb.go b/storage/tsdb/tsdb.go index 966dfd55e..33572c93f 100644 --- a/storage/tsdb/tsdb.go +++ b/storage/tsdb/tsdb.go @@ -233,14 +233,14 @@ func convertMatcher(m *labels.Matcher) tsdbLabels.Matcher { return tsdbLabels.Not(tsdbLabels.NewEqualMatcher(m.Name, m.Value)) case labels.MatchRegexp: - res, err := tsdbLabels.NewRegexpMatcher(m.Name, m.Value) + res, err := tsdbLabels.NewRegexpMatcher(m.Name, "^(?:"+m.Value+")$") if err != nil { panic(err) } return res case labels.MatchNotRegexp: - res, err := tsdbLabels.NewRegexpMatcher(m.Name, m.Value) + res, err := tsdbLabels.NewRegexpMatcher(m.Name, "^(?:"+m.Value+")$") if err != nil { panic(err) }