2018-05-14 12:36:24 +00:00
|
|
|
// Copyright 2018 Prometheus Team
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
2019-09-16 08:56:29 +00:00
|
|
|
package labels
|
2017-03-16 10:16:10 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"regexp"
|
|
|
|
"strings"
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
2018-03-20 09:08:20 +00:00
|
|
|
re = regexp.MustCompile(`(?:\s?)(\w+)(=|=~|!=|!~)(?:\"([^"=~!]+)\"|([^"=~!]+)|\"\")`)
|
2019-09-16 08:56:29 +00:00
|
|
|
typeMap = map[string]MatchType{
|
|
|
|
"=": MatchEqual,
|
|
|
|
"!=": MatchNotEqual,
|
|
|
|
"=~": MatchRegexp,
|
|
|
|
"!~": MatchNotRegexp,
|
2017-03-16 10:16:10 +00:00
|
|
|
}
|
|
|
|
)
|
|
|
|
|
2019-09-16 08:56:29 +00:00
|
|
|
func ParseMatchers(s string) ([]*Matcher, error) {
|
|
|
|
matchers := []*Matcher{}
|
2019-01-04 14:37:33 +00:00
|
|
|
s = strings.TrimPrefix(s, "{")
|
|
|
|
s = strings.TrimSuffix(s, "}")
|
2017-03-16 10:16:10 +00:00
|
|
|
|
2018-05-28 09:36:47 +00:00
|
|
|
var insideQuotes bool
|
|
|
|
var token string
|
|
|
|
var tokens []string
|
|
|
|
for _, r := range s {
|
|
|
|
if !insideQuotes && r == ',' {
|
|
|
|
tokens = append(tokens, token)
|
|
|
|
token = ""
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
token += string(r)
|
|
|
|
if r == '"' {
|
|
|
|
insideQuotes = !insideQuotes
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if token != "" {
|
|
|
|
tokens = append(tokens, token)
|
|
|
|
}
|
|
|
|
for _, token := range tokens {
|
2019-09-16 08:56:29 +00:00
|
|
|
m, err := ParseMatcher(token)
|
2017-03-16 10:16:10 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
matchers = append(matchers, m)
|
|
|
|
}
|
2018-05-28 09:36:47 +00:00
|
|
|
|
2017-03-16 10:16:10 +00:00
|
|
|
return matchers, nil
|
|
|
|
}
|
|
|
|
|
2019-09-16 08:56:29 +00:00
|
|
|
func ParseMatcher(s string) (*Matcher, error) {
|
|
|
|
var (
|
|
|
|
name, value string
|
|
|
|
matchType MatchType
|
|
|
|
)
|
2017-04-20 09:04:17 +00:00
|
|
|
|
|
|
|
ms := re.FindStringSubmatch(s)
|
|
|
|
if len(ms) < 4 {
|
2019-09-16 08:56:29 +00:00
|
|
|
return nil, fmt.Errorf("bad matcher format: %s", s)
|
2017-04-20 09:04:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
name = ms[1]
|
2019-09-16 08:56:29 +00:00
|
|
|
if name == "" {
|
|
|
|
return nil, fmt.Errorf("failed to parse label name")
|
|
|
|
}
|
|
|
|
|
|
|
|
matchType, found := typeMap[ms[2]]
|
|
|
|
if !found {
|
|
|
|
return nil, fmt.Errorf("failed to find match operator")
|
|
|
|
}
|
2017-04-20 09:04:17 +00:00
|
|
|
|
|
|
|
if ms[3] != "" {
|
|
|
|
value = ms[3]
|
|
|
|
} else {
|
|
|
|
value = ms[4]
|
|
|
|
}
|
|
|
|
|
2019-09-16 08:56:29 +00:00
|
|
|
return NewMatcher(matchType, name, value)
|
2017-04-20 09:04:17 +00:00
|
|
|
}
|