From ea0cee4c35ebbb9459dfc48824a361a86b24fa57 Mon Sep 17 00:00:00 2001 From: Kyle Brandt Date: Mon, 22 Jul 2019 13:23:33 -0400 Subject: [PATCH] alerting: more specific error when missing threshold (#18221) fixes #18184 --- pkg/services/alerting/conditions/evaluator.go | 20 +++++++++++++++++-- pkg/services/alerting/conditions/query.go | 2 +- 2 files changed, 19 insertions(+), 3 deletions(-) diff --git a/pkg/services/alerting/conditions/evaluator.go b/pkg/services/alerting/conditions/evaluator.go index 3045b633f1e..8c2883425a0 100644 --- a/pkg/services/alerting/conditions/evaluator.go +++ b/pkg/services/alerting/conditions/evaluator.go @@ -34,8 +34,8 @@ type thresholdEvaluator struct { func newThresholdEvaluator(typ string, model *simplejson.Json) (*thresholdEvaluator, error) { params := model.Get("params").MustArray() - if len(params) == 0 { - return nil, fmt.Errorf("Evaluator missing threshold parameter") + if len(params) == 0 || params[0] == nil { + return nil, fmt.Errorf("Evaluator '%v' is missing the threshold parameter", HumanThresholdType(typ)) } firstParam, ok := params[0].(json.Number) @@ -139,3 +139,19 @@ func inSlice(a string, list []string) bool { } return false } + +// HumanThresholdType converts a treshold "type" string to a string that matches the UI +// so errors are less confusing. +func HumanThresholdType(typ string) string { + switch typ { + case "gt": + return "IS ABOVE" + case "lt": + return "IS BELOW" + case "within_range": + return "IS WITHIN RANGE" + case "outside_range": + return "IS OUTSIDE RANGE" + } + return "" +} diff --git a/pkg/services/alerting/conditions/query.go b/pkg/services/alerting/conditions/query.go index 8e1916f3c3f..cb9e015f615 100644 --- a/pkg/services/alerting/conditions/query.go +++ b/pkg/services/alerting/conditions/query.go @@ -234,7 +234,7 @@ func newQueryCondition(model *simplejson.Json, index int) (*QueryCondition, erro evaluatorJSON := model.Get("evaluator") evaluator, err := NewAlertEvaluator(evaluatorJSON) if err != nil { - return nil, err + return nil, fmt.Errorf("error in condition %v: %v", index, err) } condition.Evaluator = evaluator