export ValidateMatcher for DI (#2) (#2716)

so third parties, Grafana in particular, can over ride the validation.

Grafana wants to do this because other data sources will have label keys with things like spaces, periods, or other characters - and looking for a better integration with alert manager.

goes with grafana/grafana#38629
replaces https://github.com/prometheus/alertmanager/pull/2694

Signed-off-by: Kyle Brandt <kyle@grafana.com>
This commit is contained in:
Kyle Brandt 2021-10-21 03:29:55 -04:00 committed by GitHub
parent 186362cef0
commit 1b8afe7cb5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 4 additions and 3 deletions

View File

@ -438,7 +438,8 @@ func (s *Silences) GC() (int, error) {
return n, nil
}
func validateMatcher(m *pb.Matcher) error {
// ValidateMatcher runs validation on the matcher name, type, and pattern.
var ValidateMatcher = func(m *pb.Matcher) error {
if !model.LabelName(m.Name).IsValid() {
return fmt.Errorf("invalid label name %q", m.Name)
}
@ -478,7 +479,7 @@ func validateSilence(s *pb.Silence) error {
}
allMatchEmpty := true
for i, m := range s.Matchers {
if err := validateMatcher(m); err != nil {
if err := ValidateMatcher(m); err != nil {
return fmt.Errorf("invalid label matcher %d: %s", i, err)
}
allMatchEmpty = allMatchEmpty && matchesEmpty(m)

View File

@ -1091,7 +1091,7 @@ func TestValidateMatcher(t *testing.T) {
}
for _, c := range cases {
checkErr(t, c.err, validateMatcher(c.m))
checkErr(t, c.err, ValidateMatcher(c.m))
}
}