alertmanager/types/match.go

122 lines
3.2 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-10-22 07:49:16 +00:00
"encoding/json"
2015-07-01 15:56:53 +00:00
"fmt"
"regexp"
"strings"
"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.Value)
2015-07-01 15:56:53 +00:00
}
return fmt.Sprintf("<Matcher %s:%q>", m.Name, m.Value)
}
// MarshalJSON implements json.Marshaler.
2015-10-22 07:49:16 +00:00
func (m *Matcher) MarshalJSON() ([]byte, error) {
v := struct {
Name model.LabelName `json:"name"`
Value string `json:"value"`
IsRegex bool `json:"isRegex"`
}{
Name: m.Name,
Value: m.Value,
IsRegex: m.isRegex,
}
return json.Marshal(&v)
}
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, re *regexp.Regexp) *Matcher {
value := strings.TrimSuffix(strings.TrimPrefix(re.String(), "^(?:"), ")$")
if len(re.String())-len(value) != 6 {
// Any non-anchored regexp is a bug.
panic(fmt.Errorf("regexp %q not properly anchored", re))
}
return &Matcher{
Name: name,
Value: value,
isRegex: true,
regex: re,
}
}
// Matchers provides the Match and Fingerprint methods for a slice of Matchers.
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
}
// Fingerprint returns a quasi-unique fingerprint for the matchers.
func (ms Matchers) Fingerprint() model.Fingerprint {
lset := make(model.LabelSet, 3*len(ms))
for _, m := range ms {
2015-11-05 09:25:25 +00:00
lset[model.LabelName(fmt.Sprintf("%s-%s-%v", m.Name, m.Value, m.isRegex))] = ""
}
return lset.Fingerprint()
}