2016-06-11 03:13:33 -05:00
|
|
|
package alerting
|
2016-04-18 07:15:03 -05:00
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
|
2016-06-11 03:13:33 -05:00
|
|
|
"github.com/grafana/grafana/pkg/bus"
|
2016-04-18 07:15:03 -05:00
|
|
|
"github.com/grafana/grafana/pkg/components/simplejson"
|
2016-05-25 04:14:59 -05:00
|
|
|
m "github.com/grafana/grafana/pkg/models"
|
2016-04-18 07:15:03 -05:00
|
|
|
. "github.com/smartystreets/goconvey/convey"
|
|
|
|
)
|
|
|
|
|
2016-06-11 03:13:33 -05:00
|
|
|
func TestAlertRuleExtraction(t *testing.T) {
|
2016-04-18 07:15:03 -05:00
|
|
|
|
2016-06-11 03:13:33 -05:00
|
|
|
Convey("Parsing alert rules from dashboard json", t, func() {
|
2016-06-10 03:00:00 -05:00
|
|
|
Convey("Parsing and validating alerts from dashboards", func() {
|
|
|
|
json := `{
|
2016-04-27 02:06:19 -05:00
|
|
|
"id": 57,
|
2016-04-18 07:15:03 -05:00
|
|
|
"title": "Graphite 4",
|
|
|
|
"originalTitle": "Graphite 4",
|
|
|
|
"tags": [
|
|
|
|
"graphite"
|
|
|
|
],
|
|
|
|
"rows": [
|
|
|
|
{
|
2016-06-10 03:00:00 -05:00
|
|
|
|
2016-04-18 07:15:03 -05:00
|
|
|
"panels": [
|
|
|
|
{
|
2016-04-27 02:06:19 -05:00
|
|
|
"title": "Active desktop users",
|
|
|
|
"editable": true,
|
|
|
|
"type": "graph",
|
|
|
|
"id": 3,
|
|
|
|
"targets": [
|
|
|
|
{
|
|
|
|
"refId": "A",
|
|
|
|
"target": "aliasByNode(statsd.fakesite.counters.session_start.desktop.count, 4)"
|
|
|
|
}
|
|
|
|
],
|
|
|
|
"datasource": null,
|
2016-06-11 03:54:24 -05:00
|
|
|
"alert": {
|
2016-06-11 03:13:33 -05:00
|
|
|
"name": "name1",
|
|
|
|
"description": "desc1",
|
2016-06-13 08:58:22 -05:00
|
|
|
"handler": 1,
|
2016-06-11 03:13:33 -05:00
|
|
|
"enabled": true,
|
2016-06-09 15:21:28 -05:00
|
|
|
"critical": {
|
2016-06-15 04:39:25 -05:00
|
|
|
"value": 20,
|
2016-06-10 06:41:01 -05:00
|
|
|
"op": ">"
|
2016-06-09 15:21:28 -05:00
|
|
|
},
|
2016-06-11 03:13:33 -05:00
|
|
|
"frequency": "60s",
|
2016-06-10 06:41:01 -05:00
|
|
|
"query": {
|
2016-06-09 15:21:28 -05:00
|
|
|
"from": "5m",
|
2016-06-10 06:41:01 -05:00
|
|
|
"refId": "A",
|
|
|
|
"to": "now"
|
2016-06-09 15:21:28 -05:00
|
|
|
},
|
2016-06-10 06:41:01 -05:00
|
|
|
"transform": {
|
|
|
|
"method": "avg",
|
2016-06-11 03:13:33 -05:00
|
|
|
"type": "aggregation"
|
2016-06-09 15:21:28 -05:00
|
|
|
},
|
2016-06-11 15:33:02 -05:00
|
|
|
"warn": {
|
2016-06-15 04:39:25 -05:00
|
|
|
"value": 10,
|
2016-06-10 06:41:01 -05:00
|
|
|
"op": ">"
|
2016-06-11 03:13:33 -05:00
|
|
|
}
|
2016-06-10 03:00:00 -05:00
|
|
|
}
|
2016-04-27 02:06:19 -05:00
|
|
|
},
|
|
|
|
{
|
|
|
|
"title": "Active mobile users",
|
|
|
|
"id": 4,
|
2016-04-18 07:15:03 -05:00
|
|
|
"targets": [
|
|
|
|
{
|
|
|
|
"refId": "A",
|
2016-04-27 02:06:19 -05:00
|
|
|
"target": "aliasByNode(statsd.fakesite.counters.session_start.mobile.count, 4)"
|
2016-04-18 07:15:03 -05:00
|
|
|
}
|
|
|
|
],
|
2016-04-27 02:06:19 -05:00
|
|
|
"datasource": "graphite2",
|
2016-06-11 03:54:24 -05:00
|
|
|
"alert": {
|
2016-06-11 03:13:33 -05:00
|
|
|
"name": "name2",
|
|
|
|
"description": "desc2",
|
2016-06-13 08:58:22 -05:00
|
|
|
"handler": 0,
|
2016-06-11 03:13:33 -05:00
|
|
|
"enabled": true,
|
2016-06-09 15:21:28 -05:00
|
|
|
"critical": {
|
2016-06-15 04:39:25 -05:00
|
|
|
"value": 20,
|
2016-06-10 06:41:01 -05:00
|
|
|
"op": ">"
|
2016-06-09 15:21:28 -05:00
|
|
|
},
|
2016-06-11 03:13:33 -05:00
|
|
|
"frequency": "60s",
|
2016-06-10 06:41:01 -05:00
|
|
|
"query": {
|
2016-06-09 15:21:28 -05:00
|
|
|
"from": "5m",
|
2016-06-10 06:41:01 -05:00
|
|
|
"refId": "A",
|
|
|
|
"to": "now"
|
2016-06-09 15:21:28 -05:00
|
|
|
},
|
2016-06-10 06:41:01 -05:00
|
|
|
"transform": {
|
|
|
|
"method": "avg",
|
|
|
|
"name": "aggregation"
|
2016-06-09 15:21:28 -05:00
|
|
|
},
|
2016-06-11 15:33:02 -05:00
|
|
|
"warn": {
|
2016-06-15 04:39:25 -05:00
|
|
|
"value": 10,
|
2016-06-10 06:41:01 -05:00
|
|
|
"op": ">"
|
|
|
|
}
|
2016-06-10 03:00:00 -05:00
|
|
|
}
|
2016-04-18 07:15:03 -05:00
|
|
|
}
|
|
|
|
],
|
|
|
|
"title": "Row"
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"collapse": false,
|
|
|
|
"editable": true,
|
|
|
|
"height": "250px",
|
|
|
|
"panels": [
|
|
|
|
{
|
|
|
|
"datasource": "InfluxDB",
|
|
|
|
"id": 2,
|
2016-06-15 04:39:25 -05:00
|
|
|
"alert": {
|
|
|
|
"name": "name2",
|
|
|
|
"description": "desc2",
|
|
|
|
"enabled": false,
|
|
|
|
"critical": {
|
|
|
|
"level": 20,
|
|
|
|
"op": ">"
|
|
|
|
},
|
|
|
|
"warn": {
|
|
|
|
"level": 10,
|
|
|
|
"op": ">"
|
|
|
|
}
|
|
|
|
},
|
2016-04-18 07:15:03 -05:00
|
|
|
"targets": [
|
|
|
|
{
|
|
|
|
"dsType": "influxdb",
|
|
|
|
"groupBy": [
|
|
|
|
{
|
|
|
|
"params": [
|
|
|
|
"$interval"
|
|
|
|
],
|
|
|
|
"type": "time"
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"params": [
|
|
|
|
"null"
|
|
|
|
],
|
|
|
|
"type": "fill"
|
|
|
|
}
|
|
|
|
],
|
|
|
|
"measurement": "cpu",
|
|
|
|
"policy": "default",
|
|
|
|
"query": "SELECT mean(\"value\") FROM \"cpu\" WHERE $timeFilter GROUP BY time($interval) fill(null)",
|
|
|
|
"refId": "A",
|
|
|
|
"resultFormat": "table",
|
|
|
|
"select": [
|
|
|
|
[
|
|
|
|
{
|
|
|
|
"params": [
|
|
|
|
"value"
|
|
|
|
],
|
|
|
|
"type": "field"
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"params": [],
|
|
|
|
"type": "mean"
|
|
|
|
}
|
|
|
|
]
|
|
|
|
],
|
|
|
|
"tags": [],
|
|
|
|
"target": ""
|
|
|
|
}
|
|
|
|
],
|
2016-04-27 02:06:19 -05:00
|
|
|
"title": "Broken influxdb panel",
|
2016-04-18 07:15:03 -05:00
|
|
|
"transform": "table",
|
2016-06-12 04:43:18 -05:00
|
|
|
"type": "table"
|
2016-04-18 07:15:03 -05:00
|
|
|
}
|
|
|
|
],
|
|
|
|
"title": "New row"
|
|
|
|
}
|
2016-06-10 03:00:00 -05:00
|
|
|
]
|
|
|
|
|
2016-04-18 07:15:03 -05:00
|
|
|
}`
|
2016-06-11 03:13:33 -05:00
|
|
|
dashJson, err := simplejson.NewJson([]byte(json))
|
|
|
|
So(err, ShouldBeNil)
|
|
|
|
|
|
|
|
dash := m.NewDashboardFromJson(dashJson)
|
2016-06-11 03:54:24 -05:00
|
|
|
extractor := NewDashAlertExtractor(dash, 1)
|
2016-06-11 03:13:33 -05:00
|
|
|
|
|
|
|
// mock data
|
|
|
|
defaultDs := &m.DataSource{Id: 12, OrgId: 2, Name: "I am default", IsDefault: true}
|
|
|
|
graphite2Ds := &m.DataSource{Id: 15, OrgId: 2, Name: "graphite2"}
|
|
|
|
|
|
|
|
bus.AddHandler("test", func(query *m.GetDataSourcesQuery) error {
|
|
|
|
query.Result = []*m.DataSource{defaultDs, graphite2Ds}
|
|
|
|
return nil
|
2016-06-10 03:00:00 -05:00
|
|
|
})
|
2016-05-25 04:14:59 -05:00
|
|
|
|
2016-06-11 03:13:33 -05:00
|
|
|
bus.AddHandler("test", func(query *m.GetDataSourceByNameQuery) error {
|
|
|
|
if query.Name == defaultDs.Name {
|
|
|
|
query.Result = defaultDs
|
|
|
|
}
|
|
|
|
if query.Name == graphite2Ds.Name {
|
|
|
|
query.Result = graphite2Ds
|
|
|
|
}
|
|
|
|
return nil
|
2016-06-10 03:00:00 -05:00
|
|
|
})
|
2016-04-18 07:15:03 -05:00
|
|
|
|
2016-06-12 04:43:18 -05:00
|
|
|
alerts, err := extractor.GetAlerts()
|
2016-06-11 03:13:33 -05:00
|
|
|
|
|
|
|
Convey("Get rules without error", func() {
|
|
|
|
So(err, ShouldBeNil)
|
|
|
|
})
|
2016-04-18 07:15:03 -05:00
|
|
|
|
2016-06-10 03:00:00 -05:00
|
|
|
Convey("all properties have been set", func() {
|
|
|
|
So(len(alerts), ShouldEqual, 2)
|
2016-04-18 07:15:03 -05:00
|
|
|
|
2016-06-10 03:00:00 -05:00
|
|
|
for _, v := range alerts {
|
2016-06-11 03:13:33 -05:00
|
|
|
So(v.DashboardId, ShouldEqual, 57)
|
2016-06-10 03:00:00 -05:00
|
|
|
So(v.Name, ShouldNotBeEmpty)
|
|
|
|
So(v.Description, ShouldNotBeEmpty)
|
|
|
|
}
|
2016-06-11 03:13:33 -05:00
|
|
|
|
2016-06-13 08:58:22 -05:00
|
|
|
Convey("should extract handler property", func() {
|
|
|
|
So(alerts[0].Handler, ShouldEqual, 1)
|
|
|
|
So(alerts[1].Handler, ShouldEqual, 0)
|
2016-06-11 03:13:33 -05:00
|
|
|
})
|
|
|
|
|
|
|
|
Convey("should extract panel idc", func() {
|
|
|
|
So(alerts[0].PanelId, ShouldEqual, 3)
|
|
|
|
So(alerts[1].PanelId, ShouldEqual, 4)
|
|
|
|
})
|
|
|
|
|
|
|
|
Convey("should extract name and desc", func() {
|
|
|
|
So(alerts[0].Name, ShouldEqual, "name1")
|
|
|
|
So(alerts[0].Description, ShouldEqual, "desc1")
|
|
|
|
So(alerts[1].Name, ShouldEqual, "name2")
|
|
|
|
So(alerts[1].Description, ShouldEqual, "desc2")
|
|
|
|
})
|
2016-06-10 03:00:00 -05:00
|
|
|
})
|
2016-04-18 07:15:03 -05:00
|
|
|
})
|
|
|
|
})
|
|
|
|
}
|