forked from moneyforwardvietnam/terraform-datadog-monitor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmetric_slo.tf
82 lines (69 loc) · 2.73 KB
/
metric_slo.tf
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
locals {
datadog_metric_slos = { for slo in var.datadog_slos : slo.name => slo if slo.type == "metric" && lookup(slo, "enabled", true) && local.enabled }
temp_datadog_metric_slo_alerts = flatten([
for name, slo in var.datadog_slos : [
for i, threshold in slo.thresholds : {
slo = slo,
slo_name = format("%s_threshold%s", name, i)
threshold = threshold
}
if slo.type == "metric" && local.enabled && lookup(slo, "enabled", true)
]
])
datadog_metric_slo_alerts = { for monitor in local.temp_datadog_metric_slo_alerts : monitor.slo_name => monitor }
}
resource "datadog_service_level_objective" "metric_slo" {
for_each = local.datadog_metric_slos
# Required
name = each.value.name
type = each.value.type
# Optional
description = lookup(each.value, "description", null)
force_delete = lookup(each.value, "force_delete", true)
validate = lookup(each.value, "validate", false)
query {
denominator = each.value.query.denominator
numerator = each.value.query.numerator
}
dynamic "thresholds" {
for_each = each.value.thresholds
content {
target = lookup(thresholds.value, "target", null)
timeframe = lookup(thresholds.value, "timeframe", null)
warning = lookup(thresholds.value, "warning", null)
}
}
# Convert terraform tags map to Datadog tags map
# If a key is supplied with a value, it will render "key:value" as a tag
# tags:
# key: value
# If a key is supplied without a value (null), it will render "key" as a tag
# tags:
# key: null
tags = [
for tagk, tagv in lookup(each.value, "tags", module.this.tags) : (tagv != null ? format("%s:%s", tagk, tagv) : tagk)
]
}
# https://registry.terraform.io/providers/DataDog/datadog/latest/docs/resources/monitor
resource "datadog_monitor" "metric_slo_alert" {
for_each = local.datadog_metric_slo_alerts
type = "slo alert"
name = format("(SLO Error Budget Alert) %s", each.value.slo.name)
message = format("%s %s", each.value.slo.message, local.alert_tags)
query = <<EOF
error_budget("${datadog_service_level_objective.metric_slo[each.value.slo.name].id}").over("${each.value.threshold.timeframe}") > ${lookup(each.value.threshold, "target", "99.00")}
EOF
monitor_thresholds {
critical = lookup(each.value.threshold, "target", null)
}
# Convert terraform tags map to Datadog tags map
# If a key is supplied with a value, it will render "key:value" as a tag
# tags:
# key: value
# If a key is supplied without a value (null), it will render "key" as a tag
# tags:
# key: null
tags = [
for tagk, tagv in lookup(each.value.slo, "tags", module.this.tags) : (tagv != null ? format("%s:%s", tagk, tagv) : tagk)
]
}