Commit 3219d98a by Torkel Ödegaard

feat(alerting): starting work on condition evaluation

parent f872d5cf
package alerting
import (
"testing"
"github.com/grafana/grafana/pkg/components/simplejson"
. "github.com/smartystreets/goconvey/convey"
)
func TestQueryCondition(t *testing.T) {
Convey("when evaluating query condition", t, func() {
Convey("Given avg() and > 100", func() {
jsonModel, err := simplejson.NewJson([]byte(`{
"type": "query",
"query": {
"params": ["A", "5m", "now"],
"datasourceId": 1,
"model": {"target": "aliasByNode(statsd.fakesite.counters.session_start.mobile.count, 4)"}
},
"reducer": {"type": "avg", "params": []},
"evaluator": {"type": ">", "params": [100]}
}`))
So(err, ShouldBeNil)
condition, err := NewQueryCondition(jsonModel)
So(err, ShouldBeNil)
Convey("Should set result to triggered when avg is above 100", func() {
context := &AlertResultContext{}
condition.Eval(context)
So(context.Triggered, ShouldBeTrue)
})
})
})
}
......@@ -14,7 +14,7 @@ type Engine struct {
clock clock.Clock
ticker *Ticker
scheduler Scheduler
handler AlertingHandler
handler AlertHandler
ruleReader RuleReader
log log.Logger
responseHandler ResultHandler
......
......@@ -90,6 +90,7 @@ func (e *DashAlertExtractor) GetAlerts() ([]*m.Alert, error) {
Handler: jsonAlert.Get("handler").MustInt64(),
Enabled: jsonAlert.Get("enabled").MustBool(),
Description: jsonAlert.Get("description").MustString(),
Severity: jsonAlert.Get("severity").MustString(),
Frequency: getTimeDurationStringToSeconds(jsonAlert.Get("frequency").MustString()),
}
......
......@@ -31,6 +31,16 @@ func (e *HandlerImpl) eval(rule *AlertRule) *AlertResultContext {
for _, condition := range rule.Conditions {
condition.Eval(result)
// break if condition could not be evaluated
if result.Error != nil {
break
}
// break if result has not triggered yet
if result.Triggered == false {
break
}
}
result.EndTime = time.Now()
......
......@@ -3,51 +3,42 @@ package alerting
import (
"testing"
"github.com/grafana/grafana/pkg/components/simplejson"
"github.com/grafana/grafana/pkg/models"
. "github.com/smartystreets/goconvey/convey"
)
type conditionStub struct {
triggered bool
}
func (c *conditionStub) Eval(context *AlertResultContext) {
context.Triggered = c.triggered
}
func TestAlertingExecutor(t *testing.T) {
Convey("Test alert execution", t, func() {
handler := NewHandler()
Convey("single time serie", func() {
Convey("Show return ok since avg is above 2", func() {
json := `
{
"name": "name2",
"description": "desc2",
"handler": 0,
"enabled": true,
"frequency": "60s",
"conditions": [
{
"type": "query",
"query": {
"params": ["A", "5m", "now"],
"datasourceId": 1,
"model": {"target": "aliasByNode(statsd.fakesite.counters.session_start.mobile.count, 4)"}
},
"reducer": {"type": "avg", "params": []},
"evaluator": {"type": ">", "params": [100]}
Convey("Show return triggered with single passing condition", func() {
rule := &AlertRule{
Conditions: []AlertCondition{&conditionStub{
triggered: true,
}},
}
]
}
`
alertJSON, jsonErr := simplejson.NewJson([]byte(json))
So(jsonErr, ShouldBeNil)
alert := &models.Alert{Settings: alertJSON}
rule, _ := NewAlertRuleFromDBModel(alert)
result := handler.eval(rule)
So(result.Triggered, ShouldEqual, true)
})
// timeSeries := []*tsdb.TimeSeries{
// tsdb.NewTimeSeries("test1", [][2]float64{{2, 0}}),
// }
Convey("Show return false with not passing condition", func() {
rule := &AlertRule{
Conditions: []AlertCondition{
&conditionStub{triggered: true},
&conditionStub{triggered: false},
},
}
result := handler.eval(rule)
So(result.Triggered, ShouldEqual, true)
So(result.Triggered, ShouldEqual, false)
})
// Convey("Show return critical since below 2", func() {
......@@ -168,6 +159,6 @@ func TestAlertingExecutor(t *testing.T) {
// result := executor.evaluateRule(rule, timeSeries)
// So(result.State, ShouldEqual, alertstates.Critical)
// })
})
// })
})
}
......@@ -2,7 +2,7 @@ package alerting
import "time"
type AlertingHandler interface {
type AlertHandler interface {
Execute(rule *AlertRule, resultChan chan *AlertResultContext)
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment