Skip to content

Commit

Permalink
Refactor alert constructor to include panelTitle argument
Browse files Browse the repository at this point in the history
The alert constructor was changed to include 'panelTitle' as a formal argument. Consequently, all the places where the alert constructor, New, was used have been updated with this new argument. Changes were also made to the 'PanelName' property extraction in the 'dashboards.go' file. Furthermore, the alert test was adjusted to reflect changes in the alert constructor.
  • Loading branch information
lueurxax committed Apr 30, 2024
1 parent 966d976 commit eda0f96
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 29 deletions.
2 changes: 1 addition & 1 deletion alert/alert.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,8 +88,8 @@ func New(name, panelName string, options ...Option) *Alert {
Labels: map[string]string{},
},
},
PanelName: panelName,
},
PanelName: panelName,
}

for _, opt := range append(defaults(), options...) {
Expand Down
30 changes: 16 additions & 14 deletions alert/alert_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,9 @@ import (
func TestNewAlertCanBeCreated(t *testing.T) {
req := require.New(t)
alertTitle := "some alert"
panelTitle := "some panel"

a := New(alertTitle)
a := New(alertTitle, panelTitle)

req.Len(a.Builder.Rules, 1)

Expand All @@ -25,6 +26,7 @@ func TestConditionsCanBeCombined(t *testing.T) {
req := require.New(t)

a := New(
"",
"",
IfOr(Avg, "A", IsBelow(10)),
IfOr(Avg, "B", IsBelow(8)),
Expand All @@ -36,7 +38,7 @@ func TestConditionsCanBeCombined(t *testing.T) {
func TestPanelIDCanBeHooked(t *testing.T) {
req := require.New(t)

a := New("")
a := New("", "")

a.HookPanelID("id")

Expand All @@ -46,7 +48,7 @@ func TestPanelIDCanBeHooked(t *testing.T) {
func TestDashboardUIDCanBeHooked(t *testing.T) {
req := require.New(t)

a := New("")
a := New("", "")

a.HookDashboardUID("uid")

Expand All @@ -57,7 +59,7 @@ func TestDatasourceUIDCanBeHooked(t *testing.T) {
req := require.New(t)

a := New(
"",
"", "",
WithPrometheusQuery("A", "some prometheus query"),
IfOr(Avg, "1", IsBelow(10)),
)
Expand Down Expand Up @@ -87,63 +89,63 @@ func TestDatasourceUIDCanBeHooked(t *testing.T) {
func TestSummaryCanBeSet(t *testing.T) {
req := require.New(t)

a := New("", Summary("summary content"))
a := New("", "", Summary("summary content"))

req.Equal("summary content", a.Builder.Rules[0].Annotations["summary"])
}

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

a := New("", Description("description content"))
a := New("", "", Description("description content"))

req.Equal("description content", a.Builder.Rules[0].Annotations["description"])
}

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

a := New("", Runbook("runbook url"))
a := New("", "", Runbook("runbook url"))

req.Equal("runbook url", a.Builder.Rules[0].Annotations["runbook_url"])
}

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

a := New("", For("1m"))
a := New("", "", For("1m"))

req.Equal("1m", a.Builder.Rules[0].For)
}

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

a := New("", EvaluateEvery("1m"))
a := New("", "", EvaluateEvery("1m"))

req.Equal("1m", a.Builder.Interval)
}

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

a := New("", OnExecutionError(ErrorKO))
a := New("", "", OnExecutionError(ErrorKO))

req.Equal(string(ErrorKO), a.Builder.Rules[0].GrafanaAlert.ExecutionErrorState)
}

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

a := New("", OnNoData(NoDataAlerting))
a := New("", "", OnNoData(NoDataAlerting))

req.Equal(string(NoDataAlerting), a.Builder.Rules[0].GrafanaAlert.NoDataState)
}

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

a := New("", Tags(map[string]string{
a := New("", "", Tags(map[string]string{
"severity": "warning",
}))

Expand All @@ -154,15 +156,15 @@ func TestTagsCanBeSet(t *testing.T) {
func TestConditionsCanBeSet(t *testing.T) {
req := require.New(t)

a := New("", If(Avg, "1", IsBelow(10)))
a := New("", "", If(Avg, "1", IsBelow(10)))

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

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

a := New("", IfOr(Avg, "1", IsBelow(10)))
a := New("", "", IfOr(Avg, "1", IsBelow(10)))

req.Len(a.Builder.Rules[0].GrafanaAlert.Data, 1)
}
10 changes: 5 additions & 5 deletions alert/queries_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,39 +10,39 @@ import (
func TestPrometheusQueriesCanBeAdded(t *testing.T) {
req := require.New(t)

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

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"))
a := New("", "", WithGraphiteQuery("A", "some graphite query"))

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"))
a := New("", "", WithLokiQuery("A", "some loki query"))

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")))
a := New("", "", WithStackdriverQuery(stackdriver.Gauge("A", "cloudsql.googleapis.com/database/cpu/utilization")))

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"))
a := New("", "", WithInfluxDBQuery("A", "some influxdb query"))

req.Len(a.Builder.Rules[0].GrafanaAlert.Data, 2)
}
2 changes: 1 addition & 1 deletion dashboards.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ func (client *Client) UpsertDashboard(ctx context.Context, folder *Folder, build
alert := *alerts[i]

alert.HookDashboardUID(dashboardFromGrafana.UID)
alert.HookPanelID(panelIDByTitle(dashboardFromGrafana, alert.Builder.PanelName))
alert.HookPanelID(panelIDByTitle(dashboardFromGrafana, alert.PanelName))

if err := client.AddAlert(ctx, folder.Title, alert, datasourcesMap); err != nil {
return nil, fmt.Errorf("could not add new alerts for dashboard: %w", err)
Expand Down
3 changes: 2 additions & 1 deletion dashboards_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -636,6 +636,7 @@ func TestDashboardsCanBeCreatedWithNewAlertsAndDeletesPreviousAlerts(t *testing.
),
timeseries.Alert(
"Too many heap allocations",
alert.Summary("Too many heap allocations"),
alert.WithPrometheusQuery(
"A",
"sum(go_memstats_heap_alloc_bytes{app!=\"\"}) by (app)",
Expand Down Expand Up @@ -849,7 +850,7 @@ func TestDashboardsCanBeCreatedWithNewAlertsAndDeletesPreviousAlerts(t *testing.
{
"for": "5m",
"grafana_alert": {
"title": "Heap allocations",
"title": "Too many heap allocations",
"condition": "_alert_condition_",
"no_data_state": "NoData",
"exec_err_state": "Alerting",
Expand Down
14 changes: 7 additions & 7 deletions decoder/alert_targets_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ func TestDecodingAPrometheusTarget(t *testing.T) {

req.NoError(err)

alert := alertBuilder.New("", opt)
alert := alertBuilder.New("", "", opt)

req.Len(alert.Builder.Rules, 1)
req.Len(alert.Builder.Rules[0].GrafanaAlert.Data, 2) // the query and the condition
Expand Down Expand Up @@ -101,7 +101,7 @@ func TestDecodingALokiTarget(t *testing.T) {

req.NoError(err)

alert := alertBuilder.New("", opt)
alert := alertBuilder.New("", "", opt)

req.Len(alert.Builder.Rules, 1)
req.Len(alert.Builder.Rules[0].GrafanaAlert.Data, 2) // the query and the condition
Expand Down Expand Up @@ -156,7 +156,7 @@ func TestDecodingAGraphiteTarget(t *testing.T) {

req.NoError(err)

alert := alertBuilder.New("", opt)
alert := alertBuilder.New("", "", opt)

req.Len(alert.Builder.Rules, 1)
req.Len(alert.Builder.Rules[0].GrafanaAlert.Data, 2) // the query and the condition
Expand Down Expand Up @@ -269,7 +269,7 @@ func TestDecodingStackdriverTarget(t *testing.T) {

req.NoError(err, ErrInvalidStackdriverType)

alert := alertBuilder.New("", opt)
alert := alertBuilder.New("", "", opt)

req.Len(alert.Builder.Rules, 1)
req.Len(alert.Builder.Rules[0].GrafanaAlert.Data, 2) // the query and the condition
Expand Down Expand Up @@ -335,7 +335,7 @@ func TestDecodingStackdriverPreprocessor(t *testing.T) {

req.NoError(err)

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

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

req.NoError(err)

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

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

req.NoError(err)

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

req.Equal(string(tc.expected), query.Model.MetricQuery.PerSeriesAligner)
Expand Down

0 comments on commit eda0f96

Please sign in to comment.