Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
314 changes: 314 additions & 0 deletions docs/resources/asserts_prom_rule_file.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,314 @@
---
# generated by https://github.com/hashicorp/terraform-plugin-docs
page_title: "grafana_asserts_prom_rule_file Resource - terraform-provider-grafana"
subcategory: "Knowledge Graph"
description: |-
Manages Prometheus Rules configurations through Grafana Asserts API. Allows creation and management of custom Prometheus recording and alerting rules.
---

# grafana_asserts_prom_rule_file (Resource)

Manages Prometheus Rules configurations through Grafana Asserts API. Allows creation and management of custom Prometheus recording and alerting rules.

## Example Usage

```terraform
# Basic recording rule for latency metrics
resource "grafana_asserts_prom_rule_file" "latency_metrics" {
name = "custom-latency-metrics"
active = true

group {
name = "latency_recording_rules"
interval = "30s"

rule {
record = "custom:latency:p95"
expr = "histogram_quantile(0.95, rate(http_request_duration_seconds_bucket[5m]))"
labels = {
source = "custom_instrumentation"
severity = "info"
}
}

rule {
record = "custom:latency:p99"
expr = "histogram_quantile(0.99, rate(http_request_duration_seconds_bucket[5m]))"
labels = {
source = "custom_instrumentation"
severity = "info"
}
}
}
}

# Alert rules for high latency
resource "grafana_asserts_prom_rule_file" "latency_alerts" {
name = "custom-latency-alerts"
active = true

group {
name = "latency_alerting"
interval = "30s"

rule {
alert = "HighLatency"
expr = "histogram_quantile(0.99, rate(http_request_duration_seconds_bucket[5m])) > 0.5"
duration = "5m"
labels = {
severity = "warning"
category = "Latency"
}
annotations = {
summary = "High latency detected"
description = "P99 latency is above 500ms for 5 minutes"
}
}

rule {
alert = "VeryHighLatency"
expr = "histogram_quantile(0.99, rate(http_request_duration_seconds_bucket[5m])) > 1.0"
duration = "2m"
labels = {
severity = "critical"
category = "Latency"
}
annotations = {
summary = "Very high latency detected"
description = "P99 latency is above 1 second"
}
}
}
}

# Comprehensive monitoring rules with multiple groups
resource "grafana_asserts_prom_rule_file" "comprehensive_monitoring" {
name = "custom-comprehensive-monitoring"
active = true

# Latency monitoring
group {
name = "latency_monitoring"
interval = "30s"

rule {
record = "custom:latency:p99"
expr = "histogram_quantile(0.99, rate(http_request_duration_seconds_bucket[5m]))"
labels = {
source = "custom"
}
}

rule {
alert = "HighLatency"
expr = "custom:latency:p99 > 0.5"
duration = "5m"
labels = {
severity = "warning"
}
annotations = {
summary = "High latency detected"
}
}
}

# Error rate monitoring
group {
name = "error_monitoring"
interval = "1m"

rule {
record = "custom:error:rate"
expr = "rate(http_requests_total{status=~\"5..\"}[5m])"
labels = {
source = "custom"
}
}

rule {
alert = "HighErrorRate"
expr = "custom:error:rate > 0.1"
duration = "10m"
labels = {
severity = "critical"
category = "Errors"
}
annotations = {
summary = "High error rate detected"
description = "Error rate is above 10%"
}
}
}

# Throughput monitoring
group {
name = "throughput_monitoring"
interval = "1m"

rule {
record = "custom:throughput:total"
expr = "sum(rate(http_requests_total[5m]))"
labels = {
source = "custom"
}
}

rule {
alert = "LowThroughput"
expr = "custom:throughput:total < 10"
duration = "5m"
labels = {
severity = "warning"
category = "Throughput"
}
annotations = {
summary = "Low throughput detected"
description = "Request throughput is below 10 requests/second"
}
}
}
}

# Rules with conditional enablement
resource "grafana_asserts_prom_rule_file" "conditional_rules" {
name = "custom-conditional-rules"
active = true

group {
name = "environment_specific_rules"
interval = "30s"

rule {
alert = "TestAlert"
expr = "up == 0"
duration = "1m"
labels = {
severity = "info"
}
annotations = {
summary = "Test alert that is disabled in production"
}
# This rule will be disabled in the production group
disable_in_groups = ["production"]
}

rule {
alert = "CriticalAlert"
expr = "up == 0"
duration = "30s"
labels = {
severity = "critical"
}
annotations = {
summary = "Critical alert that fires in all environments"
}
}
}
}

# Inactive rules (for staging/testing)
resource "grafana_asserts_prom_rule_file" "staging_rules" {
name = "custom-staging-rules"
active = false # Rules file is inactive

group {
name = "staging_tests"
interval = "1m"

rule {
record = "staging:test:metric"
expr = "up"
labels = {
environment = "staging"
}
}
}
}

# SLO-based alerting
resource "grafana_asserts_prom_rule_file" "slo_alerts" {
name = "custom-slo-alerts"
active = true

group {
name = "slo_monitoring"
interval = "1m"

rule {
record = "custom:slo:availability"
expr = "sum(rate(http_requests_total{status!~\"5..\"}[5m])) / sum(rate(http_requests_total[5m]))"
labels = {
slo_type = "availability"
}
}

rule {
alert = "SLOAvailabilityBreach"
expr = "custom:slo:availability < 0.995"
duration = "5m"
labels = {
severity = "critical"
category = "SLO"
}
annotations = {
summary = "SLO availability breach"
description = "Availability is below 99.5% SLO target"
runbook_url = "https://docs.example.com/runbooks/availability-breach"
}
}
}
}
```

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

### Required

- `group` (Block List, Min: 1) List of Prometheus rule groups. Each group contains one or more rules and can have its own evaluation interval. (see [below for nested schema](#nestedblock--group))
- `name` (String) The name of the Prometheus rules file. This will be stored with a .custom extension. Must follow naming validation rules (alphanumeric, hyphens, underscores).

### Optional

- `active` (Boolean) Whether the rules file is active. Inactive rules are not evaluated. Defaults to `true`.

### Read-Only

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

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

Required:

- `name` (String) The name of the rule group (e.g., 'latency_monitoring').
- `rule` (Block List, Min: 1) List of Prometheus rules in this group. (see [below for nested schema](#nestedblock--group--rule))

Optional:

- `interval` (String) Evaluation interval for this group (e.g., '30s', '1m'). If not specified, uses the global evaluation interval.

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

Required:

- `expr` (String) The PromQL expression to evaluate.

Optional:

- `active` (Boolean) Whether this specific rule is active. This field is read-only and controlled by the API.
- `alert` (String) The name of the alert for alerting rules. Either 'record' or 'alert' must be specified, but not both.
- `annotations` (Map of String) Annotations to add to alerts (e.g., summary, description).
- `disable_in_groups` (Set of String) List of group names where this rule should be disabled. Useful for conditional rule enablement.
- `duration` (String) How long the condition must be true before firing the alert (e.g., '5m'). Only applicable for alerting rules. Maps to 'for' in Prometheus.
- `labels` (Map of String) Labels to attach to the resulting time series or alert.
- `record` (String) The name of the time series to output for recording rules. Either 'record' or 'alert' must be specified, but not both.

## Import

Import is supported using the following syntax:

```shell
terraform import grafana_asserts_prom_rule_file.name "{{ name }}"
```
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
terraform import grafana_asserts_prom_rule_file.name "{{ name }}"
Loading
Loading