Skip to content

Commit

Permalink
Merge pull request #50 from dasmeta/DMVP-expression-alerts
Browse files Browse the repository at this point in the history
feat(DMVP-expression-alerts): Add new expression alert
  • Loading branch information
aghamyan44 authored Jun 23, 2023
2 parents d111f75 + 0f3865b commit 589b9c5
Show file tree
Hide file tree
Showing 7 changed files with 143 additions and 0 deletions.
2 changes: 2 additions & 0 deletions modules/alerts/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ module "alerts" {

| Name | Source | Version |
|------|--------|---------|
| <a name="module_cloudwatch_expression-alarm"></a> [cloudwatch\_expression-alarm](#module\_cloudwatch\_expression-alarm) | terraform-aws-modules/cloudwatch/aws//modules/metric-alarm | 4.3.0 |
| <a name="module_cloudwatch_log-based-metric-alarm"></a> [cloudwatch\_log-based-metric-alarm](#module\_cloudwatch\_log-based-metric-alarm) | terraform-aws-modules/cloudwatch/aws//modules/metric-alarm | 4.3.0 |
| <a name="module_cloudwatch_metric-alarm"></a> [cloudwatch\_metric-alarm](#module\_cloudwatch\_metric-alarm) | terraform-aws-modules/cloudwatch/aws//modules/metric-alarm | 4.3.0 |
| <a name="module_cloudwatch_metric-alarm_with_anomalydetection"></a> [cloudwatch\_metric-alarm\_with\_anomalydetection](#module\_cloudwatch\_metric-alarm\_with\_anomalydetection) | terraform-aws-modules/cloudwatch/aws//modules/metric-alarm | 4.3.0 |
Expand All @@ -119,6 +120,7 @@ module "alerts" {
| <a name="input_enable_alarm_actions"></a> [enable\_alarm\_actions](#input\_enable\_alarm\_actions) | Enable alarm actions alert | `bool` | `true` | no |
| <a name="input_enable_insufficient_data_actions"></a> [enable\_insufficient\_data\_actions](#input\_enable\_insufficient\_data\_actions) | Enable insufficient data actions alert | `bool` | `true` | no |
| <a name="input_enable_ok_actions"></a> [enable\_ok\_actions](#input\_enable\_ok\_actions) | Enable ok actions alert | `bool` | `true` | no |
| <a name="input_expression_alert"></a> [expression\_alert](#input\_expression\_alert) | Add multiple metrics in one alert and add expression. | `any` | `{}` | no |
| <a name="input_health_checks"></a> [health\_checks](#input\_health\_checks) | Allows to create route53 health checks and alarms on them | `list(any)` | `[]` | no |
| <a name="input_sns_topic"></a> [sns\_topic](#input\_sns\_topic) | The name of aws sns topic use as target for alarm actions | `string` | `"cloudwatch-alerts"` | no |

Expand Down
19 changes: 19 additions & 0 deletions modules/alerts/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -171,3 +171,22 @@ module "external_health_check-alarms" {
ok_actions = local.ok_actions
insufficient_data_actions = local.alarm_actions
}

module "cloudwatch_expression-alarm" {
source = "terraform-aws-modules/cloudwatch/aws//modules/metric-alarm"
version = "4.3.0"

for_each = var.expression_alert

alarm_name = each.key
alarm_description = lookup(each.value, "description", null)
comparison_operator = local.comparison_operators[each.value.equation]
threshold = each.value.threshold
evaluation_periods = 1

metric_query = each.value.metrics

alarm_actions = var.enable_alarm_actions ? local.alarm_actions : null
ok_actions = var.enable_ok_actions ? local.ok_actions : null
insufficient_data_actions = var.enable_insufficient_data_actions ? local.alarm_actions : null
}
20 changes: 20 additions & 0 deletions modules/alerts/tests/expression-alert/0-setup.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
terraform {
required_providers {
test = {
source = "terraform.io/builtin/test"
}

aws = {
source = "hashicorp/aws"
version = "~> 4.33"
}
}
}

provider "aws" {
region = "eu-central-1"
}

resource "aws_sns_topic" "tets_topic_for_alarm_actions" {
name = "tets-topic-for-alarm-actions"
}
50 changes: 50 additions & 0 deletions modules/alerts/tests/expression-alert/1-example.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
module "expression_alert" {
source = "../../"


sns_topic = aws_sns_topic.tets_topic_for_alarm_actions.name
expression_alert = {
"excl_5xx" = {
equation = "lt"
threshold = 90
metrics = [
{
id = "m5x"
period = 0
return_data = false
# metric = []
metric = [{
dimensions = { "LoadBalancer" = "app/alb-test/803783" }
metric_name = "HTTPCode_Target_5XX_Count"
namespace = "AWS/ApplicationELB"
period = 300
stat = "Average"
}
]
},
{
id = "mTotal"
period = 0
return_data = false

metric = [{
dimensions = { "LoadBalancer" = "app/alb-test/803783" }
metric_name = "RequestCount"
namespace = "AWS/ApplicationELB"
period = 300
stat = "Average"
}]
},
{
expression = "100*(mTotal-m5x)/mTotal"
id = "e1"
label = "Expression1"
period = 0
return_data = true
}
]
}
}
enable_insufficient_data_actions = false
enable_ok_actions = false
}
9 changes: 9 additions & 0 deletions modules/alerts/tests/expression-alert/2-assert.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
resource "test_assertions" "dummy" {
component = "monitoring-modules-alerts"

equal "scheme" {
description = "As module does not have any output and data just make sure the case runs. Probably can be thrown away."
got = "all good"
want = "all good"
}
}
37 changes: 37 additions & 0 deletions modules/alerts/tests/expression-alert/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
# expression-alert

<!-- BEGINNING OF PRE-COMMIT-TERRAFORM DOCS HOOK -->
## Requirements

| Name | Version |
|------|---------|
| <a name="requirement_aws"></a> [aws](#requirement\_aws) | ~> 4.33 |

## Providers

| Name | Version |
|------|---------|
| <a name="provider_aws"></a> [aws](#provider\_aws) | 4.67.0 |
| <a name="provider_test"></a> [test](#provider\_test) | n/a |

## Modules

| Name | Source | Version |
|------|--------|---------|
| <a name="module_expression_alert"></a> [expression\_alert](#module\_expression\_alert) | ../../ | n/a |

## Resources

| Name | Type |
|------|------|
| [aws_sns_topic.tets_topic_for_alarm_actions](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/sns_topic) | resource |
| test_assertions.dummy | resource |

## Inputs

No inputs.

## Outputs

No outputs.
<!-- END OF PRE-COMMIT-TERRAFORM DOCS HOOK -->
6 changes: 6 additions & 0 deletions modules/alerts/variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -70,3 +70,9 @@ variable "enable_alarm_actions" {
default = true
description = "Enable alarm actions alert"
}

variable "expression_alert" {
type = any
default = {}
description = "Add multiple metrics in one alert and add expression."
}

0 comments on commit 589b9c5

Please sign in to comment.