locals {
  traffic_threshold = 3 * pow(10, 12) # 1 TB
  nat_gws           = formatlist("qa-delphi-nat-%s", ["1"])
}

data "aws_nat_gateway" "delphi_nat" {
  count = length(local.nat_gws)
  tags = {
    "Name" = local.nat_gws[count.index]
  }
}

resource "datadog_monitor" "delphi_nat" {
  name    = "${var.environment}-${var.service_name} Weekly NAT Traffic. {{#is_alert}}Threshold exceeded: Set at ${local.traffic_threshold / pow(10, 12)} TB.{{/is_alert}}"
  type    = "query alert"
  message = <<EOT
{{#is_alert}}Traffic summary > ${local.traffic_threshold / pow(10, 12)} TB for the last week on nat gateway: {{natgatewayid.name}}{{/is_alert}}
${var.slack_channels["infra"]}
EOT

  query = "max(last_4h):max:custom.natgateway.BytesOutToSourceSummary{natgatewayid in (${join(",", data.aws_nat_gateway.delphi_nat[*].id)})} by {natgatewayid} > ${local.traffic_threshold}"

  monitor_thresholds {
    critical = local.traffic_threshold
  }

  evaluation_delay = 1200 // minimum recommended value for GCP metrics 300

  notify_no_data    = false
  renotify_interval = 3600
  restricted_roles  = [data.datadog_role.datadog_admin_role.id]

  notify_audit = false
  timeout_h    = 0
  include_tags = true

  # ignore any changes in silenced value; using silenced is deprecated in favor of downtimes
  lifecycle {
    #ignore_changes = [silenced]
  }

  tags = local.common_tags
}
