Alerting: Fix HCL export for alerts with non-zero "for" field. (#76739)

* Alerting: Fix HCL export for alerts with non-zero "for" field.

Fixes #76734

* fix tests

---------

Co-authored-by: Yuri Tseretyan <yuriy.tseretyan@grafana.com>
This commit is contained in:
Steve Simpson 2023-10-20 11:09:08 +02:00 committed by GitHub
parent 8985f79aab
commit a0476741f2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 15 additions and 10 deletions

View File

@ -605,7 +605,7 @@ func TestProvisioningApi(t *testing.T) {
no_data_state = "Alerting"
exec_err_state = "Error"
for = 0
for = "0s"
annotations = {
test = "annotation"
}
@ -632,7 +632,7 @@ func TestProvisioningApi(t *testing.T) {
no_data_state = "OK"
exec_err_state = "OK"
for = 0
for = "0s"
is_paused = false
}
}

View File

@ -186,7 +186,7 @@ func AlertRuleExportFromAlertRule(rule models.AlertRule) (definitions.AlertRuleE
IsPaused: rule.IsPaused,
}
if rule.For.Seconds() > 0 {
result.ForSeconds = util.Pointer(int64(rule.For.Seconds()))
result.ForString = util.Pointer(model.Duration(rule.For).String())
}
if rule.Annotations != nil {
result.Annotations = &rule.Annotations

View File

@ -44,6 +44,7 @@ resource "grafana_rule_group" "rule_group_0000" {
no_data_state = "NoData"
exec_err_state = "Alerting"
for = "2m"
is_paused = false
}
rule {

View File

@ -64,7 +64,7 @@
],
"noDataState": "NoData",
"execErrState": "Alerting",
"for": "0s",
"for": "2m",
"isPaused": false
},
{
@ -114,4 +114,4 @@
]
}
]
}
}

View File

@ -48,7 +48,7 @@ groups:
type: math
noDataState: NoData
execErrState: Alerting
for: 0s
for: 2m
isPaused: false
- title: reduced testdata query - 2
condition: B

View File

@ -3,6 +3,7 @@
"interval": "10s",
"rules": [
{
"for": "2m",
"grafana_alert": {
"title": "prom query with SSE - 2",
"condition": "condition",

View File

@ -243,10 +243,13 @@ type AlertRuleExport struct {
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"`
IsPaused bool `json:"isPaused" yaml:"isPaused" hcl:"is_paused"`
// ForString is used to:
// - Only export the for field for HCL if it is non-zero.
// - Format the Prometheus model.Duration type properly for HCL.
ForString *string `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.