Skip to content

Commit

Permalink
feat: Allow overriding the alert expression (#7)
Browse files Browse the repository at this point in the history
  • Loading branch information
mkilchhofer authored Sep 13, 2024
1 parent bb4938c commit 849f32c
Show file tree
Hide file tree
Showing 4 changed files with 6 additions and 2 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`. |
Expand All @@ -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 | <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 |
| overrides | Overrides per Alert rule | <pre>map(object({<br> alert_threshold = optional(number)<br> exec_err_state = optional(string)<br> expr = 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
2 changes: 1 addition & 1 deletion grafana_alert.tf
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
2 changes: 2 additions & 0 deletions test/test.tf
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
1 change: 1 addition & 0 deletions variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down

0 comments on commit 849f32c

Please sign in to comment.