Skip to content

Commit

Permalink
Alerting: Add support for alert rule groups (#584)
Browse files Browse the repository at this point in the history
* Basic TF definition of an alert rule

* Implement alert rule read context and most packing logic

* Add test harness, resource example, and register resource provider

* Expand example and fix bugs in resource schema

* Implement delete hook and checkDestroy in tests

* Map additional fields, get rule creation working

* Handle diffs of the nested json blob

* Tests for created object

* Add test for import

* Add test for updating a non-key field

* Implement update

* Add tests for changing group key fields

* Move to the new PUT route, get existing tests passing

* Add test for changing group interval

* Add test for groups with multiple rules

* Test for updating compound rules in place

* Test for adding rules to groups

* Add test for removing rules from a group

* Complete lots of field documentation

* Complete additional TODOs in docs and field requirements

* Propagate some errors correctly

* Drop manual override

* Test against 9.1.0

* Delete unused function

* Generate documentation

* Make for a prom-style string instead of integer number of seconds

* Regenerate docs

* Rename rules field to rule

* Rename object to rule group

* Regenerate docs
  • Loading branch information
alexweav authored Aug 23, 2022
1 parent fb8979d commit bf062df
Show file tree
Hide file tree
Showing 10 changed files with 1,544 additions and 0 deletions.
171 changes: 171 additions & 0 deletions docs/resources/rule_group.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,171 @@
---
# generated by https://github.com/hashicorp/terraform-plugin-docs
page_title: "grafana_rule_group Resource - terraform-provider-grafana"
subcategory: ""
description: |-
Manages Grafana Alerting rule groups.
Official documentation https://grafana.com/docs/grafana/latest/alerting/alerting-rulesHTTP API https://grafana.com/docs/grafana/latest/developers/http_api/alerting_provisioning/#alert-rules
---

# grafana_rule_group (Resource)

Manages Grafana Alerting rule groups.

* [Official documentation](https://grafana.com/docs/grafana/latest/alerting/alerting-rules)
* [HTTP API](https://grafana.com/docs/grafana/latest/developers/http_api/alerting_provisioning/#alert-rules)

## Example Usage

```terraform
resource "grafana_folder" "rule_folder" {
title = "My Alert Rule Folder"
}
resource "grafana_rule_group" "my_alert_rule" {
name = "My Rule Group"
folder_uid = grafana_folder.rule_folder.uid
interval_seconds = 240
org_id = 1
rule {
name = "My Alert Rule 1"
for = "2m"
condition = "B"
no_data_state = "NoData"
exec_err_state = "Alerting"
annotations = {
"a" = "b"
"c" = "d"
}
labels = {
"e" = "f"
"g" = "h"
}
data {
ref_id = "A"
query_type = ""
relative_time_range {
from = 600
to = 0
}
datasource_uid = "PD8C576611E62080A"
model = jsonencode({
hide = false
intervalMs = 1000
maxDataPoints = 43200
refId = "A"
})
}
data {
ref_id = "B"
query_type = ""
relative_time_range {
from = 0
to = 0
}
datasource_uid = "-100"
model = <<EOT
{
"conditions": [
{
"evaluator": {
"params": [
3
],
"type": "gt"
},
"operator": {
"type": "and"
},
"query": {
"params": [
"A"
]
},
"reducer": {
"params": [],
"type": "last"
},
"type": "query"
}
],
"datasource": {
"type": "__expr__",
"uid": "-100"
},
"hide": false,
"intervalMs": 1000,
"maxDataPoints": 43200,
"refId": "B",
"type": "classic_conditions"
}
EOT
}
}
}
```

<!-- schema generated by tfplugindocs -->
## Schema

### Required

- `folder_uid` (String) The UID of the group that the folder belongs to.
- `interval_seconds` (Number) The interval, in seconds, at which all rules in the group are evaluated. If a group contains many rules, the rules are evaluated sequentially.
- `name` (String) The name of the rule group.
- `org_id` (Number) The ID of the org to which the group belongs.
- `rule` (Block List, Min: 1) The rules within the group. (see [below for nested schema](#nestedblock--rule))

### Read-Only

- `id` (String) The ID of this resource.

<a id="nestedblock--rule"></a>
### Nested Schema for `rule`

Required:

- `condition` (String) The `ref_id` of the query node in the `data` field to use as the alert condition.
- `data` (Block List, Min: 1) A sequence of stages that describe the contents of the rule. (see [below for nested schema](#nestedblock--rule--data))
- `name` (String) The name of the alert rule.

Optional:

- `annotations` (Map of String) Key-value pairs of metadata to attach to the alert rule that may add user-defined context, but cannot be used for matching, grouping, or routing. Defaults to `map[]`.
- `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, and Alerting. Defaults to `Alerting`.
- `for` (String) The amount of time for which the rule must be breached for the rule to be considered to be Firing. Before this time has elapsed, the rule is only considered to be Pending. Defaults to `0`.
- `labels` (Map of String) Key-value pairs to attach to the alert rule that can be used in matching, grouping, and routing. Defaults to `map[]`.
- `no_data_state` (String) Describes what state to enter when the rule's query returns No Data. Options are OK, NoData, and Alerting. Defaults to `NoData`.

Read-Only:

- `uid` (String) The unique identifier of the alert rule.

<a id="nestedblock--rule--data"></a>
### Nested Schema for `rule.data`

Required:

- `datasource_uid` (String) The UID of the datasource being queried, or "-100" if this stage is an expression stage.
- `model` (String) Custom JSON data to send to the specified datasource when querying.
- `ref_id` (String) A unique string to identify this query stage within a rule.
- `relative_time_range` (Block List, Min: 1, Max: 1) The time range, relative to when the query is executed, across which to query. (see [below for nested schema](#nestedblock--rule--data--relative_time_range))

Optional:

- `query_type` (String) An optional identifier for the type of query being executed. Defaults to ``.

<a id="nestedblock--rule--data--relative_time_range"></a>
### Nested Schema for `rule.data.relative_time_range`

Required:

- `from` (Number) The number of seconds in the past, relative to when the rule is evaluated, at which the time range begins.
- `to` (Number) The number of seconds in the past, relative to when the rule is evaluated, at which the time range ends.

## Import

Import is supported using the following syntax:

```shell
terraform import grafana_alert_rule.alert_rule_name {{alert_rule_name}}
```
161 changes: 161 additions & 0 deletions examples/resources/grafana_rule_group/_acc_multi_rule_group.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,161 @@
resource "grafana_folder" "compound_rule_folder" {
title = "My Compound Alert Rule Folder"
}

resource "grafana_rule_group" "my_multi_alert_group" {
name = "My Multi-Alert Rule Group"
folder_uid = grafana_folder.compound_rule_folder.uid
interval_seconds = 240
org_id = 1
rule {
name = "My Alert Rule 1"
for = "2m"
condition = "B"
no_data_state = "NoData"
exec_err_state = "Alerting"
annotations = {
"a" = "b"
"c" = "d"
}
labels = {
"e" = "f"
"g" = "h"
}
data {
ref_id = "A"
query_type = ""
relative_time_range {
from = 600
to = 0
}
datasource_uid = "PD8C576611E62080A"
model = jsonencode({
hide = false
intervalMs = 1000
maxDataPoints = 43200
refId = "A"
})
}
data {
ref_id = "B"
query_type = ""
relative_time_range {
from = 0
to = 0
}
datasource_uid = "-100"
model = <<EOT
{
"conditions": [
{
"evaluator": {
"params": [
3
],
"type": "gt"
},
"operator": {
"type": "and"
},
"query": {
"params": [
"A"
]
},
"reducer": {
"params": [],
"type": "last"
},
"type": "query"
}
],
"datasource": {
"type": "__expr__",
"uid": "-100"
},
"hide": false,
"intervalMs": 1000,
"maxDataPoints": 43200,
"refId": "B",
"type": "classic_conditions"
}
EOT
}
}

rule {
name = "My Alert Rule 2"
for = "4m"
condition = "B"
no_data_state = "NoData"
exec_err_state = "Alerting"
annotations = {
"a" = "b"
"c" = "d"
}
labels = {
"e" = "f"
"g" = "h"
}
data {
ref_id = "A"
query_type = ""
relative_time_range {
from = 600
to = 0
}
datasource_uid = "PD8C576611E62080A"
model = jsonencode({
hide = false
intervalMs = 1000
maxDataPoints = 43200
refId = "A"
})
}
data {
ref_id = "B"
query_type = ""
relative_time_range {
from = 0
to = 0
}
datasource_uid = "-100"
model = <<EOT
{
"conditions": [
{
"evaluator": {
"params": [
3
],
"type": "gt"
},
"operator": {
"type": "and"
},
"query": {
"params": [
"A"
]
},
"reducer": {
"params": [],
"type": "last"
},
"type": "query"
}
],
"datasource": {
"type": "__expr__",
"uid": "-100"
},
"hide": false,
"intervalMs": 1000,
"maxDataPoints": 43200,
"refId": "B",
"type": "classic_conditions"
}
EOT
}
}
}
Loading

0 comments on commit bf062df

Please sign in to comment.