From 51499d7763ad77b57b4c34641f9698586efafbee Mon Sep 17 00:00:00 2001 From: Yuri Tseretyan Date: Thu, 5 Oct 2023 15:16:44 -0400 Subject: [PATCH] Alerting: Update alert rule export models to omit default values (#75918) * do not include rule uid in response if it's empty * make some fields of export models nillable --- .../ngalert/api/api_provisioning_test.go | 8 +--- pkg/services/ngalert/api/compat.go | 38 ++++++++++--------- .../test-data/post-rulegroup-101-export.hcl | 21 +++------- .../test-data/post-rulegroup-101-export.json | 2 - .../test-data/post-rulegroup-101-export.yaml | 6 +-- .../definitions/provisioning_alert_rules.go | 14 +++---- 6 files changed, 36 insertions(+), 53 deletions(-) diff --git a/pkg/services/ngalert/api/api_provisioning_test.go b/pkg/services/ngalert/api/api_provisioning_test.go index af8c1ddee77..5128072c039 100644 --- a/pkg/services/ngalert/api/api_provisioning_test.go +++ b/pkg/services/ngalert/api/api_provisioning_test.go @@ -592,8 +592,7 @@ func TestProvisioningApi(t *testing.T) { condition = "A" data { - ref_id = "A" - query_type = "" + ref_id = "A" relative_time_range { from = 0 @@ -620,8 +619,7 @@ func TestProvisioningApi(t *testing.T) { condition = "A" data { - ref_id = "A" - query_type = "" + ref_id = "A" relative_time_range { from = 0 @@ -635,8 +633,6 @@ func TestProvisioningApi(t *testing.T) { no_data_state = "OK" exec_err_state = "OK" for = 0 - annotations = null - labels = null is_paused = false } } diff --git a/pkg/services/ngalert/api/compat.go b/pkg/services/ngalert/api/compat.go index f1938cba94c..fc5a01ba083 100644 --- a/pkg/services/ngalert/api/compat.go +++ b/pkg/services/ngalert/api/compat.go @@ -8,6 +8,7 @@ import ( "github.com/grafana/grafana/pkg/services/ngalert/api/tooling/definitions" "github.com/grafana/grafana/pkg/services/ngalert/models" + "github.com/grafana/grafana/pkg/util" ) // AlertRuleFromProvisionedAlertRule converts definitions.ProvisionedAlertRule to models.AlertRule @@ -172,31 +173,28 @@ func AlertRuleExportFromAlertRule(rule models.AlertRule) (definitions.AlertRuleE data = append(data, query) } - var dashboardUID string - if rule.DashboardUID != nil { - dashboardUID = *rule.DashboardUID - } - - var panelID int64 - if rule.PanelID != nil { - panelID = *rule.PanelID - } - - return definitions.AlertRuleExport{ + result := definitions.AlertRuleExport{ UID: rule.UID, Title: rule.Title, For: model.Duration(rule.For), - ForSeconds: int64(rule.For.Seconds()), Condition: rule.Condition, Data: data, - DashboardUID: dashboardUID, - PanelID: panelID, + DashboardUID: rule.DashboardUID, + PanelID: rule.PanelID, NoDataState: definitions.NoDataState(rule.NoDataState), ExecErrState: definitions.ExecutionErrorState(rule.ExecErrState), - Annotations: rule.Annotations, - Labels: rule.Labels, IsPaused: rule.IsPaused, - }, nil + } + if rule.For.Seconds() > 0 { + result.ForSeconds = util.Pointer(int64(rule.For.Seconds())) + } + if rule.Annotations != nil { + result.Annotations = &rule.Annotations + } + if rule.Labels != nil { + result.Labels = &rule.Labels + } + return result, nil } // AlertQueryExportFromAlertQuery creates a definitions.AlertQueryExport DTO from models.AlertQuery. @@ -207,9 +205,13 @@ func AlertQueryExportFromAlertQuery(query models.AlertQuery) (definitions.AlertQ if err != nil { return definitions.AlertQueryExport{}, err } + var queryType *string + if query.QueryType != "" { + queryType = &query.QueryType + } return definitions.AlertQueryExport{ RefID: query.RefID, - QueryType: query.QueryType, + QueryType: queryType, RelativeTimeRange: definitions.RelativeTimeRangeExport{ FromSeconds: int64(time.Duration(query.RelativeTimeRange.From).Seconds()), ToSeconds: int64(time.Duration(query.RelativeTimeRange.To).Seconds()), diff --git a/pkg/services/ngalert/api/test-data/post-rulegroup-101-export.hcl b/pkg/services/ngalert/api/test-data/post-rulegroup-101-export.hcl index a851c059399..928b590a28a 100644 --- a/pkg/services/ngalert/api/test-data/post-rulegroup-101-export.hcl +++ b/pkg/services/ngalert/api/test-data/post-rulegroup-101-export.hcl @@ -9,8 +9,7 @@ resource "grafana_rule_group" "rule_group_0000" { condition = "condition" data { - ref_id = "query" - query_type = "" + ref_id = "query" relative_time_range { from = 18000 @@ -21,8 +20,7 @@ resource "grafana_rule_group" "rule_group_0000" { model = "{\n \"expr\": \"http_request_duration_microseconds_count\",\n \"hide\": false,\n \"interval\": \"\",\n \"intervalMs\": 1000,\n \"legendFormat\": \"\",\n \"maxDataPoints\": 100,\n \"refId\": \"query\"\n }" } data { - ref_id = "reduced" - query_type = "" + ref_id = "reduced" relative_time_range { from = 18000 @@ -33,8 +31,7 @@ resource "grafana_rule_group" "rule_group_0000" { model = "{\n \"expression\": \"query\",\n \"hide\": false,\n \"intervalMs\": 1000,\n \"maxDataPoints\": 100,\n \"reducer\": \"mean\",\n \"refId\": \"reduced\",\n \"type\": \"reduce\"\n }" } data { - ref_id = "condition" - query_type = "" + ref_id = "condition" relative_time_range { from = 18000 @@ -47,9 +44,6 @@ resource "grafana_rule_group" "rule_group_0000" { no_data_state = "NoData" exec_err_state = "Alerting" - for = 0 - annotations = null - labels = null is_paused = false } rule { @@ -57,8 +51,7 @@ resource "grafana_rule_group" "rule_group_0000" { condition = "B" data { - ref_id = "A" - query_type = "" + ref_id = "A" relative_time_range { from = 18000 @@ -69,8 +62,7 @@ resource "grafana_rule_group" "rule_group_0000" { model = "{\n \"alias\": \"just-testing\",\n \"intervalMs\": 1000,\n \"maxDataPoints\": 100,\n \"orgId\": 0,\n \"refId\": \"A\",\n \"scenarioId\": \"csv_metric_values\",\n \"stringInput\": \"1,20,90,30,5,0\"\n }" } data { - ref_id = "B" - query_type = "" + ref_id = "B" relative_time_range { from = 18000 @@ -83,9 +75,6 @@ resource "grafana_rule_group" "rule_group_0000" { no_data_state = "NoData" exec_err_state = "Alerting" - for = 0 - annotations = null - labels = null is_paused = false } } diff --git a/pkg/services/ngalert/api/test-data/post-rulegroup-101-export.json b/pkg/services/ngalert/api/test-data/post-rulegroup-101-export.json index e040a20bca9..07f2eaa24ca 100644 --- a/pkg/services/ngalert/api/test-data/post-rulegroup-101-export.json +++ b/pkg/services/ngalert/api/test-data/post-rulegroup-101-export.json @@ -8,7 +8,6 @@ "interval": "10s", "rules": [ { - "uid": "", "title": "prom query with SSE - 2", "condition": "condition", "data": [ @@ -69,7 +68,6 @@ "isPaused": false }, { - "uid": "", "title": "reduced testdata query - 2", "condition": "B", "data": [ diff --git a/pkg/services/ngalert/api/test-data/post-rulegroup-101-export.yaml b/pkg/services/ngalert/api/test-data/post-rulegroup-101-export.yaml index b49f057ea7b..f565e3628b1 100644 --- a/pkg/services/ngalert/api/test-data/post-rulegroup-101-export.yaml +++ b/pkg/services/ngalert/api/test-data/post-rulegroup-101-export.yaml @@ -5,8 +5,7 @@ groups: folder: foo bar interval: 10s rules: - - uid: "" - title: prom query with SSE - 2 + - title: prom query with SSE - 2 condition: condition data: - refId: query @@ -51,8 +50,7 @@ groups: execErrState: Alerting for: 0s isPaused: false - - uid: "" - title: reduced testdata query - 2 + - title: reduced testdata query - 2 condition: B data: - refId: A diff --git a/pkg/services/ngalert/api/tooling/definitions/provisioning_alert_rules.go b/pkg/services/ngalert/api/tooling/definitions/provisioning_alert_rules.go index 777b6cc86e3..c24cb424c73 100644 --- a/pkg/services/ngalert/api/tooling/definitions/provisioning_alert_rules.go +++ b/pkg/services/ngalert/api/tooling/definitions/provisioning_alert_rules.go @@ -234,25 +234,25 @@ type AlertRuleGroupExport struct { // AlertRuleExport is the provisioned file export of models.AlertRule. type AlertRuleExport struct { - UID string `json:"uid" yaml:"uid"` + UID string `json:"uid,omitempty" yaml:"uid,omitempty"` Title string `json:"title" yaml:"title" hcl:"name"` Condition string `json:"condition" yaml:"condition" hcl:"condition"` Data []AlertQueryExport `json:"data" yaml:"data" hcl:"data,block"` - DashboardUID string `json:"dasboardUid,omitempty" yaml:"dashboardUid,omitempty"` - PanelID int64 `json:"panelId,omitempty" yaml:"panelId,omitempty"` + DashboardUID *string `json:"dasboardUid,omitempty" yaml:"dashboardUid,omitempty"` + PanelID *int64 `json:"panelId,omitempty" yaml:"panelId,omitempty"` NoDataState NoDataState `json:"noDataState" yaml:"noDataState" hcl:"no_data_state"` ExecErrState ExecutionErrorState `json:"execErrState" yaml:"execErrState" hcl:"exec_err_state"` For model.Duration `json:"for" yaml:"for"` - ForSeconds int64 `json:"-" yaml:"-" hcl:"for"` - Annotations map[string]string `json:"annotations,omitempty" yaml:"annotations,omitempty" hcl:"annotations"` - Labels map[string]string `json:"labels,omitempty" yaml:"labels,omitempty" hcl:"labels"` + ForSeconds *int64 `json:"-" yaml:"-" hcl:"for"` + Annotations *map[string]string `json:"annotations,omitempty" yaml:"annotations,omitempty" hcl:"annotations"` + Labels *map[string]string `json:"labels,omitempty" yaml:"labels,omitempty" hcl:"labels"` IsPaused bool `json:"isPaused" yaml:"isPaused" hcl:"is_paused"` } // AlertQueryExport is the provisioned export of models.AlertQuery. type AlertQueryExport struct { RefID string `json:"refId" yaml:"refId" hcl:"ref_id"` - QueryType string `json:"queryType,omitempty" yaml:"queryType,omitempty" hcl:"query_type"` + QueryType *string `json:"queryType,omitempty" yaml:"queryType,omitempty" hcl:"query_type"` RelativeTimeRange RelativeTimeRangeExport `json:"relativeTimeRange,omitempty" yaml:"relativeTimeRange,omitempty" hcl:"relative_time_range,block"` DatasourceUID string `json:"datasourceUid" yaml:"datasourceUid" hcl:"datasource_uid"` Model map[string]any `json:"model" yaml:"model"`