From eda0f96f9506e68d87abc1af982c1971ff75c894 Mon Sep 17 00:00:00 2001 From: lueurxax Date: Tue, 30 Apr 2024 18:13:55 +0200 Subject: [PATCH] Refactor alert constructor to include panelTitle argument 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. --- alert/alert.go | 2 +- alert/alert_test.go | 30 ++++++++++++++++-------------- alert/queries_test.go | 10 +++++----- dashboards.go | 2 +- dashboards_test.go | 3 ++- decoder/alert_targets_test.go | 14 +++++++------- 6 files changed, 32 insertions(+), 29 deletions(-) diff --git a/alert/alert.go b/alert/alert.go index ef2d090d..93797088 100644 --- a/alert/alert.go +++ b/alert/alert.go @@ -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...) { diff --git a/alert/alert_test.go b/alert/alert_test.go index fd40b715..343f0ee2 100644 --- a/alert/alert_test.go +++ b/alert/alert_test.go @@ -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) @@ -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)), @@ -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") @@ -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") @@ -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)), ) @@ -87,7 +89,7 @@ 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"]) } @@ -95,7 +97,7 @@ func TestSummaryCanBeSet(t *testing.T) { 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"]) } @@ -103,7 +105,7 @@ func TestDescriptionCanBeSet(t *testing.T) { 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"]) } @@ -111,7 +113,7 @@ func TestRunbookCanBeSet(t *testing.T) { 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) } @@ -119,7 +121,7 @@ func TestForIntervalCanBeSet(t *testing.T) { func TestFrequencyCanBeSet(t *testing.T) { req := require.New(t) - a := New("", EvaluateEvery("1m")) + a := New("", "", EvaluateEvery("1m")) req.Equal("1m", a.Builder.Interval) } @@ -127,7 +129,7 @@ func TestFrequencyCanBeSet(t *testing.T) { 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) } @@ -135,7 +137,7 @@ func TestErrorModeCanBeSet(t *testing.T) { 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) } @@ -143,7 +145,7 @@ func TestNoDataModeCanBeSet(t *testing.T) { func TestTagsCanBeSet(t *testing.T) { req := require.New(t) - a := New("", Tags(map[string]string{ + a := New("", "", Tags(map[string]string{ "severity": "warning", })) @@ -154,7 +156,7 @@ 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) } @@ -162,7 +164,7 @@ func TestConditionsCanBeSet(t *testing.T) { 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) } diff --git a/alert/queries_test.go b/alert/queries_test.go index fb64083f..c893c3b1 100644 --- a/alert/queries_test.go +++ b/alert/queries_test.go @@ -10,7 +10,7 @@ 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) } @@ -18,7 +18,7 @@ func TestPrometheusQueriesCanBeAdded(t *testing.T) { 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) } @@ -26,7 +26,7 @@ func TestGraphiteQueriesCanBeAdded(t *testing.T) { 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) } @@ -34,7 +34,7 @@ func TestLokiQueriesCanBeAdded(t *testing.T) { 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) } @@ -42,7 +42,7 @@ func TestStackdriverQueriesCanBeAdded(t *testing.T) { 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) } diff --git a/dashboards.go b/dashboards.go index 78659373..5ada40b3 100644 --- a/dashboards.go +++ b/dashboards.go @@ -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) diff --git a/dashboards_test.go b/dashboards_test.go index ded065c9..cb10f1ff 100644 --- a/dashboards_test.go +++ b/dashboards_test.go @@ -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)", @@ -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", diff --git a/decoder/alert_targets_test.go b/decoder/alert_targets_test.go index 0cb86f6b..8bbceb8b 100644 --- a/decoder/alert_targets_test.go +++ b/decoder/alert_targets_test.go @@ -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 @@ -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 @@ -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 @@ -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 @@ -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) @@ -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) @@ -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)