2015-06-30 12:29:30 +00:00
|
|
|
// Copyright 2015 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.
|
|
|
|
|
2015-09-25 16:14:46 +00:00
|
|
|
package types
|
2015-06-30 12:29:30 +00:00
|
|
|
|
|
|
|
import (
|
2015-07-01 15:56:53 +00:00
|
|
|
"fmt"
|
2015-06-30 12:29:30 +00:00
|
|
|
"regexp"
|
2016-06-09 13:15:33 +00:00
|
|
|
"sort"
|
2015-06-30 12:29:30 +00:00
|
|
|
|
|
|
|
"github.com/prometheus/common/model"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Matcher defines a matching rule for the value of a given label.
|
|
|
|
type Matcher struct {
|
2016-05-29 23:12:05 +00:00
|
|
|
Name string `json:"name"`
|
|
|
|
Value string `json:"value"`
|
|
|
|
IsRegex bool `json:"isRegex"`
|
2015-06-30 12:29:30 +00:00
|
|
|
|
2016-05-29 23:12:05 +00:00
|
|
|
regex *regexp.Regexp
|
2015-06-30 12:29:30 +00:00
|
|
|
}
|
|
|
|
|
2016-05-29 23:12:05 +00:00
|
|
|
// Init internals of the Matcher. Must be called before using Match.
|
|
|
|
func (m *Matcher) Init() error {
|
|
|
|
if !m.IsRegex {
|
|
|
|
return nil
|
2015-07-01 15:56:53 +00:00
|
|
|
}
|
2016-05-29 23:12:05 +00:00
|
|
|
re, err := regexp.Compile(m.Value)
|
|
|
|
if err == nil {
|
|
|
|
m.regex = re
|
2015-10-22 07:49:16 +00:00
|
|
|
}
|
2016-05-29 23:12:05 +00:00
|
|
|
return err
|
2015-10-22 07:49:16 +00:00
|
|
|
}
|
|
|
|
|
2016-05-29 23:12:05 +00:00
|
|
|
func (m *Matcher) String() string {
|
|
|
|
if m.IsRegex {
|
|
|
|
return fmt.Sprintf("<RegexMatcher %s:%q>", m.Name, m.Value)
|
|
|
|
}
|
|
|
|
return fmt.Sprintf("<Matcher %s:%q>", m.Name, m.Value)
|
2015-07-01 11:17:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Match checks whether the label of the matcher has the specified
|
|
|
|
// matching value.
|
|
|
|
func (m *Matcher) Match(lset model.LabelSet) bool {
|
|
|
|
// Unset labels are treated as unset labels globally. Thus, if a
|
|
|
|
// label is not set we retrieve the empty label which is correct
|
|
|
|
// for the comparison below.
|
2016-05-29 23:12:05 +00:00
|
|
|
v := lset[model.LabelName(m.Name)]
|
2015-07-01 11:17:08 +00:00
|
|
|
|
2016-05-29 23:12:05 +00:00
|
|
|
if m.IsRegex {
|
2015-07-01 11:17:08 +00:00
|
|
|
return m.regex.MatchString(string(v))
|
|
|
|
}
|
|
|
|
return string(v) == m.Value
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewMatcher returns a new matcher that compares against equality of
|
|
|
|
// the given value.
|
2015-06-30 12:29:30 +00:00
|
|
|
func NewMatcher(name model.LabelName, value string) *Matcher {
|
|
|
|
return &Matcher{
|
2016-05-29 23:12:05 +00:00
|
|
|
Name: string(name),
|
|
|
|
Value: value,
|
|
|
|
IsRegex: false,
|
2015-06-30 12:29:30 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-05-29 23:12:05 +00:00
|
|
|
// NewRegexMatcher returns a new matcher that compares values against
|
|
|
|
// a regular expression. The matcher is already initialized.
|
|
|
|
//
|
|
|
|
// TODO(fabxc): refactor usage.
|
2015-11-20 14:10:38 +00:00
|
|
|
func NewRegexMatcher(name model.LabelName, re *regexp.Regexp) *Matcher {
|
|
|
|
return &Matcher{
|
2016-05-29 23:12:05 +00:00
|
|
|
Name: string(name),
|
|
|
|
Value: re.String(),
|
|
|
|
IsRegex: true,
|
2015-06-30 12:29:30 +00:00
|
|
|
regex: re,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-11-20 14:10:38 +00:00
|
|
|
// Matchers provides the Match and Fingerprint methods for a slice of Matchers.
|
2016-06-09 13:15:33 +00:00
|
|
|
// Matchers must always be sorted.
|
2015-06-30 12:29:30 +00:00
|
|
|
type Matchers []*Matcher
|
2015-07-01 11:17:08 +00:00
|
|
|
|
2016-06-09 13:15:33 +00:00
|
|
|
// NewMatchers returns the given Matchers sorted.
|
|
|
|
func NewMatchers(ms ...*Matcher) Matchers {
|
|
|
|
m := Matchers(ms)
|
|
|
|
sort.Sort(m)
|
|
|
|
return m
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ms Matchers) Len() int { return len(ms) }
|
|
|
|
func (ms Matchers) Swap(i, j int) { ms[i], ms[j] = ms[j], ms[i] }
|
|
|
|
|
|
|
|
func (ms Matchers) Less(i, j int) bool {
|
|
|
|
if ms[i].Name > ms[j].Name {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
if ms[i].Name < ms[j].Name {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
if ms[i].Value > ms[j].Value {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
if ms[i].Value < ms[j].Value {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
return !ms[i].IsRegex && ms[j].IsRegex
|
|
|
|
}
|
|
|
|
|
|
|
|
// Equal returns whether both Matchers are equal.
|
|
|
|
func (ms Matchers) Equal(o Matchers) bool {
|
|
|
|
if len(ms) != len(o) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
for i, a := range ms {
|
|
|
|
if *a != *o[i] {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2015-07-09 13:01:38 +00:00
|
|
|
// Match checks whether all matchers are fulfilled against the given label set.
|
|
|
|
func (ms Matchers) Match(lset model.LabelSet) bool {
|
2015-07-01 11:17:08 +00:00
|
|
|
for _, m := range ms {
|
|
|
|
if !m.Match(lset) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|
2015-10-21 11:08:53 +00:00
|
|
|
|
2016-05-29 23:12:05 +00:00
|
|
|
// Validate returns true iff all fields of the matcher have valid values.
|
|
|
|
func (m *Matcher) Validate() error {
|
|
|
|
if !model.LabelName(m.Name).IsValid() {
|
|
|
|
return fmt.Errorf("invalid name %q", m.Name)
|
|
|
|
}
|
|
|
|
if m.IsRegex {
|
|
|
|
if _, err := regexp.Compile(m.Value); err != nil {
|
|
|
|
return fmt.Errorf("invalid regular expression %q", m.Value)
|
|
|
|
}
|
|
|
|
} else if !model.LabelValue(m.Value).IsValid() || len(m.Value) == 0 {
|
|
|
|
return fmt.Errorf("invalid value %q", m.Value)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-11-20 14:10:38 +00:00
|
|
|
// Fingerprint returns a quasi-unique fingerprint for the matchers.
|
2015-10-21 11:08:53 +00:00
|
|
|
func (ms Matchers) Fingerprint() model.Fingerprint {
|
|
|
|
lset := make(model.LabelSet, 3*len(ms))
|
|
|
|
|
|
|
|
for _, m := range ms {
|
2016-05-29 23:12:05 +00:00
|
|
|
lset[model.LabelName(fmt.Sprintf("%s-%s-%v", m.Name, m.Value, m.IsRegex))] = ""
|
2015-10-21 11:08:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return lset.Fingerprint()
|
|
|
|
}
|