Skip to content

Commit

Permalink
feat: Allow adding extra annotations or override existing ones
Browse files Browse the repository at this point in the history
  • Loading branch information
mkilchhofer committed Sep 11, 2024
1 parent 30c0917 commit 930804f
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 2 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`. |


Expand All @@ -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 | <pre>map(object({<br> alert_threshold = optional(number)<br> exec_err_state = optional(string)<br> is_paused = optional(bool)<br> no_data_state = optional(string)<br> labels = optional(map(string))<br> }))</pre> | `{}` | no |
| overrides | Overrides per Alert rule | <pre>map(object({<br> alert_threshold = optional(number)<br> exec_err_state = optional(string)<br> is_paused = optional(bool)<br> no_data_state = optional(string)<br> labels = optional(map(string))<br> annotations = optional(map(string))<br> }))</pre> | `{}` | no |
| prometheus\_alerts\_file\_path | Path to the Prometheus Alerting rules file | `string` | n/a | yes |

### Outputs
Expand Down
5 changes: 4 additions & 1 deletion grafana_alert.tf
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
14 changes: 14 additions & 0 deletions test/test.tf
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ variable "overrides" {
is_paused = optional(bool)
no_data_state = optional(string)
labels = optional(map(string))
annotations = optional(map(string))
}))
default = {}
}
Expand Down

0 comments on commit 930804f

Please sign in to comment.