Skip to content

Commit

Permalink
Merge pull request #183 from K-Phoen/fix-alert-conditions-combining
Browse files Browse the repository at this point in the history
fix: allow alerts to have several trigger conditions
  • Loading branch information
K-Phoen authored Jun 17, 2022
2 parents 74810a5 + 13b2d7d commit c8391d6
Show file tree
Hide file tree
Showing 5 changed files with 68 additions and 56 deletions.
40 changes: 20 additions & 20 deletions alert/alert.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,8 @@ type Alert struct {

// New creates a new alert.
func New(name string, options ...Option) *Alert {
nope := false

alert := &Alert{
Builder: &sdk.Alert{
Name: name,
Expand All @@ -63,7 +65,23 @@ func New(name string, options ...Option) *Alert {
GrafanaAlert: &sdk.GrafanaAlert{
Title: name,
Condition: alertConditionRef,
Data: nil,
Data: []sdk.AlertQuery{
{
RefID: alertConditionRef,
QueryType: "",
DatasourceUID: "-100",
Model: sdk.AlertModel{
RefID: alertConditionRef,
Type: "classic_conditions",
Hide: &nope,
Datasource: sdk.AlertDatasourceRef{
UID: "-100",
Type: "__expr__",
},
Conditions: []sdk.AlertCondition{},
},
},
},
},
Annotations: map[string]string{},
Labels: map[string]string{},
Expand Down Expand Up @@ -185,25 +203,7 @@ func ifOperand(operand Operator, reducer QueryReducer, queryRef string, evaluato
cond := newCondition(reducer, queryRef, evaluator)
cond.builder.Operator = sdk.AlertOperator{Type: string(operand)}

nope := false

alertQuery := sdk.AlertQuery{
RefID: alertConditionRef,
QueryType: "",
DatasourceUID: "-100",
Model: sdk.AlertModel{
RefID: alertConditionRef,
Type: "classic_conditions",
Hide: &nope,
Datasource: sdk.AlertDatasourceRef{
UID: "-100",
Type: "__expr__",
},
Conditions: []sdk.AlertCondition{*cond.builder},
},
}

alert.Builder.Rules[0].GrafanaAlert.Data = append(alert.Builder.Rules[0].GrafanaAlert.Data, alertQuery)
alert.Builder.Rules[0].GrafanaAlert.Data[0].Model.Conditions = append(alert.Builder.Rules[0].GrafanaAlert.Data[0].Model.Conditions, *cond.builder)
}
}

Expand Down
12 changes: 12 additions & 0 deletions alert/alert_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,18 @@ func TestNewAlertCanBeCreated(t *testing.T) {
req.Equal(string(ErrorAlerting), a.Builder.Rules[0].GrafanaAlert.ExecutionErrorState)
}

func TestConditionsCanBeCombined(t *testing.T) {
req := require.New(t)

a := New(
"",
IfOr(Avg, "A", IsBelow(10)),
IfOr(Avg, "B", IsBelow(8)),
)

req.Len(a.Builder.Rules[0].GrafanaAlert.Data, 1)
}

func TestPanelIDCanBeHooked(t *testing.T) {
req := require.New(t)

Expand Down
10 changes: 5 additions & 5 deletions alert/queries_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,37 +13,37 @@ func TestPrometheusQueriesCanBeAdded(t *testing.T) {

a := New("", WithPrometheusQuery("A", "some prometheus query"))

req.Len(a.Builder.Rules[0].GrafanaAlert.Data, 1)
req.Len(a.Builder.Rules[0].GrafanaAlert.Data, 2)
}

func TestGraphiteQueriesCanBeAdded(t *testing.T) {
req := require.New(t)

a := New("", WithGraphiteQuery("A", "some graphite query"))

req.Len(a.Builder.Rules[0].GrafanaAlert.Data, 1)
req.Len(a.Builder.Rules[0].GrafanaAlert.Data, 2)
}

func TestLokiQueriesCanBeAdded(t *testing.T) {
req := require.New(t)

a := New("", WithLokiQuery("A", "some loki query"))

req.Len(a.Builder.Rules[0].GrafanaAlert.Data, 1)
req.Len(a.Builder.Rules[0].GrafanaAlert.Data, 2)
}

func TestStackdriverQueriesCanBeAdded(t *testing.T) {
req := require.New(t)

a := New("", WithStackdriverQuery(stackdriver.Gauge("A", "cloudsql.googleapis.com/database/cpu/utilization")))

req.Len(a.Builder.Rules[0].GrafanaAlert.Data, 1)
req.Len(a.Builder.Rules[0].GrafanaAlert.Data, 2)
}

func TestInfluxDBQueriesCanBeAdded(t *testing.T) {
req := require.New(t)

a := New("", WithInfluxDBQuery("A", "some influxdb query"))

req.Len(a.Builder.Rules[0].GrafanaAlert.Data, 1)
req.Len(a.Builder.Rules[0].GrafanaAlert.Data, 2)
}
40 changes: 20 additions & 20 deletions dashboards_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -778,26 +778,6 @@ func TestDashboardsCanBeCreatedWithNewAlertsAndDeletesPreviousAlerts(t *testing.
"no_data_state": "NoData",
"exec_err_state": "Alerting",
"data": [
{
"refId": "A",
"queryType": "",
"relativeTimeRange": {
"from": 600,
"to": 0
},
"datasourceUid": "prom-uid",
"model": {
"refId": "A",
"expr": "sum(go_memstats_heap_alloc_bytes{app!=\"\"}) by (app)",
"format": "time_series",
"datasource": {
"uid": "prom-uid",
"type": "prometheus"
},
"intervalMs": 15000,
"hide": false
}
},
{
"refId": "_alert_condition_",
"queryType": "",
Expand Down Expand Up @@ -833,6 +813,26 @@ func TestDashboardsCanBeCreatedWithNewAlertsAndDeletesPreviousAlerts(t *testing.
}
]
}
},
{
"refId": "A",
"queryType": "",
"relativeTimeRange": {
"from": 600,
"to": 0
},
"datasourceUid": "prom-uid",
"model": {
"refId": "A",
"expr": "sum(go_memstats_heap_alloc_bytes{app!=\"\"}) by (app)",
"format": "time_series",
"datasource": {
"uid": "prom-uid",
"type": "prometheus"
},
"intervalMs": 15000,
"hide": false
}
}
]
},
Expand Down
22 changes: 11 additions & 11 deletions decoder/alert_targets_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,9 @@ func TestDecodingAPrometheusTarget(t *testing.T) {
alert := alertBuilder.New("", opt)

req.Len(alert.Builder.Rules, 1)
req.Len(alert.Builder.Rules[0].GrafanaAlert.Data, 1)
req.Len(alert.Builder.Rules[0].GrafanaAlert.Data, 2) // the query and the condition

promQuery := alert.Builder.Rules[0].GrafanaAlert.Data[0]
promQuery := alert.Builder.Rules[0].GrafanaAlert.Data[1]

req.Equal("A", promQuery.RefID)
req.Equal("A", promQuery.Model.RefID)
Expand Down Expand Up @@ -104,9 +104,9 @@ func TestDecodingALokiTarget(t *testing.T) {
alert := alertBuilder.New("", opt)

req.Len(alert.Builder.Rules, 1)
req.Len(alert.Builder.Rules[0].GrafanaAlert.Data, 1)
req.Len(alert.Builder.Rules[0].GrafanaAlert.Data, 2) // the query and the condition

promQuery := alert.Builder.Rules[0].GrafanaAlert.Data[0]
promQuery := alert.Builder.Rules[0].GrafanaAlert.Data[1]

req.Equal("A", promQuery.RefID)
req.Equal("A", promQuery.Model.RefID)
Expand Down Expand Up @@ -159,9 +159,9 @@ func TestDecodingAGraphiteTarget(t *testing.T) {
alert := alertBuilder.New("", opt)

req.Len(alert.Builder.Rules, 1)
req.Len(alert.Builder.Rules[0].GrafanaAlert.Data, 1)
req.Len(alert.Builder.Rules[0].GrafanaAlert.Data, 2) // the query and the condition

promQuery := alert.Builder.Rules[0].GrafanaAlert.Data[0]
promQuery := alert.Builder.Rules[0].GrafanaAlert.Data[1]

req.Equal("A", promQuery.RefID)
req.Equal("A", promQuery.Model.RefID)
Expand Down Expand Up @@ -271,9 +271,9 @@ func TestDecodingStackdriverTarget(t *testing.T) {
alert := alertBuilder.New("", opt)

req.Len(alert.Builder.Rules, 1)
req.Len(alert.Builder.Rules[0].GrafanaAlert.Data, 1)
req.Len(alert.Builder.Rules[0].GrafanaAlert.Data, 2) // the query and the condition

query := alert.Builder.Rules[0].GrafanaAlert.Data[0]
query := alert.Builder.Rules[0].GrafanaAlert.Data[1]
stackdriverData := query.Model.MetricQuery

req.Equal("A", query.RefID)
Expand Down Expand Up @@ -334,7 +334,7 @@ func TestDecodingStackdriverPreprocessor(t *testing.T) {
req.NoError(err)

alert := alertBuilder.New("", opt)
query := alert.Builder.Rules[0].GrafanaAlert.Data[0]
query := alert.Builder.Rules[0].GrafanaAlert.Data[1]

req.Equal(tc.expected, query.Model.MetricQuery.Preprocessor)
})
Expand Down Expand Up @@ -448,7 +448,7 @@ func TestDecodingStackdriverAggregation(t *testing.T) {
req.NoError(err)

alert := alertBuilder.New("", opt)
query := alert.Builder.Rules[0].GrafanaAlert.Data[0]
query := alert.Builder.Rules[0].GrafanaAlert.Data[1]

req.Equal(string(tc.expected), query.Model.MetricQuery.CrossSeriesReducer)
})
Expand Down Expand Up @@ -590,7 +590,7 @@ func TestDecodingStackdriverAlignment(t *testing.T) {
req.NoError(err)

alert := alertBuilder.New("", opt)
query := alert.Builder.Rules[0].GrafanaAlert.Data[0]
query := alert.Builder.Rules[0].GrafanaAlert.Data[1]

req.Equal(string(tc.expected), query.Model.MetricQuery.PerSeriesAligner)
req.Equal("stackdriver-auto", query.Model.MetricQuery.AlignmentPeriod)
Expand Down

0 comments on commit c8391d6

Please sign in to comment.