-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add new validators alertNameMatchesRegexp, groupNameMatchesRege…
…xp, recordedMetricNameMatchesRegexp (#81) Signed-off-by: Martin Chodur <m.chodur@seznam.cz>
- Loading branch information
Showing
7 changed files
with
223 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
package validator | ||
|
||
import ( | ||
"fmt" | ||
"regexp" | ||
|
||
"github.com/fusakla/promruval/v2/pkg/prometheus" | ||
"github.com/fusakla/promruval/v2/pkg/unmarshaler" | ||
"github.com/prometheus/prometheus/model/rulefmt" | ||
"gopkg.in/yaml.v3" | ||
) | ||
|
||
func newRecordedMetricNameMatchesRegexp(paramsConfig yaml.Node) (Validator, error) { | ||
params := struct { | ||
Regexp string `yaml:"regexp"` | ||
}{} | ||
if err := paramsConfig.Decode(¶ms); err != nil { | ||
return nil, err | ||
} | ||
if params.Regexp == "" { | ||
return nil, fmt.Errorf("missing regexp") | ||
} | ||
r, err := regexp.Compile(params.Regexp) | ||
if err != nil { | ||
return nil, fmt.Errorf("invalid regexp %s: %w", params.Regexp, err) | ||
} | ||
return &recordedMetricNameMatchesRegexp{ | ||
pattern: r, | ||
}, nil | ||
} | ||
|
||
type recordedMetricNameMatchesRegexp struct { | ||
pattern *regexp.Regexp | ||
} | ||
|
||
func (h recordedMetricNameMatchesRegexp) String() string { | ||
return fmt.Sprintf("Recorded metric name matches regexp: %s", h.pattern.String()) | ||
} | ||
|
||
func (h recordedMetricNameMatchesRegexp) Validate(_ unmarshaler.RuleGroup, rule rulefmt.Rule, _ *prometheus.Client) []error { | ||
var errs []error | ||
if !h.pattern.MatchString(rule.Record) { | ||
errs = append(errs, fmt.Errorf("recorded metric name %s does not match pattern %s", rule.Alert, h.pattern.String())) | ||
} | ||
return errs | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters