mirror of
https://github.com/kubernetes-sigs/prometheus-adapter.git
synced 2026-04-06 17:57:51 +00:00
* Some bug fixes found during testing/test repair. * Trying to tease apart the various responsibilities of `metricNamer` into smaller chunks and adding tests for each individual chunk. * Updating the `provider_test` and `series_registry_test` to fix failures.
47 lines
1 KiB
Go
47 lines
1 KiB
Go
package provider
|
|
|
|
import (
|
|
"fmt"
|
|
"regexp"
|
|
|
|
"github.com/directxman12/k8s-prometheus-adapter/pkg/config"
|
|
)
|
|
|
|
// reMatcher either positively or negatively matches a regex
|
|
type reMatcher struct {
|
|
regex *regexp.Regexp
|
|
positive bool
|
|
}
|
|
|
|
func newReMatcher(cfg config.RegexFilter) (*reMatcher, error) {
|
|
if cfg.Is != "" && cfg.IsNot != "" {
|
|
return nil, fmt.Errorf("cannot have both an `is` (%q) and `isNot` (%q) expression in a single filter", cfg.Is, cfg.IsNot)
|
|
}
|
|
if cfg.Is == "" && cfg.IsNot == "" {
|
|
return nil, fmt.Errorf("must have either an `is` or `isNot` expression in a filter")
|
|
}
|
|
|
|
var positive bool
|
|
var regexRaw string
|
|
if cfg.Is != "" {
|
|
positive = true
|
|
regexRaw = cfg.Is
|
|
} else {
|
|
positive = false
|
|
regexRaw = cfg.IsNot
|
|
}
|
|
|
|
regex, err := regexp.Compile(regexRaw)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("unable to compile series filter %q: %v", regexRaw, err)
|
|
}
|
|
|
|
return &reMatcher{
|
|
regex: regex,
|
|
positive: positive,
|
|
}, nil
|
|
}
|
|
|
|
func (m *reMatcher) Matches(val string) bool {
|
|
return m.regex.MatchString(val) == m.positive
|
|
}
|