resource "datadog_monitor" "ecs_service_check_monitor" {
  name    = "${var.environment}-${var.service_name} service {{service.name}} does not respond."
  message = <<EOF
{{#is_warning}}Warning: {{service.name}} has shown potential availability issues. Please take a look.{{/is_warning}}
{{#is_alert}}Critical Alert: {{service.name}} has potential availability issues. Immediate attention required.{{/is_alert}}
{{#is_recovery}}Recovery: {{service.name}} has recovered. No further actions are required{{/is_recovery}}

${var.slack_channel}
EOF
  type    = "service check"

  query = <<EOF
"fargate_check".over("ecs_cluster_name:${var.ecs_cluster}").by("service").last(6).count_by_status()
EOF

  monitor_thresholds {
    warning  = 3
    critical = 5
  }

  notify_audit      = false
  timeout_h         = 0
  new_group_delay   = 60
  renotify_interval = 0
  restricted_roles  = [data.datadog_role.datadog_admin_role.id]

  include_tags = true
  tags         = local.common_tags
}

resource "datadog_monitor" "ecs_cpu_utilization_monitor" {
  name    = "${var.environment}-${var.service_name} service {{servicename.name}} CPU utilization at {{value}}%. {{#is_alert}}Threshold exceeded: Set at {{threshold}}%.{{/is_alert}}{{#is_warning}}Warning threshold: Set at {{warn_threshold}}%.{{/is_warning}}"
  message = <<EOF
{{#is_warning}}Warning: CPU Utilization for {{servicename.name}} is approaching limits. Consider optimizing or scaling resources.{{/is_warning}}
{{#is_alert}}Critical Alter: CPU Utilization for {{servicename.name}} is too high. Consider optimizing or scaling resources.{{/is_alert}}
{{#is_recovery}}Recovery: CPU Utilization for {{servicename.name}} is back to normal. No further actions are required.{{/is_recovery}}

${var.slack_channel}
EOF
  type    = "metric alert"

  query = <<EOF
max(last_5m):avg:aws.ecs.cpuutilization{clustername:${var.ecs_cluster}} by {servicename} > 80
EOF


  monitor_thresholds {
    critical = 80
    warning  = 50
  }

  evaluation_delay    = 900
  new_group_delay     = 60
  notify_no_data      = false
  require_full_window = true
  renotify_interval   = 0
  notify_audit        = false
  timeout_h           = 0
  include_tags        = true
  restricted_roles    = [data.datadog_role.datadog_admin_role.id]

  tags = local.common_tags
}

resource "datadog_monitor" "ecs_memory_usage_monitor" {
  name    = "${var.environment}-${var.service_name} service {{servicename.name}} memory utilization at {{value}}%. {{#is_alert}}Threshold exceeded: Set at {{threshold}}%.{{/is_alert}}{{#is_warning}}Warning threshold: Set at {{warn_threshold}}%.{{/is_warning}}"
  message = <<EOF
{{#is_alert}}Critical Alter: Memory Usage for {{servicename.name}} is too high. Consider optimizing or scaling resources.{{/is_alert}}
{{#is_warning}}Warning: Memory Usage for {{servicename.name}} is approaching limits. Consider optimizing or scaling resources.{{/is_warning}}
{{#is_recovery}}Recovery: Memory Usage for {{servicename.name}} is back to normal. No further actions are required.{{/is_recovery}}

${var.slack_channel}
EOF
  type    = "metric alert"

  query = <<EOF
max(last_5m):avg:aws.ecs.memory_utilization{clustername:${var.ecs_cluster}} by {servicename} > 80
EOF

  monitor_thresholds {
    critical = 80
    warning  = 50
  }

  evaluation_delay    = 900
  new_group_delay     = 60
  notify_no_data      = false
  require_full_window = true
  renotify_interval   = 0
  notify_audit        = false
  timeout_h           = 0
  include_tags        = true
  restricted_roles    = [data.datadog_role.datadog_admin_role.id]

  tags = local.common_tags
}
