Skip to content

Commit

Permalink
alerting: supports extracting alerts from collapsed panels
Browse files Browse the repository at this point in the history
collapsed rows wrap the hidden rows within itself. This caused
the extractor to miss the panel and therefore delete the alert
accosiated with the graph.

closes grafana#11222
  • Loading branch information
bergquist committed Mar 14, 2018
1 parent 9cae6f0 commit be7ec31
Show file tree
Hide file tree
Showing 3 changed files with 638 additions and 0 deletions.
15 changes: 15 additions & 0 deletions pkg/services/alerting/extractor.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,21 @@ func (e *DashAlertExtractor) GetAlertFromPanels(jsonWithPanels *simplejson.Json)

for _, panelObj := range jsonWithPanels.Get("panels").MustArray() {
panel := simplejson.NewFromAny(panelObj)

collapsedJson, collapsed := panel.CheckGet("collapsed")
// check if the panel is collapsed
if collapsed && collapsedJson.MustBool() {

// extract alerts from sub panels for collapsed panels
als, err := e.GetAlertFromPanels(panel)
if err != nil {
return nil, err
}

alerts = append(alerts, als...)
continue
}

jsonAlert, hasAlert := panel.CheckGet("alert")

if !hasAlert {
Expand Down
26 changes: 26 additions & 0 deletions pkg/services/alerting/extractor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ func TestAlertRuleExtraction(t *testing.T) {
defaultDs := &m.DataSource{Id: 12, OrgId: 1, Name: "I am default", IsDefault: true}
graphite2Ds := &m.DataSource{Id: 15, OrgId: 1, Name: "graphite2"}
influxDBDs := &m.DataSource{Id: 16, OrgId: 1, Name: "InfluxDB"}
prom := &m.DataSource{Id: 17, OrgId: 1, Name: "Prometheus"}

bus.AddHandler("test", func(query *m.GetDataSourcesQuery) error {
query.Result = []*m.DataSource{defaultDs, graphite2Ds}
Expand All @@ -38,6 +39,10 @@ func TestAlertRuleExtraction(t *testing.T) {
if query.Name == influxDBDs.Name {
query.Result = influxDBDs
}
if query.Name == prom.Name {
query.Result = prom
}

return nil
})

Expand Down Expand Up @@ -214,5 +219,26 @@ func TestAlertRuleExtraction(t *testing.T) {
}
})
})

Convey("Should be able to extract collapsed panels", func() {
json, err := ioutil.ReadFile("./test-data/collapsed-panels.json")
So(err, ShouldBeNil)

dashJson, err := simplejson.NewJson(json)
So(err, ShouldBeNil)

dash := m.NewDashboardFromJson(dashJson)
extractor := NewDashAlertExtractor(dash, 1)

alerts, err := extractor.GetAlerts()

Convey("Get rules without error", func() {
So(err, ShouldBeNil)
})

Convey("should be able to extract collapsed alerts", func() {
So(len(alerts), ShouldEqual, 4)
})
})
})
}
Loading

0 comments on commit be7ec31

Please sign in to comment.