updated grafana modules

This commit is contained in:
Stanislav Kopp 2025-08-05 16:40:48 +02:00
parent ae79eda745
commit 7413af8b2b
15 changed files with 159 additions and 81 deletions

View file

@ -1,10 +1,8 @@
locals {
#################################################################
# 1. Discover & decode alert YAML files from <root>/<alerts_dir>
#################################################################
# 1. Find and decode YAML files
alert_files = fileset(
"${path.root}/${var.alerts_dir}",
var.file_pattern
"**/*.yaml"
)
decoded_files = [
@ -12,31 +10,36 @@ locals {
yamldecode(file("${path.root}/${var.alerts_dir}/${f}"))
]
#################################################################
# 2. Flatten: each file may contain multiple groups
#################################################################
# 2. Flatten: each YAML may define multiple groups
groups_raw = flatten([
for doc in local.decoded_files : try(doc.groups, [])
])
#################################################################
# 3. Merge defaults & convert camelCase snake_case
#################################################################
# 3. Inject defaults (datasource, folder, interval) and sanitize models
groups = [
for g in local.groups_raw : merge(g, {
uid = try(
g.uid,
trim(replace(replace(lower(g.name), " ", "-"), "_", "-"), "-")
)
folder_uid = try(try(g.folder_uid, g.folder), var.default_folder_uid)
interval = try(g.interval, "1m")
uid = try(g.uid, lower(replace(g.name, " ", "-")))
folder_uid = var.folder_uid
interval = try(g.interval, "${var.default_interval_seconds}s")
rules = [
for r in g.rules : merge(r, {
data = [
for d in r.data : merge(d, {
datasource_uid = try(d.datasourceUid, var.default_datasource_uid)
# Preserve __expr__ for expressions, otherwise inject default UID
datasource_uid = (
try(d.datasourceUid, "") == "__expr__"
? "__expr__"
: var.datasource_uid
)
relative_time_range = try(d.relativeTimeRange, { from = 600, to = 0 })
# Sanitize model: remove keys Grafana ignores to prevent plan drift
model = {
for k, v in d.model : k => v
if !(k == "queryType" || k == "query_type")
}
})
]
})

View file

@ -1,8 +1,5 @@
resource "grafana_rule_group" "this" {
for_each = {
for g in local.groups :
g.uid => g
}
for_each = { for g in local.groups : g.uid => g }
name = each.value.name
folder_uid = each.value.folder_uid
@ -21,14 +18,15 @@ resource "grafana_rule_group" "this" {
uid = rule.value.uid
name = try(rule.value.title, rule.value.name)
condition = rule.value.condition
for = try(rule.value.for, null)
no_data_state = rule.value.noDataState
exec_err_state = rule.value.execErrState
is_paused = try(rule.value.isPaused, false)
for = try(rule.value.for, null)
labels = try(rule.value.labels, {})
# Merge team labels with platform-injected receiver
annotations = try(rule.value.annotations, {})
labels = try(rule.value.labels, {})
dynamic "data" {
for_each = rule.value.data

View file

@ -2,3 +2,10 @@ output "rule_group_ids" {
description = "Map: <folder_uid>.<name> → Grafana rule-group ID"
value = { for k, v in grafana_rule_group.this : k => v.id }
}
output "debug_alert_files" {
value = local.alert_files
}
output "debug_groups" {
description = "Parsed groups from YAML"
value = local.groups
}

View file

@ -4,35 +4,29 @@ variable "alerts_dir" {
description = "Relative path to the directory containing alert rule YAML files."
}
variable "file_pattern" {
variable "datasource_uid" {
description = "Grafana datasource UID to apply to all alerts in this module"
type = string
default = "*.y{a,}ml"
description = "Glob pattern to match alert rule YAML files (e.g. *.yaml, *.yml)."
}
variable "default_datasource_uid" {
variable "folder_uid" {
description = "Grafana folder UID where alerts will be placed"
type = string
description = "UID of the Prometheus or Thanos datasource to use if not specified in the alert rule."
}
variable "default_receiver" {
variable "receiver" {
description = "Contact point name to associate with alerts"
type = string
description = "Name of the contact point (receiver) to use for notifications if not defined in alert rule."
}
variable "default_folder_uid" {
type = string
description = "UID of the Grafana folder to use for alert rules when not defined in the YAML."
}
variable "default_interval_seconds" {
description = "Default evaluation interval (in seconds)"
type = number
default = 60
description = "Default evaluation interval (in seconds) for alert rule groups if not set in YAML."
}
variable "disable_provenance" {
description = "Disable provenance flag for imported alerts"
type = bool
default = false
description = "If true, disables Grafana alert provisioning provenance (sets disable_provenance = true)."
}