diff --git a/README.md b/README.md index b1ca52f..e821610 100644 --- a/README.md +++ b/README.md @@ -70,6 +70,7 @@ Every alert supports the following overrides: |--------------------|---------------|-------------| | `alert_threshold` | `number` | Threshold of the Grafana alert. Defaults to `0` | | `exec_err_state` | `string` | Describes what state to enter when the rule's query is invalid and the rule cannot be executed. Options are `OK`, `Error`, `KeepLast`, and `Alerting`. Defaults to `Error`. | +| `expr` | `string` | Use a custom PromQL expression for this Grafana alert. | | `is_paused` | `bool` | Sets whether the alert should be paused or not. Defaults to `false`. | | `no_data_state` | `string` | Describes what state to enter when the rule's query returns No Data. Options are `OK`, `NoData`, `KeepLast`, and `Alerting`. Defaults to `OK`. | | `annotations` | `map(string)` | Extra annotations to add. It is also possible to override already defined annotations like `runbook_url`. | @@ -94,7 +95,7 @@ Every alert supports the following overrides: | disable\_provenance | Allow modifying the rule group from other sources than Terraform or the Grafana API. | `bool` | `false` | no | | folder\_uid | The UID of the Grafana folder that the alerts belongs to. | `string` | n/a | yes | | org\_id | The Organization ID of of the Grafana Alerting rule groups. (Only supported with basic auth, API keys are already org-scoped) | `string` | `null` | no | -| overrides | Overrides per Alert rule |
map(object({| `{}` | no | +| overrides | Overrides per Alert rule |
alert_threshold = optional(number)
exec_err_state = optional(string)
is_paused = optional(bool)
no_data_state = optional(string)
labels = optional(map(string))
annotations = optional(map(string))
}))
map(object({| `{}` | no | | prometheus\_alerts\_file\_path | Path to the Prometheus Alerting rules file | `string` | n/a | yes | ### Outputs diff --git a/grafana_alert.tf b/grafana_alert.tf index 966a1b1..06400e1 100644 --- a/grafana_alert.tf +++ b/grafana_alert.tf @@ -42,7 +42,7 @@ resource "grafana_rule_group" "this" { datasource_uid = var.datasource_uid model = jsonencode({ editorMode = "code" - expr = rule.value.expr + expr = coalesce(try(var.overrides[rule.value.alert].expr, null), rule.value.expr) intervalMs = 1000 maxDataPoints = 43200 refId = "QUERY" diff --git a/test/test.tf b/test/test.tf index ae69917..8516b7d 100644 --- a/test/test.tf +++ b/test/test.tf @@ -12,6 +12,8 @@ module "test_cert_manager" { # Overrides per alert overrides = { "CertManagerAbsent" = { + expr = "absent(up{job=\"cert-manager\", cluster!~\"staging.*\"})" + annotations = { my_custom_annotation = "foobar" runbook_url = "https://example.com" diff --git a/variables.tf b/variables.tf index 5942c3a..d3997c8 100644 --- a/variables.tf +++ b/variables.tf @@ -18,6 +18,7 @@ variable "overrides" { type = map(object({ alert_threshold = optional(number) exec_err_state = optional(string) + expr = optional(string) is_paused = optional(bool) no_data_state = optional(string) labels = optional(map(string))
alert_threshold = optional(number)
exec_err_state = optional(string)
expr = optional(string)
is_paused = optional(bool)
no_data_state = optional(string)
labels = optional(map(string))
annotations = optional(map(string))
}))