alertmanager/types/match.go

94 lines
2.3 KiB
Go
Raw Normal View History

// 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
import (
2015-07-01 15:56:53 +00:00
"fmt"
"regexp"
"github.com/prometheus/common/model"
)
// Matcher defines a matching rule for the value of a given label.
type Matcher struct {
Name model.LabelName
Value string
isRegex bool
regex *regexp.Regexp
}
2015-07-01 15:56:53 +00:00
func (m *Matcher) String() string {
if m.isRegex {
return fmt.Sprintf("<RegexMatcher %s:%q>", m.Name, m.regex)
}
return fmt.Sprintf("<Matcher %s:%q>", m.Name, m.Value)
}
2015-07-01 11:17:08 +00:00
// IsRegex returns true of the matcher compares against a regular expression.
func (m *Matcher) IsRegex() bool {
return m.isRegex
}
// 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.
v := lset[m.Name]
if m.isRegex {
return m.regex.MatchString(string(v))
}
return string(v) == m.Value
}
// NewMatcher returns a new matcher that compares against equality of
// the given value.
func NewMatcher(name model.LabelName, value string) *Matcher {
return &Matcher{
Name: name,
Value: value,
}
}
2015-07-01 11:17:08 +00:00
// NewRegexMatcher returns a new matcher that treats value as a regular
// expression which is used for matching.
func NewRegexMatcher(name model.LabelName, value string) (*Matcher, error) {
re, err := regexp.Compile(value)
if err != nil {
return nil, err
}
m := &Matcher{
Name: name,
Value: value,
isRegex: true,
regex: re,
}
return m, nil
}
type Matchers []*Matcher
2015-07-01 11:17:08 +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
}