Skip to content

Commit

Permalink
Add custom metrics
Browse files Browse the repository at this point in the history
  • Loading branch information
KamranBiglari committed Jul 12, 2023
1 parent a6674ec commit 1f7f833
Show file tree
Hide file tree
Showing 5 changed files with 120 additions and 57 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ No providers.

| Name | Source | Version |
|------|--------|---------|
| <a name="module_metric-alarm"></a> [metric-alarm](#module\_metric-alarm) | terraform-aws-modules/cloudwatch/aws//modules/metric-alarm | ~> 4.0 |
| <a name="module_metric-alarm"></a> [metric-alarm](#module\_metric-alarm) | terraform-aws-modules/cloudwatch/aws//modules/log-metric-filter | ~> 4.0 |

## Resources

Expand All @@ -56,8 +56,8 @@ No resources.
| <a name="input_alarm_name_prefix"></a> [alarm\_name\_prefix](#input\_alarm\_name\_prefix) | Prefix of alarm name | `string` | n/a | yes |
| <a name="input_current_environment"></a> [current\_environment](#input\_current\_environment) | Current environment | `string` | n/a | yes |
| <a name="input_input"></a> [input](#input\_input) | Path to yaml file | `string` | n/a | yes |
| <a name="input_loop"></a> [loop](#input\_loop) | n/a | `map` | `{}` | no |
| <a name="input_template_data"></a> [template\_data](#input\_template\_data) | values to replace in template | `map` | `{}` | no |
| <a name="input_loop"></a> [loop](#input\_loop) | n/a | `map(any)` | `{}` | no |
| <a name="input_template_data"></a> [template\_data](#input\_template\_data) | values to replace in template | `map(any)` | `{}` | no |

## Outputs

Expand Down
30 changes: 30 additions & 0 deletions example/cloudwatch.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -66,3 +66,33 @@ Cloudwatch:
Environment:
- dev
- prod

- Name: websocket-subscriptions
Status: 1
Description: Check websocket subscriptions.
Namespace: cryptofeed/websocketmonitoring
Metrics: SubscribeCount
CustomMetrics:
Patten: '{$.level = "INFO" && $.message = "Subscribed*"}'
LogGroupName: ${websocketmonitoring.log_group_name}
Dimensions:
- Name:
Value:
EvaluationPeriods: 2
Period: 60
Operator: LessThanOrEqualToThreshold
Statistic: Sum
Unit:
Threshold: 30
TreatMissingData: breaching
AlertLevel: critical
AlarmActions:
OK:
default:
dev: critical
ALARM:
default:
dev: critical
Environment:
- dev
- prod
10 changes: 9 additions & 1 deletion example/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,20 @@ module "cloudwatch-monitor" {
alarm_name_prefix = "cloudwatch-monitor"
current_environment = "dev"
template_data = {

rediscluster = {}
websocketmonitoring = module.websocketmonitoring #
}
alarm_actions = {
default = {
alarm = ""
ok = ""
}
}
}

module "websocketmonitoring" {
source = "terraform-aws-modules/cloudwatch/aws//modules/log-group"
version = "~> 3.0"
name = "/APP_NAME/websocket-monitoring"
retention_in_days = 14
}
129 changes: 77 additions & 52 deletions main.tf
Original file line number Diff line number Diff line change
@@ -1,93 +1,118 @@
variable "alarm_name_prefix" {
type = string
description = "Prefix of alarm name"
type = string
description = "Prefix of alarm name"
}

variable "input" {
type = string
description = "Path to yaml file"
type = string
description = "Path to yaml file"
}

variable "loop" {
type = map
default = {}
type = map(any)
default = {}
}

variable "current_environment" {
type = string
description = "Current environment"
type = string
description = "Current environment"
}

variable "template_data" {
type = map
description = "values to replace in template"
default = {}
type = map(any)
description = "values to replace in template"
default = {}
}

variable "alarm_actions" {
type = map(any)
description = "map of alarm actions"
default = {}
type = map(any)
description = "map of alarm actions"
default = {}
}

locals {
CloudWatch = yamldecode(templatefile(var.input, var.template_data))
CloudWatch = yamldecode(templatefile(var.input, var.template_data))
}

# CloudWatch Alarms
module "metric-alarm" {
source = "terraform-aws-modules/cloudwatch/aws//modules/metric-alarm"
version = "~> 4.0"
for_each = {for i in flatten([
for Key, Value in local.CloudWatch["Cloudwatch"]: [
for loopK, loopV in try(var.loop[Value.Loop],["default"]): {
ServiceKey = loopV
Name = Value.Name
Key = Key
Config = Value
for_each = { for i in flatten([
for Key, Value in local.CloudWatch["Cloudwatch"] : [
for loopK, loopV in try(var.loop[Value.Loop], ["default"]) : {
ServiceKey = loopV
Name = Value.Name
Key = Key
Config = Value
}
]
]) : "${i.Name}-${i.ServiceKey}" => i
if contains(i.Config.Environment, var.current_environment) || !can(i.Config.Environment)}
]) : "${i.Name}-${i.ServiceKey}" => i
if contains(i.Config.Environment, var.current_environment) || !can(i.Config.Environment) }
alarm_name = "${var.alarm_name_prefix}-${each.key}"
alarm_description = each.value.Config.Description
comparison_operator = each.value.Config.Operator
evaluation_periods = each.value.Config.EvaluationPeriods
threshold = each.value.Config.Threshold
treat_missing_data = each.value.Config.TreatMissingData

treat_missing_data = each.value.Config.TreatMissingData

namespace = can(each.value.Config.Query) ? null : each.value.Config.Namespace
metric_name = can(each.value.Config.Query) ? null : each.value.Config.Metrics
statistic = can(each.value.Config.Query) ? null : each.value.Config.Statistic
dimensions = can(each.value.Config.Query) ? null : try(tomap(each.value.Config.Dimensions),{})
period = can(each.value.Config.Query) ? null : each.value.Config.Period
metric_query = can(each.value.Config.Query) ? [for qK,qV in each.value.Config.Query : {
id = (can(qV.expression)) ? "e${qK}" : "m${qK}"
label = qV.label
return_data = try(qV.return_data,false)
expression = try(qV.expression,null)
period = try(qV.period,null)

metric = (can(qV.metric)) ? [{
namespace = qV.metric.namespace
metric_name = qV.metric.metric_name
period = qV.metric.period
stat = qV.metric.stat
unit = try(qV.metric.unit,null)
dimensions = {for dK,dV in try(qV.metric.dimensions,[]) : dK => dV}
}] : []

dimensions = can(each.value.Config.Query) ? null : try(tomap(each.value.Config.Dimensions), {})
period = can(each.value.Config.Query) ? null : each.value.Config.Period
metric_query = can(each.value.Config.Query) ? [for qK, qV in each.value.Config.Query : {
id = (can(qV.expression)) ? "e${qK}" : "m${qK}"
label = qV.label
return_data = try(qV.return_data, false)
expression = try(qV.expression, null)
period = try(qV.period, null)

metric = (can(qV.metric)) ? [{
namespace = qV.metric.namespace
metric_name = qV.metric.metric_name
period = qV.metric.period
stat = qV.metric.stat
unit = try(qV.metric.unit, null)
dimensions = { for dK, dV in try(qV.metric.dimensions, []) : dK => dV }
}] : []



}] : []


actions_enabled = can(each.value.Config.AlarmActions) ? true : false
alarm_actions = [for aK,aV in each.value.Config.AlarmActions.ALARM :
try(var.alarm_actions[aK]["alarm"][aV[var.current_environment]].arn,"")
if can(var.alarm_actions[aK]["alarm"][aV[var.current_environment]].arn)
alarm_actions = [for aK, aV in each.value.Config.AlarmActions.ALARM :
try(var.alarm_actions[aK]["alarm"][aV[var.current_environment]].arn, "")
if can(var.alarm_actions[aK]["alarm"][aV[var.current_environment]].arn)
]
ok_actions = [for aK,aV in each.value.Config.AlarmActions.OK :
try(var.alarm_actions[aK]["ok"][aV[var.current_environment]].arn,"")
if can(var.alarm_actions[aK]["ok"][aV[var.current_environment]].arn)
ok_actions = [for aK, aV in each.value.Config.AlarmActions.OK :
try(var.alarm_actions[aK]["ok"][aV[var.current_environment]].arn, "")
if can(var.alarm_actions[aK]["ok"][aV[var.current_environment]].arn)
]
}

#CloudWatch Custom Metrics
module "metric-alarm" {
source = "terraform-aws-modules/cloudwatch/aws//modules/log-metric-filter"
version = "~> 4.0"
for_each = { for i in flatten([
for Key, Value in local.CloudWatch["Cloudwatch"] : [
for loopK, loopV in try(var.loop[Value.Loop], ["default"]) : {
ServiceKey = loopV
Name = Value.Name
Key = Key
Config = Value
}
]
]) : "${i.Name}-${i.ServiceKey}" => i
if(contains(i.Config.Environment, var.current_environment) || !can(i.Config.Environment)) && can(i.Config.CustomMetrics) }

log_group_name = each.value.Config.CustomMetrics.LogGroupName

name = "${each.key}-${each.value.Config.Metrics}-metricfilter"
pattern = each.value.Config.CustomMetrics.Patten

metric_transformation_namespace = each.value.Config.Namespace
metric_transformation_name = each.value.Config.Metrics
}
2 changes: 1 addition & 1 deletion versions.tf
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
terraform {
required_version = ">= 1.0.11"
required_providers {
aws = ">= 4.8.0"
aws = ">= 4.8.0"
}
}

0 comments on commit 1f7f833

Please sign in to comment.