From 930804f98bf411f117d375c1b1bd111577d815b9 Mon Sep 17 00:00:00 2001 From: Marco Maurer Date: Wed, 11 Sep 2024 11:58:36 +0200 Subject: [PATCH] feat: Allow adding extra annotations or override existing ones --- README.md | 3 ++- grafana_alert.tf | 5 ++++- test/test.tf | 14 ++++++++++++++ variables.tf | 1 + 4 files changed, 21 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index a918425..b1ca52f 100644 --- a/README.md +++ b/README.md @@ -72,6 +72,7 @@ Every alert supports the following overrides: | `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`. | | `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`. | | `labels` | `map(string)` | Extra labels to add. It is also possible to override already defined labels like `severity`. | @@ -93,7 +94,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({
alert_threshold = optional(number)
exec_err_state = optional(string)
is_paused = optional(bool)
no_data_state = optional(string)
labels = optional(map(string))
}))
| `{}` | no | +| overrides | Overrides per Alert rule |
map(object({
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))
}))
| `{}` | 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 66d4914..dd39c72 100644 --- a/grafana_alert.tf +++ b/grafana_alert.tf @@ -24,7 +24,10 @@ resource "grafana_rule_group" "this" { for = try(rule.value.for, null) condition = "ALERTCONDITION" - annotations = {for k, v in rule.value.annotations : k => replace(v, "$value", "$values.QUERY_RESULT.Value")} + annotations = { + for k, v in merge(rule.value.annotations, try(var.overrides[rule.value.alert].annotations, {})) : + k => replace(v, "$value", "$values.QUERY_RESULT.Value") + } labels = merge(rule.value.labels, try(var.overrides[rule.value.alert].labels, {})) exec_err_state = coalesce(try(var.overrides[rule.value.alert].exec_err_state, null), "Error") diff --git a/test/test.tf b/test/test.tf index a7e1321..6703609 100644 --- a/test/test.tf +++ b/test/test.tf @@ -8,6 +8,20 @@ module "test_cert_manager" { # Allow editing the rule within Grafana UI disable_provenance = true + + # Overrides per alert + overrides = { + "CertManagerAbsent" = { + annotations = { + my_custom_annotation = "foobar" + runbook_url = "https://example.com" + } + + labels = { + mycustomlabel = "foobar" + } + } + } } # Test the module against well-known kubernetes alert rules diff --git a/variables.tf b/variables.tf index b7e35fb..25b4d50 100644 --- a/variables.tf +++ b/variables.tf @@ -21,6 +21,7 @@ variable "overrides" { is_paused = optional(bool) no_data_state = optional(string) labels = optional(map(string)) + annotations = optional(map(string)) })) default = {} }