Skip to content

Commit

Permalink
feat: add new validator keepFiringForIsNotLongerThan
Browse files Browse the repository at this point in the history
Signed-off-by: Martin Chodur <[email protected]>
  • Loading branch information
FUSAKLA committed Mar 1, 2024
1 parent 80a54eb commit 2aeb916
Show file tree
Hide file tree
Showing 7 changed files with 46 additions and 1 deletion.
10 changes: 10 additions & 0 deletions docs/validations.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ All the supported validations are listed here. The validations are grouped by th
- [`validateAnnotationTemplates`](#validateannotationtemplates)
- [Other](#other-1)
- [`forIsNotLongerThan`](#forisnotlongerthan)
- [`keepFiringForIsNotLongerThan`](#keepfiringforisnotlongerthan)
- [Recording rules validators](#recording-rules-validators)


Expand Down Expand Up @@ -419,5 +420,14 @@ params:
limit: "1h"
```

#### `keepFiringForIsNotLongerThan`

Fails if the alert uses longer `keep_firing_for` than the specified limit.

```yaml
params:
limit: "1h"
```

## Recording rules validators
Validators that can be used on `Recording rule` scope.
1 change: 1 addition & 0 deletions examples/human_readable.html
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ <h2><a href="#check-metric-name">check-metric-name</a></h2>
<ul>
<li>Alert expression uses metric name in selectors</li>
<li>Alert labels are valid templates</li>
<li>Alert <code>keep_firing_for</code> is not longer than <code>1h</code></li>
</ul>

<h2><a href="#check-groups">check-groups</a></h2>
Expand Down
1 change: 1 addition & 0 deletions examples/human_readable.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ Validation rules:
check-metric-name
- Alert expression uses metric name in selectors
- Alert labels are valid templates
- Alert `keep_firing_for` is not longer than `1h`

check-groups
- Group does not have other `source_tenants` than: `tenant1`, `tenant2`, `k8s`
Expand Down
3 changes: 3 additions & 0 deletions examples/validation.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,9 @@ validationRules:
validations:
- type: expressionWithNoMetricName
- type: validateLabelTemplates
- type: keepFiringForIsNotLongerThan
params:
limit: "1h"

- name: check-groups
scope: Group
Expand Down
25 changes: 25 additions & 0 deletions pkg/validator/alert.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,31 @@ func (h forIsNotLongerThan) Validate(_ unmarshaler.RuleGroup, rule rulefmt.Rule,
return nil
}

func newKeepFiringForIsNotLongerThan(paramsConfig yaml.Node) (Validator, error) {
params := struct {
Limit model.Duration `yaml:"limit"`
}{}
if err := paramsConfig.Decode(&params); err != nil {
return nil, err
}
return &keepFiringForIsNotLongerThan{limit: params.Limit}, nil
}

type keepFiringForIsNotLongerThan struct {
limit model.Duration
}

func (h keepFiringForIsNotLongerThan) String() string {
return fmt.Sprintf("`keep_firing_for` is not longer than `%s`", h.limit)
}

func (h keepFiringForIsNotLongerThan) Validate(_ unmarshaler.RuleGroup, rule rulefmt.Rule, _ *prometheus.Client) []error {
if rule.KeepFiringFor > h.limit {
return []error{fmt.Errorf("alert has `keep_firing_for: %s` which is longer than the specified limit of %s", rule.KeepFiringFor, h.limit)}
}
return nil
}

func newValidateLabelTemplates(paramsConfig yaml.Node) (Validator, error) {
params := struct{}{}
if err := paramsConfig.Decode(&params); err != nil {
Expand Down
3 changes: 2 additions & 1 deletion pkg/validator/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,8 @@ var registeredUniversalRuleValidators = map[string]validatorCreator{
var registeredRecordingRuleValidators = map[string]validatorCreator{}

var registeredAlertValidators = map[string]validatorCreator{
"forIsNotLongerThan": newForIsNotLongerThan,
"forIsNotLongerThan": newForIsNotLongerThan,
"keepFiringForIsNotLongerThan": newKeepFiringForIsNotLongerThan,

"validateAnnotationTemplates": newValidateAnnotationTemplates,
"annotationIsValidPromQL": newAnnotationIsValidPromQL,
Expand Down
4 changes: 4 additions & 0 deletions pkg/validator/validator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,10 @@ var testCases = []struct {
{name: "noTemplate", validator: validateLabelTemplates{}, rule: rulefmt.Rule{Labels: map[string]string{"foo": "bar"}}, expectedErrors: 0},
{name: "validLabelTemplate", validator: validateLabelTemplates{}, rule: rulefmt.Rule{Labels: map[string]string{"foo": "foo {{ $value | humanizeDuration }} bar"}}, expectedErrors: 0},
{name: "invalidLabelTemplate", validator: validateLabelTemplates{}, rule: rulefmt.Rule{Labels: map[string]string{"foo": "foo {{ $value | huuuuumanizeDuration }} bar"}}, expectedErrors: 1},

// keepFiringForIsNotLongerThan
{name: "keepFiringForIsNotLongerThanOK", validator: keepFiringForIsNotLongerThan{limit: model.Duration(time.Minute)}, rule: rulefmt.Rule{KeepFiringFor: model.Duration(time.Second)}, expectedErrors: 0},
{name: "keepFiringForIsNotLongerThanWrong", validator: keepFiringForIsNotLongerThan{limit: model.Duration(time.Minute)}, rule: rulefmt.Rule{KeepFiringFor: model.Duration(time.Minute * 2)}, expectedErrors: 1},
}

func Test(t *testing.T) {
Expand Down

0 comments on commit 2aeb916

Please sign in to comment.