2017-03-16 10:16:10 +00:00
|
|
|
package parse
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"regexp"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/prometheus/prometheus/pkg/labels"
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
2018-03-20 09:08:20 +00:00
|
|
|
re = regexp.MustCompile(`(?:\s?)(\w+)(=|=~|!=|!~)(?:\"([^"=~!]+)\"|([^"=~!]+)|\"\")`)
|
2017-03-16 10:16:10 +00:00
|
|
|
typeMap = map[string]labels.MatchType{
|
|
|
|
"=": labels.MatchEqual,
|
|
|
|
"!=": labels.MatchNotEqual,
|
|
|
|
"=~": labels.MatchRegexp,
|
|
|
|
"!~": labels.MatchNotRegexp,
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
|
|
|
func Matchers(s string) ([]*labels.Matcher, error) {
|
|
|
|
matchers := []*labels.Matcher{}
|
|
|
|
if strings.HasPrefix(s, "{") {
|
|
|
|
s = s[1:]
|
|
|
|
}
|
|
|
|
if strings.HasSuffix(s, "}") {
|
|
|
|
s = s[:len(s)-1]
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, toParse := range strings.Split(s, ",") {
|
|
|
|
m, err := Matcher(toParse)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
matchers = append(matchers, m)
|
|
|
|
}
|
|
|
|
return matchers, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func Matcher(s string) (*labels.Matcher, error) {
|
2017-04-20 09:04:17 +00:00
|
|
|
name, value, matchType, err := Input(s)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2017-03-16 10:16:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
m, err := labels.NewMatcher(matchType, name, value)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return m, nil
|
|
|
|
}
|
2017-04-20 09:04:17 +00:00
|
|
|
|
|
|
|
func Input(s string) (name, value string, matchType labels.MatchType, err error) {
|
|
|
|
ms := re.FindStringSubmatch(s)
|
|
|
|
if len(ms) < 4 {
|
2018-04-04 08:37:35 +00:00
|
|
|
return "", "", labels.MatchEqual, fmt.Errorf("bad matcher format: %s", s)
|
2017-04-20 09:04:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var prs bool
|
|
|
|
name = ms[1]
|
|
|
|
matchType, prs = typeMap[ms[2]]
|
|
|
|
|
|
|
|
if ms[3] != "" {
|
|
|
|
value = ms[3]
|
|
|
|
} else {
|
|
|
|
value = ms[4]
|
|
|
|
}
|
|
|
|
|
2018-03-20 09:08:20 +00:00
|
|
|
if name == "" || !prs {
|
2017-04-20 09:04:17 +00:00
|
|
|
return "", "", labels.MatchEqual, fmt.Errorf("failed to parse")
|
|
|
|
}
|
|
|
|
|
|
|
|
return name, value, matchType, nil
|
|
|
|
}
|