Browse Source

Validate labels and annotations

Signed-off-by: Goutham Veeramachaneni <cs14btech11014@iith.ac.in>
pull/2842/head
Goutham Veeramachaneni 8 years ago
parent
commit
e893c89333
No known key found for this signature in database
GPG Key ID: F1C217E8E9023CAD
  1. 18
      pkg/rulefmt/rulefmt.go
  2. 8
      pkg/rulefmt/rulefmt_test.go
  3. 8
      pkg/rulefmt/testdata/bad_annotation.bad.yaml
  4. 8
      pkg/rulefmt/testdata/bad_lname.bad.yaml
  5. 18
      pkg/rulefmt/testdata/test.yaml
  6. 4
      rules/manager.go

18
pkg/rulefmt/rulefmt.go

@ -18,6 +18,7 @@ import (
"github.com/ghodss/yaml" "github.com/ghodss/yaml"
"github.com/pkg/errors" "github.com/pkg/errors"
"github.com/prometheus/common/model"
"github.com/prometheus/prometheus/promql" "github.com/prometheus/prometheus/promql"
) )
@ -111,6 +112,23 @@ func (r *Rule) Validate() (errs []error) {
errs = append(errs, errors.Errorf("invalid field 'for' in recording rule")) errs = append(errs, errors.Errorf("invalid field 'for' in recording rule"))
} }
} }
for k, v := range r.Labels {
if !model.LabelName(k).IsValid() {
errs = append(errs, errors.Errorf("invalid label name: %s", k))
}
if !model.LabelValue(v).IsValid() {
errs = append(errs, errors.Errorf("invalid label value: %s", v))
}
}
for k := range r.Annotations {
if !model.LabelName(k).IsValid() {
errs = append(errs, errors.Errorf("invalid annotation name: %s", k))
}
}
return errs return errs
} }

8
pkg/rulefmt/rulefmt_test.go

@ -57,6 +57,14 @@ func TestParseFileFailure(t *testing.T) {
filename: "noexpr.bad.yaml", filename: "noexpr.bad.yaml",
errMsg: "field 'expr' must be set in rule", errMsg: "field 'expr' must be set in rule",
}, },
{
filename: "bad_lname.bad.yaml",
errMsg: "invalid label name",
},
{
filename: "bad_annotation.bad.yaml",
errMsg: "invalid annotation name",
},
} }
for _, c := range table { for _, c := range table {

8
pkg/rulefmt/testdata/bad_annotation.bad.yaml vendored

@ -0,0 +1,8 @@
Version: 1
Groups:
- name: yolo
rules:
- alert: hola
expr: 1
annotations:
ins-tance: localhost

8
pkg/rulefmt/testdata/bad_lname.bad.yaml vendored

@ -0,0 +1,8 @@
Version: 1
Groups:
- name: yolo
rules:
- record: hola
expr: 1
labels:
ins-tance: localhost

18
pkg/rulefmt/testdata/test.yaml vendored

@ -6,7 +6,8 @@ groups:
- alert: HighErrors - alert: HighErrors
expr: | expr: |
sum without(instance) (rate(errors_total[5m])) sum without(instance) (rate(errors_total[5m]))
/ sum without(instance) (rate(requests_total[5m])) /
sum without(instance) (rate(requests_total[5m]))
for: 5m for: 5m
labels: labels:
severity: critical severity: critical
@ -17,7 +18,8 @@ groups:
- record: "new_metric" - record: "new_metric"
expr: | expr: |
sum without(instance) (rate(errors_total[5m])) sum without(instance) (rate(errors_total[5m]))
/ sum without(instance) (rate(requests_total[5m])) /
sum without(instance) (rate(requests_total[5m]))
labels: labels:
abc: edf abc: edf
uvw: xyz uvw: xyz
@ -25,7 +27,8 @@ groups:
- alert: HighErrors - alert: HighErrors
expr: | expr: |
sum without(instance) (rate(errors_total[5m])) sum without(instance) (rate(errors_total[5m]))
/ sum without(instance) (rate(requests_total[5m])) /
sum without(instance) (rate(requests_total[5m]))
for: 5m for: 5m
labels: labels:
severity: critical severity: critical
@ -38,7 +41,8 @@ groups:
- alert: HighErrors - alert: HighErrors
expr: | expr: |
sum without(instance) (rate(errors_total[5m])) sum without(instance) (rate(errors_total[5m]))
/ sum without(instance) (rate(requests_total[5m])) /
sum without(instance) (rate(requests_total[5m]))
for: 5m for: 5m
labels: labels:
severity: critical severity: critical
@ -46,12 +50,14 @@ groups:
- record: "new_metric" - record: "new_metric"
expr: | expr: |
sum without(instance) (rate(errors_total[5m])) sum without(instance) (rate(errors_total[5m]))
/ sum without(instance) (rate(requests_total[5m])) /
sum without(instance) (rate(requests_total[5m]))
- alert: HighErrors - alert: HighErrors
expr: | expr: |
sum without(instance) (rate(errors_total[5m])) sum without(instance) (rate(errors_total[5m]))
/ sum without(instance) (rate(requests_total[5m])) /
sum without(instance) (rate(requests_total[5m]))
for: 5m for: 5m
labels: labels:
severity: critical severity: critical

4
rules/manager.go

@ -267,9 +267,7 @@ func typeForRule(r Rule) ruleType {
panic(fmt.Errorf("unknown rule type: %T", r)) panic(fmt.Errorf("unknown rule type: %T", r))
} }
// Eval runs a single evaluation cycle in which all rules are evaluated in parallel. // Eval runs a single evaluation cycle in which all rules are evaluated sequentially.
// In the future a single group will be evaluated sequentially to properly handle
// rule dependency.
func (g *Group) Eval(ts time.Time) { func (g *Group) Eval(ts time.Time) {
for i, rule := range g.rules { for i, rule := range g.rules {
rtyp := string(typeForRule(rule)) rtyp := string(typeForRule(rule))

Loading…
Cancel
Save