2013-07-16 15:03:56 +00:00
|
|
|
// Copyright 2013 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.
|
|
|
|
|
2013-07-17 15:45:01 +00:00
|
|
|
package manager
|
2013-07-16 15:03:56 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"hash/fnv"
|
|
|
|
"regexp"
|
|
|
|
)
|
|
|
|
|
2013-07-17 15:45:01 +00:00
|
|
|
type Filters []*Filter
|
|
|
|
|
2013-07-16 15:03:56 +00:00
|
|
|
type Filter struct {
|
2013-08-09 14:02:05 +00:00
|
|
|
Name *regexp.Regexp
|
|
|
|
Value *regexp.Regexp
|
|
|
|
NamePattern string
|
|
|
|
ValuePattern string
|
2013-07-16 15:03:56 +00:00
|
|
|
|
|
|
|
fingerprint uint64
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewFilter(namePattern string, valuePattern string) *Filter {
|
|
|
|
summer := fnv.New64a()
|
|
|
|
fmt.Fprintf(summer, namePattern, valuePattern)
|
|
|
|
|
|
|
|
return &Filter{
|
2013-08-09 14:02:05 +00:00
|
|
|
Name: regexp.MustCompile("^" + namePattern + "$"),
|
|
|
|
Value: regexp.MustCompile("^" + valuePattern + "$"),
|
|
|
|
NamePattern: namePattern,
|
|
|
|
ValuePattern: valuePattern,
|
|
|
|
fingerprint: summer.Sum64(),
|
2013-07-16 15:03:56 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-08-27 13:32:08 +00:00
|
|
|
func (f *Filter) Handles(l AlertLabelSet) bool {
|
|
|
|
for k, v := range l {
|
2013-07-16 15:03:56 +00:00
|
|
|
if f.Name.MatchString(k) && f.Value.MatchString(v) {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2013-08-27 13:32:08 +00:00
|
|
|
func (f Filters) Handles(l AlertLabelSet) bool {
|
2013-07-16 15:03:56 +00:00
|
|
|
fCount := len(f)
|
|
|
|
fMatch := 0
|
|
|
|
|
|
|
|
for _, filter := range f {
|
2013-08-27 13:32:08 +00:00
|
|
|
if filter.Handles(l) {
|
2013-07-16 15:03:56 +00:00
|
|
|
fMatch++
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return fCount == fMatch
|
|
|
|
}
|
|
|
|
|
2013-08-27 13:32:08 +00:00
|
|
|
func (f Filters) Filter(l AlertLabelSets) AlertLabelSets {
|
|
|
|
out := AlertLabelSets{}
|
|
|
|
for _, labels := range l {
|
|
|
|
if f.Handles(labels) {
|
|
|
|
out = append(out, labels)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return out
|
|
|
|
}
|
|
|
|
|
2013-07-16 15:03:56 +00:00
|
|
|
func (f Filters) fingerprint() uint64 {
|
|
|
|
summer := fnv.New64a()
|
|
|
|
|
|
|
|
for i, f := range f {
|
|
|
|
fmt.Fprintln(summer, i, f.fingerprint)
|
|
|
|
}
|
|
|
|
|
|
|
|
return summer.Sum64()
|
|
|
|
}
|