|
|
@ -14,11 +14,16 @@
|
|
|
|
package rulefmt
|
|
|
|
package rulefmt
|
|
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
import (
|
|
|
|
|
|
|
|
"context"
|
|
|
|
|
|
|
|
"fmt"
|
|
|
|
"io/ioutil"
|
|
|
|
"io/ioutil"
|
|
|
|
|
|
|
|
"time"
|
|
|
|
|
|
|
|
|
|
|
|
"github.com/pkg/errors"
|
|
|
|
"github.com/pkg/errors"
|
|
|
|
"github.com/prometheus/common/model"
|
|
|
|
"github.com/prometheus/common/model"
|
|
|
|
|
|
|
|
"github.com/prometheus/prometheus/pkg/timestamp"
|
|
|
|
"github.com/prometheus/prometheus/promql"
|
|
|
|
"github.com/prometheus/prometheus/promql"
|
|
|
|
|
|
|
|
"github.com/prometheus/prometheus/template"
|
|
|
|
yaml "gopkg.in/yaml.v2"
|
|
|
|
yaml "gopkg.in/yaml.v2"
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
@ -137,6 +142,50 @@ func (r *Rule) Validate() (errs []error) {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
errs = append(errs, testTemplateParsing(r)...)
|
|
|
|
|
|
|
|
return errs
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// testTemplateParsing checks if the templates used in labels and annotations
|
|
|
|
|
|
|
|
// of the alerting rules are parsed correctly.
|
|
|
|
|
|
|
|
func testTemplateParsing(rl *Rule) (errs []error) {
|
|
|
|
|
|
|
|
if rl.Alert == "" {
|
|
|
|
|
|
|
|
// Not an alerting rule.
|
|
|
|
|
|
|
|
return errs
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Trying to parse templates.
|
|
|
|
|
|
|
|
tmplData := template.AlertTemplateData(make(map[string]string), 0)
|
|
|
|
|
|
|
|
defs := "{{$labels := .Labels}}{{$value := .Value}}"
|
|
|
|
|
|
|
|
parseTest := func(text string) error {
|
|
|
|
|
|
|
|
tmpl := template.NewTemplateExpander(
|
|
|
|
|
|
|
|
context.TODO(),
|
|
|
|
|
|
|
|
defs+text,
|
|
|
|
|
|
|
|
"__alert_"+rl.Alert,
|
|
|
|
|
|
|
|
tmplData,
|
|
|
|
|
|
|
|
model.Time(timestamp.FromTime(time.Now())),
|
|
|
|
|
|
|
|
nil,
|
|
|
|
|
|
|
|
nil,
|
|
|
|
|
|
|
|
)
|
|
|
|
|
|
|
|
return tmpl.ParseTest()
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Parsing Labels.
|
|
|
|
|
|
|
|
for _, val := range rl.Labels {
|
|
|
|
|
|
|
|
err := parseTest(val)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
|
|
errs = append(errs, fmt.Errorf("msg=%s", err.Error()))
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Parsing Annotations.
|
|
|
|
|
|
|
|
for _, val := range rl.Annotations {
|
|
|
|
|
|
|
|
err := parseTest(val)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
|
|
errs = append(errs, fmt.Errorf("msg=%s", err.Error()))
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return errs
|
|
|
|
return errs
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|