mirror of https://github.com/prometheus/prometheus
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
110 lines
3.3 KiB
110 lines
3.3 KiB
8 years ago
|
// Copyright 2017 The Prometheus Authors
|
||
|
// 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.
|
||
|
|
||
8 years ago
|
package labels
|
||
|
|
||
7 years ago
|
import (
|
||
6 years ago
|
"fmt"
|
||
7 years ago
|
"regexp"
|
||
|
)
|
||
8 years ago
|
|
||
|
// Selector holds constraints for matching against a label set.
|
||
|
type Selector []Matcher
|
||
|
|
||
|
// Matches returns whether the labels satisfy all matchers.
|
||
|
func (s Selector) Matches(labels Labels) bool {
|
||
|
for _, m := range s {
|
||
|
if v := labels.Get(m.Name()); !m.Matches(v) {
|
||
|
return false
|
||
|
}
|
||
|
}
|
||
|
return true
|
||
|
}
|
||
|
|
||
|
// Matcher specifies a constraint for the value of a label.
|
||
|
type Matcher interface {
|
||
|
// Name returns the label name the matcher should apply to.
|
||
|
Name() string
|
||
|
// Matches checks whether a value fulfills the constraints.
|
||
|
Matches(v string) bool
|
||
6 years ago
|
// String returns a human readable matcher.
|
||
|
String() string
|
||
8 years ago
|
}
|
||
|
|
||
8 years ago
|
// EqualMatcher matches on equality.
|
||
8 years ago
|
type EqualMatcher struct {
|
||
8 years ago
|
name, value string
|
||
8 years ago
|
}
|
||
|
|
||
8 years ago
|
// Name implements Matcher interface.
|
||
6 years ago
|
func (m EqualMatcher) Name() string { return m.name }
|
||
8 years ago
|
|
||
|
// Matches implements Matcher interface.
|
||
6 years ago
|
func (m EqualMatcher) Matches(v string) bool { return v == m.value }
|
||
|
|
||
|
// String implements Matcher interface.
|
||
|
func (m EqualMatcher) String() string { return fmt.Sprintf("%s=%q", m.name, m.value) }
|
||
8 years ago
|
|
||
8 years ago
|
// Value returns the matched value.
|
||
6 years ago
|
func (m EqualMatcher) Value() string { return m.value }
|
||
8 years ago
|
|
||
8 years ago
|
// NewEqualMatcher returns a new matcher matching an exact label value.
|
||
|
func NewEqualMatcher(name, value string) Matcher {
|
||
8 years ago
|
return &EqualMatcher{name: name, value: value}
|
||
8 years ago
|
}
|
||
|
|
||
6 years ago
|
type RegexpMatcher struct {
|
||
8 years ago
|
name string
|
||
|
re *regexp.Regexp
|
||
|
}
|
||
|
|
||
6 years ago
|
func (m RegexpMatcher) Name() string { return m.name }
|
||
|
func (m RegexpMatcher) Matches(v string) bool { return m.re.MatchString(v) }
|
||
|
func (m RegexpMatcher) String() string { return fmt.Sprintf("%s=~%q", m.name, m.re.String()) }
|
||
|
func (m RegexpMatcher) Value() string { return m.re.String() }
|
||
8 years ago
|
|
||
|
// NewRegexpMatcher returns a new matcher verifying that a value matches
|
||
|
// the regular expression pattern.
|
||
|
func NewRegexpMatcher(name, pattern string) (Matcher, error) {
|
||
|
re, err := regexp.Compile(pattern)
|
||
|
if err != nil {
|
||
|
return nil, err
|
||
|
}
|
||
6 years ago
|
return &RegexpMatcher{name: name, re: re}, nil
|
||
8 years ago
|
}
|
||
|
|
||
6 years ago
|
// NewMustRegexpMatcher returns a new matcher verifying that a value matches
|
||
7 years ago
|
// the regular expression pattern. Will panic if the pattern is not a valid
|
||
|
// regular expression.
|
||
|
func NewMustRegexpMatcher(name, pattern string) Matcher {
|
||
|
re, err := regexp.Compile(pattern)
|
||
|
if err != nil {
|
||
|
panic(err)
|
||
|
}
|
||
6 years ago
|
return &RegexpMatcher{name: name, re: re}
|
||
7 years ago
|
|
||
|
}
|
||
|
|
||
6 years ago
|
// NotMatcher inverts the matching result for a matcher.
|
||
|
type NotMatcher struct {
|
||
8 years ago
|
Matcher
|
||
|
}
|
||
|
|
||
6 years ago
|
func (m NotMatcher) Matches(v string) bool { return !m.Matcher.Matches(v) }
|
||
|
func (m NotMatcher) String() string { return fmt.Sprintf("not(%s)", m.Matcher.String()) }
|
||
8 years ago
|
|
||
|
// Not inverts the matcher's matching result.
|
||
|
func Not(m Matcher) Matcher {
|
||
6 years ago
|
return &NotMatcher{m}
|
||
8 years ago
|
}
|