Skip to content

Commit

Permalink
Move old parser into matcher package
Browse files Browse the repository at this point in the history
This is the first of two commits that removes matchers from the
deprecated pkg/labels package and into the new matcher package.
It moves the old regex parser that is known as classic mode
into a package called oldparse. This package will be deleted
in future when classic mode is removed.

Signed-off-by: George Robinson <[email protected]>
  • Loading branch information
grobinson-grafana committed Aug 22, 2024
1 parent ce94719 commit 508116e
Show file tree
Hide file tree
Showing 4 changed files with 437 additions and 432 deletions.
9 changes: 5 additions & 4 deletions matcher/compat/parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import (
"github.com/prometheus/common/model"

"github.com/prometheus/alertmanager/featurecontrol"
"github.com/prometheus/alertmanager/matcher/oldparse"
"github.com/prometheus/alertmanager/matcher/parse"
"github.com/prometheus/alertmanager/pkg/labels"
)
Expand Down Expand Up @@ -77,7 +78,7 @@ func InitFromFlags(l log.Logger, f featurecontrol.Flagger) {
func ClassicMatcherParser(l log.Logger) ParseMatcher {
return func(input, origin string) (matcher *labels.Matcher, err error) {
level.Debug(l).Log("msg", "Parsing with classic matchers parser", "input", input, "origin", origin)
return labels.ParseMatcher(input)
return oldparse.ParseMatcher(input)
}
}

Expand All @@ -86,7 +87,7 @@ func ClassicMatcherParser(l log.Logger) ParseMatcher {
func ClassicMatchersParser(l log.Logger) ParseMatchers {
return func(input, origin string) (matchers labels.Matchers, err error) {
level.Debug(l).Log("msg", "Parsing with classic matchers parser", "input", input, "origin", origin)
return labels.ParseMatchers(input)
return oldparse.ParseMatchers(input)
}
}

Expand Down Expand Up @@ -124,7 +125,7 @@ func FallbackMatcherParser(l log.Logger) ParseMatcher {
// Parse the input in both parsers to look for disagreement and incompatible
// inputs.
nMatcher, nErr := parse.Matcher(input)
cMatcher, cErr := labels.ParseMatcher(input)
cMatcher, cErr := oldparse.ParseMatcher(input)
if nErr != nil {
// If the input is invalid in both parsers, return the error.
if cErr != nil {
Expand Down Expand Up @@ -155,7 +156,7 @@ func FallbackMatchersParser(l log.Logger) ParseMatchers {
// Parse the input in both parsers to look for disagreement and incompatible
// inputs.
nMatchers, nErr := parse.Matchers(input)
cMatchers, cErr := labels.ParseMatchers(input)
cMatchers, cErr := oldparse.ParseMatchers(input)
if nErr != nil {
// If the input is invalid in both parsers, return the error.
if cErr != nil {
Expand Down
22 changes: 12 additions & 10 deletions pkg/labels/parse.go → matcher/oldparse/parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,24 +11,26 @@
// See the License for the specific language governing permissions and
// limitations under the License.

package labels
package oldparse

import (
"fmt"
"regexp"
"strings"
"unicode/utf8"

"github.com/prometheus/alertmanager/pkg/labels"
)

var (
// '=~' has to come before '=' because otherwise only the '='
// will be consumed, and the '~' will be part of the 3rd token.
re = regexp.MustCompile(`^\s*([a-zA-Z_:][a-zA-Z0-9_:]*)\s*(=~|=|!=|!~)\s*((?s).*?)\s*$`)
typeMap = map[string]MatchType{
"=": MatchEqual,
"!=": MatchNotEqual,
"=~": MatchRegexp,
"!~": MatchNotRegexp,
typeMap = map[string]labels.MatchType{
"=": labels.MatchEqual,
"!=": labels.MatchNotEqual,
"=~": labels.MatchRegexp,
"!~": labels.MatchNotRegexp,
}
)

Expand All @@ -52,8 +54,8 @@ var (
// statuscode=~"5.."
//
// See ParseMatcher for details on how an individual Matcher is parsed.
func ParseMatchers(s string) ([]*Matcher, error) {
matchers := []*Matcher{}
func ParseMatchers(s string) ([]*labels.Matcher, error) {
matchers := []*labels.Matcher{}
s = strings.TrimPrefix(s, "{")
s = strings.TrimSuffix(s, "}")

Expand Down Expand Up @@ -114,7 +116,7 @@ func ParseMatchers(s string) ([]*Matcher, error) {
// character). However, literal line feed characters are tolerated, as are
// single '\' characters not followed by '\', 'n', or '"'. They act as a literal
// backslash in that case.
func ParseMatcher(s string) (_ *Matcher, err error) {
func ParseMatcher(s string) (_ *labels.Matcher, err error) {
ms := re.FindStringSubmatch(s)
if len(ms) == 0 {
return nil, fmt.Errorf("bad matcher format: %s", s)
Expand Down Expand Up @@ -174,5 +176,5 @@ func ParseMatcher(s string) (_ *Matcher, err error) {
return nil, fmt.Errorf("matcher value contains unescaped double quote: %s", ms[3])
}

return NewMatcher(typeMap[ms[2]], ms[1], value.String())
return labels.NewMatcher(typeMap[ms[2]], ms[1], value.String())
}
Loading

0 comments on commit 508116e

Please sign in to comment.