From 50811d98de355f2987d7aaefdee1d9559df7683c Mon Sep 17 00:00:00 2001 From: Quentin Manfroi Date: Tue, 2 Oct 2018 13:53:19 +0200 Subject: [PATCH 1/7] MON-301 add support for excluding on service check --- common/alerting-message/{output.tf => outputs.tf} | 0 common/filter-tags/inputs.tf | 9 +++++++-- common/filter-tags/locals.tf | 3 ++- common/filter-tags/output.tf | 9 --------- common/filter-tags/outputs.tf | 10 ++++++++++ 5 files changed, 19 insertions(+), 12 deletions(-) rename common/alerting-message/{output.tf => outputs.tf} (100%) delete mode 100644 common/filter-tags/output.tf create mode 100644 common/filter-tags/outputs.tf diff --git a/common/alerting-message/output.tf b/common/alerting-message/outputs.tf similarity index 100% rename from common/alerting-message/output.tf rename to common/alerting-message/outputs.tf diff --git a/common/filter-tags/inputs.tf b/common/filter-tags/inputs.tf index 3e5abad..9c34609 100644 --- a/common/filter-tags/inputs.tf +++ b/common/filter-tags/inputs.tf @@ -14,12 +14,17 @@ variable "filter_tags_use_defaults" { } variable "filter_tags_custom" { - description = "Tags used for custom filtering when filter_tags_use_defaults is false" + description = "Tags used for custom filtering when filter_tags_use_defaults is false (i.e. \"tag1:val1,tag2:val2\")" default = "*" } +variable "filter_tags_custom_excluded" { + description = "Tags excluded for custom filtering when filter_tags_use_defaults is false (i.e. \"tag1:val1,tag2:val2\")" + default = "" +} + variable "extra_tags" { - description = "Extra optional tags (i.e. [\"tag1:val1\", \"tag2:val2\"])" + description = "Extra optional tags added for default filtering when filter_tags_use_defaults is true (i.e. [\"tag1:val1\", \"tag2:val2\"])" type = "list" default = [] } diff --git a/common/filter-tags/locals.tf b/common/filter-tags/locals.tf index fa392da..5289a3f 100644 --- a/common/filter-tags/locals.tf +++ b/common/filter-tags/locals.tf @@ -1,3 +1,4 @@ locals { - filters = "${var.filter_tags_use_defaults == "true" ? join(",", compact(concat(split(",", format("dd_monitoring:enabled,dd_%s:enabled,env:%s", var.resource, var.environment)), compact(var.extra_tags)))) : "${var.filter_tags_custom}"}" + including = "${var.filter_tags_use_defaults == "true" ? join(",", compact(concat(split(",", format("dd_monitoring:enabled,dd_%s:enabled,env:%s", var.resource, var.environment)), compact(var.extra_tags)))) : "${var.filter_tags_custom}"}" + excluding = "${join(",", compact(split(",", "${var.filter_tags_custom_excluded}")))}" } diff --git a/common/filter-tags/output.tf b/common/filter-tags/output.tf deleted file mode 100644 index d0d086b..0000000 --- a/common/filter-tags/output.tf +++ /dev/null @@ -1,9 +0,0 @@ -output "query_alert" { - description = "The full filtering pattern including parentheses for service check monitor type" - value = "{${local.filters}}" -} - -output "service_check" { - description = "The full filtering pattern including braces for query alert monitor type" - value = "(\"${replace(local.filters, ",", "\",\"")}\")" -} diff --git a/common/filter-tags/outputs.tf b/common/filter-tags/outputs.tf new file mode 100644 index 0000000..15a660a --- /dev/null +++ b/common/filter-tags/outputs.tf @@ -0,0 +1,10 @@ +output "query_alert" { + description = "The full filtering pattern including parentheses for service check monitor type" + value = "{${join(",", compact(concat(list(local.including), formatlist("!%s", compact(split(",", local.excluding))))))}}" +} + +output "service_check" { + description = "The full filtering pattern including braces for query alert monitor type" + #value = "over.(\"${replace(local.including, ",", "\",\"")}\").exclude(\"${replace(local.excluding, ",", "\",\"")}\")" + value = ".over(\"${replace(local.including, ",", "\",\"")}\")${local.excluding == "" ? "" : ".exclude(\"${replace(local.excluding, ",", "\",\"")}\")"}" +} From 34414720521b5823babaa022e43e1b20a2763503 Mon Sep 17 00:00:00 2001 From: Quentin Manfroi Date: Tue, 2 Oct 2018 14:57:04 +0200 Subject: [PATCH 2/7] MON-301 add support for excluding on event alert --- common/filter-tags/outputs.tf | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/common/filter-tags/outputs.tf b/common/filter-tags/outputs.tf index 15a660a..b1b8b5a 100644 --- a/common/filter-tags/outputs.tf +++ b/common/filter-tags/outputs.tf @@ -5,6 +5,10 @@ output "query_alert" { output "service_check" { description = "The full filtering pattern including braces for query alert monitor type" - #value = "over.(\"${replace(local.including, ",", "\",\"")}\").exclude(\"${replace(local.excluding, ",", "\",\"")}\")" value = ".over(\"${replace(local.including, ",", "\",\"")}\")${local.excluding == "" ? "" : ".exclude(\"${replace(local.excluding, ",", "\",\"")}\")"}" } + +output "event_alert" { + description = "The full filtering pattern for event alert monitor type" + value = "tags:${replace(local.including, "aaa", "aaaa")}${local.excluding == "" ? "" : " excluded_tags:${replace(local.excluding, "aa", "aaaa")}"}" +} From e5eb4734037075c8af466fa9491c651b916b7503 Mon Sep 17 00:00:00 2001 From: Quentin Manfroi Date: Tue, 2 Oct 2018 15:03:57 +0200 Subject: [PATCH 3/7] MON-301 update readme --- common/alerting-message/README.md | 1 + common/filter-tags/README.md | 7 +++++-- 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/common/alerting-message/README.md b/common/alerting-message/README.md index 49ac413..96d42f3 100644 --- a/common/alerting-message/README.md +++ b/common/alerting-message/README.md @@ -40,3 +40,4 @@ Creates a DataDog monitor alert message with the following inputs : ## Related documentation +Datadog notifications official documentation: [https://docs.datadoghq.com/monitors/notifications/?tab=is_alertis_warning](https://docs.datadoghq.com/monitors/notifications/?tab=is_alertis_warning) diff --git a/common/filter-tags/README.md b/common/filter-tags/README.md index 5f19e92..f79e8c4 100644 --- a/common/filter-tags/README.md +++ b/common/filter-tags/README.md @@ -22,14 +22,16 @@ Creates all kinds of filters tags patterns depending of the monitor type and dir * A filter tags pattern for service check * A filter tags pattern for query alert +* A filter tags pattern for event alert ## Inputs | Name | Description | Type | Default | Required | |------|-------------|:----:|:-----:|:-----:| | environment | Architecture Environment | string | - | yes | -| extra_tags | Extra optional tags (i.e. ["tag1:val1", "tag2:val2"]) | list | `` | no | -| filter_tags_custom | Tags used for custom filtering when filter_tags_use_defaults is false | string | `*` | no | +| extra_tags | Extra optional tags added for default filtering when filter_tags_use_defaults is true (i.e. ["tag1:val1", "tag2:val2"]) | list | `[]` | no | +| filter_tags_custom | Tags used for custom filtering when filter_tags_use_defaults is false (i.e. "tag1:val1,tag2:val2") | string | `*` | no | +| filter_tags_custom_excluded | Tags excluded for custom filtering when filter_tags_use_defaults is false (i.e. "tag1:val1,tag2:val2") | string | `` | no | | filter_tags_use_defaults | Use default filter tags convention | string | `true` | no | | resource | The dedicated tag for the resource | string | - | yes | @@ -37,6 +39,7 @@ Creates all kinds of filters tags patterns depending of the monitor type and dir | Name | Description | |------|-------------| +| event_alert | The full filtering pattern for event alert monitor type | | query_alert | The full filtering pattern including parentheses for service check monitor type | | service_check | The full filtering pattern including braces for query alert monitor type | From 5acbe5045cf016b8b64a9d974a497301c00d851b Mon Sep 17 00:00:00 2001 From: Quentin Manfroi Date: Tue, 2 Oct 2018 15:15:10 +0200 Subject: [PATCH 4/7] MON-301 remove useless replace for event alert output --- common/filter-tags/outputs.tf | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/common/filter-tags/outputs.tf b/common/filter-tags/outputs.tf index b1b8b5a..9eb9fc6 100644 --- a/common/filter-tags/outputs.tf +++ b/common/filter-tags/outputs.tf @@ -10,5 +10,5 @@ output "service_check" { output "event_alert" { description = "The full filtering pattern for event alert monitor type" - value = "tags:${replace(local.including, "aaa", "aaaa")}${local.excluding == "" ? "" : " excluded_tags:${replace(local.excluding, "aa", "aaaa")}"}" + value = "tags:${local.including}${local.excluding == "" ? "" : " excluded_tags:${local.excluding}"}" } From e564cf068d3811f828ee96debf99f243521fd461 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A9r=C3=B4me=20Respaut?= Date: Tue, 2 Oct 2018 15:39:11 +0200 Subject: [PATCH 5/7] MON-301: Refactor a bit terraform code with intermediate locals --- common/filter-tags/locals.tf | 7 +++++-- common/filter-tags/outputs.tf | 6 +++--- 2 files changed, 8 insertions(+), 5 deletions(-) diff --git a/common/filter-tags/locals.tf b/common/filter-tags/locals.tf index 5289a3f..9e6d8cf 100644 --- a/common/filter-tags/locals.tf +++ b/common/filter-tags/locals.tf @@ -1,4 +1,7 @@ locals { - including = "${var.filter_tags_use_defaults == "true" ? join(",", compact(concat(split(",", format("dd_monitoring:enabled,dd_%s:enabled,env:%s", var.resource, var.environment)), compact(var.extra_tags)))) : "${var.filter_tags_custom}"}" - excluding = "${join(",", compact(split(",", "${var.filter_tags_custom_excluded}")))}" + including_default_list = "${compact(concat(split(",", format("dd_monitoring:enabled,dd_%s:enabled,env:%s", var.resource, var.environment)), compact(var.extra_tags)))}" + excluding_list = "${compact(split(",", "${var.filter_tags_custom_excluded}"))}" + + including_string = "${var.filter_tags_use_defaults == "true" ? join(",", local.including_default_list) : "${var.filter_tags_custom}"}" + excluding_string = "${join(",", local.excluding_list)}" } diff --git a/common/filter-tags/outputs.tf b/common/filter-tags/outputs.tf index 9eb9fc6..f64a661 100644 --- a/common/filter-tags/outputs.tf +++ b/common/filter-tags/outputs.tf @@ -1,14 +1,14 @@ output "query_alert" { description = "The full filtering pattern including parentheses for service check monitor type" - value = "{${join(",", compact(concat(list(local.including), formatlist("!%s", compact(split(",", local.excluding))))))}}" + value = "{${join(",", compact(concat(list(local.including_string), formatlist("!%s", local.excluding_list))))}}" } output "service_check" { description = "The full filtering pattern including braces for query alert monitor type" - value = ".over(\"${replace(local.including, ",", "\",\"")}\")${local.excluding == "" ? "" : ".exclude(\"${replace(local.excluding, ",", "\",\"")}\")"}" + value = ".over(\"${replace(local.including_string, ",", "\",\"")}\")${local.excluding_string == "" ? "" : ".exclude(\"${replace(local.excluding_string, ",", "\",\"")}\")"}" } output "event_alert" { description = "The full filtering pattern for event alert monitor type" - value = "tags:${local.including}${local.excluding == "" ? "" : " excluded_tags:${local.excluding}"}" + value = "tags:${local.including_string}${local.excluding_string == "" ? "" : " excluded_tags:${local.excluding_string}"}" } From e38d382d4350db0122d1f224a3f4ac19988fa7fb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A9r=C3=B4me=20Respaut?= Date: Tue, 2 Oct 2018 15:52:07 +0200 Subject: [PATCH 6/7] MON-301: Include outputs example results in code comments --- common/filter-tags/outputs.tf | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/common/filter-tags/outputs.tf b/common/filter-tags/outputs.tf index f64a661..c70bbdc 100644 --- a/common/filter-tags/outputs.tf +++ b/common/filter-tags/outputs.tf @@ -1,13 +1,19 @@ +# Inputs example values: +#TF_VAR_environment='prod' TF_VAR_resource='nginx' TF_VAR_filter_tags_use_defaults=false TF_VAR_filter_tags_custom="tag:val,tag2:val2" TF_VAR_filter_tags_custom_excluded="excludedtag:value,exludedtag2:value2" terraform apply + +# query_alert = {tag:val,tag2:val2,!excludedtag:value,!exludedtag2:value2} output "query_alert" { description = "The full filtering pattern including parentheses for service check monitor type" value = "{${join(",", compact(concat(list(local.including_string), formatlist("!%s", local.excluding_list))))}}" } +# service_check = .over("tag:val","tag2:val2").exclude("excludedtag:value","exludedtag2:value2") output "service_check" { description = "The full filtering pattern including braces for query alert monitor type" value = ".over(\"${replace(local.including_string, ",", "\",\"")}\")${local.excluding_string == "" ? "" : ".exclude(\"${replace(local.excluding_string, ",", "\",\"")}\")"}" } +# event_alert = tags:tag:val,tag2:val2 excluded_tags:excludedtag:value,exludedtag2:value2 output "event_alert" { description = "The full filtering pattern for event alert monitor type" value = "tags:${local.including_string}${local.excluding_string == "" ? "" : " excluded_tags:${local.excluding_string}"}" From 4870c3388d43eec717ef31fcf55917d4e38d5beb Mon Sep 17 00:00:00 2001 From: Quentin Manfroi Date: Tue, 2 Oct 2018 19:54:03 +0200 Subject: [PATCH 7/7] MON-301 ignore excluded tags when filter_tags_use_defaults is true --- common/filter-tags/locals.tf | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/common/filter-tags/locals.tf b/common/filter-tags/locals.tf index 9e6d8cf..d153e77 100644 --- a/common/filter-tags/locals.tf +++ b/common/filter-tags/locals.tf @@ -1,6 +1,6 @@ locals { including_default_list = "${compact(concat(split(",", format("dd_monitoring:enabled,dd_%s:enabled,env:%s", var.resource, var.environment)), compact(var.extra_tags)))}" - excluding_list = "${compact(split(",", "${var.filter_tags_custom_excluded}"))}" + excluding_list = "${compact(split(",", var.filter_tags_use_defaults == "true" ? "" : join(",", split(",", "${var.filter_tags_custom_excluded}"))))}" including_string = "${var.filter_tags_use_defaults == "true" ? join(",", local.including_default_list) : "${var.filter_tags_custom}"}" excluding_string = "${join(",", local.excluding_list)}"