mirror of
https://github.com/grafana/grafana.git
synced 2025-02-25 18:55:37 -06:00
feat(alerting): progress on updating extractor to work with new alert model
This commit is contained in:
parent
f60efed5d8
commit
2a30baef05
@ -45,7 +45,7 @@ func TestAlertRuleModel(t *testing.T) {
|
|||||||
"query": {
|
"query": {
|
||||||
"params": ["A", "5m", "now"],
|
"params": ["A", "5m", "now"],
|
||||||
"datasourceId": 1,
|
"datasourceId": 1,
|
||||||
"query": "aliasByNode(statsd.fakesite.counters.session_start.mobile.count, 4)"
|
"model": {"target": "aliasByNode(statsd.fakesite.counters.session_start.mobile.count, 4)"}
|
||||||
},
|
},
|
||||||
"reducer": {"type": "avg", "params": []},
|
"reducer": {"type": "avg", "params": []},
|
||||||
"evaluator": {"type": ">", "params": [100]}
|
"evaluator": {"type": ">", "params": [100]}
|
||||||
|
@ -2,6 +2,7 @@ package alerting
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"errors"
|
"errors"
|
||||||
|
"fmt"
|
||||||
|
|
||||||
"github.com/grafana/grafana/pkg/bus"
|
"github.com/grafana/grafana/pkg/bus"
|
||||||
"github.com/grafana/grafana/pkg/components/simplejson"
|
"github.com/grafana/grafana/pkg/components/simplejson"
|
||||||
@ -47,6 +48,17 @@ func (e *DashAlertExtractor) lookupDatasourceId(dsName string) (*m.DataSource, e
|
|||||||
return nil, errors.New("Could not find datasource id for " + dsName)
|
return nil, errors.New("Could not find datasource id for " + dsName)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func findPanelQueryByRefId(panel *simplejson.Json, refId string) *simplejson.Json {
|
||||||
|
for _, targetsObj := range panel.Get("targets").MustArray() {
|
||||||
|
target := simplejson.NewFromAny(targetsObj)
|
||||||
|
|
||||||
|
if target.Get("refId").MustString() == refId {
|
||||||
|
return target
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
func (e *DashAlertExtractor) GetAlerts() ([]*m.Alert, error) {
|
func (e *DashAlertExtractor) GetAlerts() ([]*m.Alert, error) {
|
||||||
e.log.Debug("GetAlerts")
|
e.log.Debug("GetAlerts")
|
||||||
|
|
||||||
@ -81,15 +93,20 @@ func (e *DashAlertExtractor) GetAlerts() ([]*m.Alert, error) {
|
|||||||
Frequency: getTimeDurationStringToSeconds(jsonAlert.Get("frequency").MustString()),
|
Frequency: getTimeDurationStringToSeconds(jsonAlert.Get("frequency").MustString()),
|
||||||
}
|
}
|
||||||
|
|
||||||
valueQuery := jsonAlert.Get("query")
|
for _, condition := range jsonAlert.Get("conditions").MustArray() {
|
||||||
valueQueryRef := valueQuery.Get("refId").MustString()
|
jsonCondition := simplejson.NewFromAny(condition)
|
||||||
for _, targetsObj := range panel.Get("targets").MustArray() {
|
|
||||||
target := simplejson.NewFromAny(targetsObj)
|
jsonQuery := jsonCondition.Get("query")
|
||||||
|
queryRefId := jsonQuery.Get("params").MustArray()[0].(string)
|
||||||
|
panelQuery := findPanelQueryByRefId(panel, queryRefId)
|
||||||
|
|
||||||
|
if panelQuery == nil {
|
||||||
|
return nil, fmt.Errorf("Alert referes to query %s, that could not be found", queryRefId)
|
||||||
|
}
|
||||||
|
|
||||||
if target.Get("refId").MustString() == valueQueryRef {
|
|
||||||
dsName := ""
|
dsName := ""
|
||||||
if target.Get("datasource").MustString() != "" {
|
if panelQuery.Get("datasource").MustString() != "" {
|
||||||
dsName = target.Get("datasource").MustString()
|
dsName = panelQuery.Get("datasource").MustString()
|
||||||
} else if panel.Get("datasource").MustString() != "" {
|
} else if panel.Get("datasource").MustString() != "" {
|
||||||
dsName = panel.Get("datasource").MustString()
|
dsName = panel.Get("datasource").MustString()
|
||||||
}
|
}
|
||||||
@ -97,15 +114,10 @@ func (e *DashAlertExtractor) GetAlerts() ([]*m.Alert, error) {
|
|||||||
if datasource, err := e.lookupDatasourceId(dsName); err != nil {
|
if datasource, err := e.lookupDatasourceId(dsName); err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
} else {
|
} else {
|
||||||
valueQuery.SetPath([]string{"datasourceId"}, datasource.Id)
|
jsonQuery.SetPath([]string{"datasourceId"}, datasource.Id)
|
||||||
valueQuery.SetPath([]string{"datasourceType"}, datasource.Type)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
targetQuery := target.Get("target").MustString()
|
jsonQuery.Set("model", panelQuery.Interface())
|
||||||
if targetQuery != "" {
|
|
||||||
jsonAlert.SetPath([]string{"query", "query"}, targetQuery)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
alert.Settings = jsonAlert
|
alert.Settings = jsonAlert
|
||||||
@ -118,7 +130,6 @@ func (e *DashAlertExtractor) GetAlerts() ([]*m.Alert, error) {
|
|||||||
e.log.Error("Failed to extract alerts from dashboard", "error", err)
|
e.log.Error("Failed to extract alerts from dashboard", "error", err)
|
||||||
return nil, errors.New("Failed to extract alerts from dashboard")
|
return nil, errors.New("Failed to extract alerts from dashboard")
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -17,12 +17,9 @@ func TestAlertRuleExtraction(t *testing.T) {
|
|||||||
"id": 57,
|
"id": 57,
|
||||||
"title": "Graphite 4",
|
"title": "Graphite 4",
|
||||||
"originalTitle": "Graphite 4",
|
"originalTitle": "Graphite 4",
|
||||||
"tags": [
|
"tags": ["graphite"],
|
||||||
"graphite"
|
|
||||||
],
|
|
||||||
"rows": [
|
"rows": [
|
||||||
{
|
{
|
||||||
|
|
||||||
"panels": [
|
"panels": [
|
||||||
{
|
{
|
||||||
"title": "Active desktop users",
|
"title": "Active desktop users",
|
||||||
@ -41,34 +38,23 @@ func TestAlertRuleExtraction(t *testing.T) {
|
|||||||
"description": "desc1",
|
"description": "desc1",
|
||||||
"handler": 1,
|
"handler": 1,
|
||||||
"enabled": true,
|
"enabled": true,
|
||||||
"critical": {
|
|
||||||
"value": 20,
|
|
||||||
"op": ">"
|
|
||||||
},
|
|
||||||
"frequency": "60s",
|
"frequency": "60s",
|
||||||
"query": {
|
"conditions": [
|
||||||
"from": "5m",
|
{
|
||||||
"refId": "A",
|
"type": "query",
|
||||||
"to": "now"
|
"query": {"params": ["A", "5m", "now"]},
|
||||||
},
|
"reducer": {"type": "avg", "params": []},
|
||||||
"transform": {
|
"evaluator": {"type": ">", "params": [100]}
|
||||||
"type": "avg",
|
|
||||||
"name": "aggregation"
|
|
||||||
},
|
|
||||||
"warn": {
|
|
||||||
"value": 10,
|
|
||||||
"op": ">"
|
|
||||||
}
|
}
|
||||||
|
]
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"title": "Active mobile users",
|
"title": "Active mobile users",
|
||||||
"id": 4,
|
"id": 4,
|
||||||
"targets": [
|
"targets": [
|
||||||
{
|
{"refId": "A", "target": ""},
|
||||||
"refId": "A",
|
{"refId": "B", "target": "aliasByNode(statsd.fakesite.counters.session_start.mobile.count, 4)"}
|
||||||
"target": "aliasByNode(statsd.fakesite.counters.session_start.mobile.count, 4)"
|
|
||||||
}
|
|
||||||
],
|
],
|
||||||
"datasource": "graphite2",
|
"datasource": "graphite2",
|
||||||
"alert": {
|
"alert": {
|
||||||
@ -76,99 +62,20 @@ func TestAlertRuleExtraction(t *testing.T) {
|
|||||||
"description": "desc2",
|
"description": "desc2",
|
||||||
"handler": 0,
|
"handler": 0,
|
||||||
"enabled": true,
|
"enabled": true,
|
||||||
"critical": {
|
|
||||||
"value": 20,
|
|
||||||
"op": ">"
|
|
||||||
},
|
|
||||||
"frequency": "60s",
|
"frequency": "60s",
|
||||||
"query": {
|
"conditions": [
|
||||||
"from": "5m",
|
|
||||||
"refId": "A",
|
|
||||||
"to": "now"
|
|
||||||
},
|
|
||||||
"transform": {
|
|
||||||
"type": "avg",
|
|
||||||
"name": "aggregation"
|
|
||||||
},
|
|
||||||
"warn": {
|
|
||||||
"value": 10,
|
|
||||||
"op": ">"
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
],
|
|
||||||
"title": "Row"
|
|
||||||
},
|
|
||||||
{
|
{
|
||||||
"collapse": false,
|
"type": "query",
|
||||||
"editable": true,
|
"query": {"params": ["B", "5m", "now"]},
|
||||||
"height": "250px",
|
"reducer": {"type": "avg", "params": []},
|
||||||
"panels": [
|
"evaluator": {"type": ">", "params": [100]}
|
||||||
{
|
|
||||||
"datasource": "InfluxDB",
|
|
||||||
"id": 2,
|
|
||||||
"alert": {
|
|
||||||
"name": "name2",
|
|
||||||
"description": "desc2",
|
|
||||||
"enabled": false,
|
|
||||||
"critical": {
|
|
||||||
"level": 20,
|
|
||||||
"op": ">"
|
|
||||||
},
|
|
||||||
"warn": {
|
|
||||||
"level": 10,
|
|
||||||
"op": ">"
|
|
||||||
}
|
|
||||||
},
|
|
||||||
"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": ""
|
|
||||||
}
|
}
|
||||||
],
|
|
||||||
"title": "Broken influxdb panel",
|
|
||||||
"transform": "table",
|
|
||||||
"type": "table"
|
|
||||||
}
|
|
||||||
],
|
|
||||||
"title": "New row"
|
|
||||||
}
|
}
|
||||||
]
|
]
|
||||||
|
}
|
||||||
|
]
|
||||||
}`
|
}`
|
||||||
dashJson, err := simplejson.NewJson([]byte(json))
|
dashJson, err := simplejson.NewJson([]byte(json))
|
||||||
So(err, ShouldBeNil)
|
So(err, ShouldBeNil)
|
||||||
@ -231,6 +138,18 @@ func TestAlertRuleExtraction(t *testing.T) {
|
|||||||
So(alerts[1].Name, ShouldEqual, "name2")
|
So(alerts[1].Name, ShouldEqual, "name2")
|
||||||
So(alerts[1].Description, ShouldEqual, "desc2")
|
So(alerts[1].Description, ShouldEqual, "desc2")
|
||||||
})
|
})
|
||||||
|
|
||||||
|
Convey("should set datasourceId", func() {
|
||||||
|
condition := simplejson.NewFromAny(alerts[0].Settings.Get("conditions").MustArray()[0])
|
||||||
|
query := condition.Get("query")
|
||||||
|
So(query.Get("datasourceId").MustInt64(), ShouldEqual, 12)
|
||||||
|
})
|
||||||
|
|
||||||
|
Convey("should copy query model to condition", func() {
|
||||||
|
condition := simplejson.NewFromAny(alerts[0].Settings.Get("conditions").MustArray()[0])
|
||||||
|
model := condition.Get("query").Get("model")
|
||||||
|
So(model.Get("target").MustString(), ShouldEqual, "aliasByNode(statsd.fakesite.counters.session_start.desktop.count, 4)")
|
||||||
|
})
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
Loading…
Reference in New Issue
Block a user