*: improve error messages when parsing bad rules (#5965)

Signed-off-by: Simon Pasquier <spasquie@redhat.com>
This commit is contained in:
Simon Pasquier 2019-08-28 17:36:48 +02:00 committed by GitHub
parent 1fa5a75a3a
commit 06066a3619
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 20 additions and 15 deletions

View File

@ -163,8 +163,11 @@ func CheckConfig(files ...string) int {
fmt.Println() fmt.Println()
for _, rf := range ruleFiles { for _, rf := range ruleFiles {
if n, err := checkRules(rf); err != nil { if n, errs := checkRules(rf); len(errs) > 0 {
fmt.Fprintln(os.Stderr, " FAILED:", err) fmt.Fprintln(os.Stderr, " FAILED:")
for _, err := range errs {
fmt.Fprintln(os.Stderr, " ", err)
}
failed = true failed = true
} else { } else {
fmt.Printf(" SUCCESS: %d rules found\n", n) fmt.Printf(" SUCCESS: %d rules found\n", n)

View File

@ -113,7 +113,7 @@ func (r *Rule) Validate() (errs []error) {
if r.Expr == "" { if r.Expr == "" {
errs = append(errs, errors.Errorf("field 'expr' must be set in rule")) errs = append(errs, errors.Errorf("field 'expr' must be set in rule"))
} else if _, err := promql.ParseExpr(r.Expr); err != nil { } else if _, err := promql.ParseExpr(r.Expr); err != nil {
errs = append(errs, errors.Errorf("could not parse expression: %s", err)) errs = append(errs, errors.Wrap(err, "could not parse expression"))
} }
if r.Record != "" { if r.Record != "" {
if len(r.Annotations) > 0 { if len(r.Annotations) > 0 {
@ -143,8 +143,7 @@ func (r *Rule) Validate() (errs []error) {
} }
} }
errs = append(errs, testTemplateParsing(r)...) return append(errs, testTemplateParsing(r)...)
return errs
} }
// testTemplateParsing checks if the templates used in labels and annotations // testTemplateParsing checks if the templates used in labels and annotations
@ -176,18 +175,18 @@ func testTemplateParsing(rl *Rule) (errs []error) {
} }
// Parsing Labels. // Parsing Labels.
for _, val := range rl.Labels { for k, val := range rl.Labels {
err := parseTest(val) err := parseTest(val)
if err != nil { if err != nil {
errs = append(errs, errors.Errorf("msg=%s", err.Error())) errs = append(errs, errors.Wrapf(err, "label %q", k))
} }
} }
// Parsing Annotations. // Parsing Annotations.
for _, val := range rl.Annotations { for k, val := range rl.Annotations {
err := parseTest(val) err := parseTest(val)
if err != nil { if err != nil {
errs = append(errs, errors.Errorf("msg=%s", err.Error())) errs = append(errs, errors.Wrapf(err, "annotation %q", k))
} }
} }
@ -207,7 +206,11 @@ func Parse(content []byte) (*RuleGroups, []error) {
func ParseFile(file string) (*RuleGroups, []error) { func ParseFile(file string) (*RuleGroups, []error) {
b, err := ioutil.ReadFile(file) b, err := ioutil.ReadFile(file)
if err != nil { if err != nil {
return nil, []error{err} return nil, []error{errors.Wrap(err, file)}
} }
return Parse(b) rgs, errs := Parse(b)
for i := range errs {
errs[i] = errors.Wrap(errs[i], file)
}
return rgs, errs
} }

View File

@ -15,18 +15,17 @@ package rules
import ( import (
"context" "context"
"errors" html_template "html/template"
"math" "math"
"net/url" "net/url"
"sort" "sort"
"sync" "sync"
"time" "time"
html_template "html/template"
"github.com/go-kit/kit/log" "github.com/go-kit/kit/log"
"github.com/go-kit/kit/log/level" "github.com/go-kit/kit/log/level"
opentracing "github.com/opentracing/opentracing-go" opentracing "github.com/opentracing/opentracing-go"
"github.com/pkg/errors"
"github.com/prometheus/client_golang/prometheus" "github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/common/model" "github.com/prometheus/common/model"
@ -898,7 +897,7 @@ func (m *Manager) LoadGroups(
for _, r := range rg.Rules { for _, r := range rg.Rules {
expr, err := promql.ParseExpr(r.Expr) expr, err := promql.ParseExpr(r.Expr)
if err != nil { if err != nil {
return nil, []error{err} return nil, []error{errors.Wrap(err, fn)}
} }
if r.Alert != "" { if r.Alert != "" {