|
1 | 1 | locals {
|
2 | 2 | enabled = module.this.enabled
|
3 | 3 |
|
4 |
| - datadog_monitor_slos = { for slo in var.datadog_slos : slo.name => slo if slo.type == "monitor" && lookup(slo, "enabled", true) && local.enabled } |
5 |
| - datadog_metric_slos = { for slo in var.datadog_slos : slo.name => slo if slo.type == "metric" && lookup(slo, "enabled", true) && local.enabled } |
6 |
| - |
7 |
| - temp_datadog_slo_metric_monitors = flatten([ |
8 |
| - for name, slo in var.datadog_slos : [ |
9 |
| - for i, threshold in slo.thresholds : { |
10 |
| - slo = slo, |
11 |
| - slo_name = format("%s_threshold%s", name, i) |
12 |
| - threshold = threshold |
13 |
| - } |
14 |
| - if slo.type == "metric" && local.enabled && lookup(slo, "enabled", true) |
15 |
| - ] |
16 |
| - ]) |
17 |
| - |
18 |
| - datadog_slo_metric_monitors = { for monitor in local.temp_datadog_slo_metric_monitors : monitor.slo_name => monitor } |
19 |
| - |
20 | 4 | alert_tags = local.enabled && var.alert_tags != null ? format("%s%s", var.alert_tags_separator, join(var.alert_tags_separator, var.alert_tags)) : ""
|
21 | 5 | }
|
22 |
| - |
23 |
| -resource "datadog_service_level_objective" "monitor_slo" { |
24 |
| - for_each = local.datadog_monitor_slos |
25 |
| - |
26 |
| - # Required |
27 |
| - name = each.value.name |
28 |
| - type = each.value.type |
29 |
| - |
30 |
| - dynamic "thresholds" { |
31 |
| - for_each = each.value.thresholds |
32 |
| - content { |
33 |
| - target = lookup(thresholds, "target", "99.00") |
34 |
| - timeframe = lookup(thresholds, "timeframe", "7d") |
35 |
| - |
36 |
| - target_display = lookup(thresholds, "target_display", "98.00") |
37 |
| - warning = lookup(thresholds, "warning", "99.95") |
38 |
| - warning_display = lookup(thresholds, "warning_display", "98.00") |
39 |
| - } |
40 |
| - } |
41 |
| - |
42 |
| - groups = lookup(each.value, "groups", []) |
43 |
| - monitor_ids = each.value.monitor_ids |
44 |
| - |
45 |
| - # Optional |
46 |
| - description = lookup(each.value, "description", null) |
47 |
| - force_delete = lookup(each.value, "force_delete", true) |
48 |
| - validate = lookup(each.value, "validate", false) |
49 |
| - |
50 |
| - # Convert terraform tags map to Datadog tags map |
51 |
| - # If a key is supplied with a value, it will render "key:value" as a tag |
52 |
| - # tags: |
53 |
| - # key: value |
54 |
| - # If a key is supplied without a value (null), it will render "key" as a tag |
55 |
| - # tags: |
56 |
| - # key: null |
57 |
| - tags = [ |
58 |
| - for tagk, tagv in lookup(each.value, "tags", module.this.tags) : (tagv != null ? format("%s:%s", tagk, tagv) : tagk) |
59 |
| - ] |
60 |
| -} |
61 |
| - |
62 |
| -resource "datadog_service_level_objective" "metric_slo" { |
63 |
| - for_each = local.datadog_metric_slos |
64 |
| - |
65 |
| - # Required |
66 |
| - name = each.value.name |
67 |
| - type = each.value.type |
68 |
| - |
69 |
| - query { |
70 |
| - denominator = each.value.query.denominator |
71 |
| - numerator = each.value.query.numerator |
72 |
| - } |
73 |
| - |
74 |
| - # Optional |
75 |
| - description = lookup(each.value, "description", null) |
76 |
| - force_delete = lookup(each.value, "force_delete", true) |
77 |
| - validate = lookup(each.value, "validate", false) |
78 |
| - |
79 |
| - dynamic "thresholds" { |
80 |
| - for_each = each.value.thresholds |
81 |
| - content { |
82 |
| - target = lookup(thresholds.value, "target", null) |
83 |
| - timeframe = lookup(thresholds.value, "timeframe", null) |
84 |
| - warning = lookup(thresholds.value, "warning", null) |
85 |
| - } |
86 |
| - } |
87 |
| - |
88 |
| - # Convert terraform tags map to Datadog tags map |
89 |
| - # If a key is supplied with a value, it will render "key:value" as a tag |
90 |
| - # tags: |
91 |
| - # key: value |
92 |
| - # If a key is supplied without a value (null), it will render "key" as a tag |
93 |
| - # tags: |
94 |
| - # key: null |
95 |
| - tags = [ |
96 |
| - for tagk, tagv in lookup(each.value, "tags", module.this.tags) : (tagv != null ? format("%s:%s", tagk, tagv) : tagk) |
97 |
| - ] |
98 |
| -} |
99 |
| - |
100 |
| -resource "datadog_monitor" "metric_slo_alert" { |
101 |
| - for_each = local.datadog_slo_metric_monitors |
102 |
| - |
103 |
| - name = format("(SLO Error Budget Alert) %s", each.value.slo.name) |
104 |
| - type = "slo alert" |
105 |
| - message = format("%s%s", each.value.slo.message, local.alert_tags) |
106 |
| - |
107 |
| - query = <<EOF |
108 |
| - 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")} |
109 |
| - EOF |
110 |
| - monitor_thresholds { |
111 |
| - critical = lookup(each.value.threshold, "target", null) |
112 |
| - } |
113 |
| - |
114 |
| - # Convert terraform tags map to Datadog tags map |
115 |
| - # If a key is supplied with a value, it will render "key:value" as a tag |
116 |
| - # tags: |
117 |
| - # key: value |
118 |
| - # If a key is supplied without a value (null), it will render "key" as a tag |
119 |
| - # tags: |
120 |
| - # key: null |
121 |
| - tags = [ |
122 |
| - for tagk, tagv in lookup(each.value.slo, "tags", module.this.tags) : (tagv != null ? format("%s:%s", tagk, tagv) : tagk) |
123 |
| - ] |
124 |
| -} |
0 commit comments