################
# Alerting — Datadog monitors for API Gateway
#
# ECS service health (server, platform, runner, search) is already monitored
# by the terraform-datadog service dashboard modules in each service file,
# all routing to @slack-coda-monitoring.
#
# This file adds API Gateway monitors for metrics not covered by those modules.
################

locals {
  create_alerting      = !local.is_ephemeral
  api_gateway_api_name = "${var.environment}-${var.service_name}-api"
}

resource "datadog_monitor" "api_gateway_5xx" {
  count   = local.create_alerting ? 1 : 0
  name    = "[${var.environment}] ${var.service_name} API Gateway 5xx error rate"
  type    = "metric alert"
  message = <<-EOT
    API Gateway returning elevated 5xx errors.

    {{#is_alert}}5xx count exceeded threshold for 5+ minutes.{{/is_alert}}
    {{#is_recovery}}5xx error rate has returned to normal.{{/is_recovery}}

    ${local.datadog_escalation_notification_endpoints}
  EOT

  query = "sum(last_5m):sum:aws.apigateway.5xx{apiname:${local.api_gateway_api_name}}.as_count() > 50"

  monitor_thresholds {
    critical = 50
    warning  = 20
  }

  notify_no_data    = false
  renotify_interval = 60
  include_tags      = true

  tags = [
    "env:${var.environment}",
    "service:${var.service_name}",
    "team:coda",
  ]
}

resource "datadog_monitor" "api_gateway_latency" {
  count   = local.create_alerting ? 1 : 0
  name    = "[${var.environment}] ${var.service_name} API Gateway p99 latency"
  type    = "metric alert"
  message = <<-EOT
    API Gateway p99 latency is elevated.

    {{#is_alert}}p99 latency exceeded 10s for 5+ minutes.{{/is_alert}}
    {{#is_recovery}}Latency has returned to normal.{{/is_recovery}}

    ${local.datadog_escalation_notification_endpoints}
  EOT

  query = "avg(last_5m):avg:aws.apigateway.latency.p99{apiname:${local.api_gateway_api_name}} > 10000"

  monitor_thresholds {
    critical = 10000
    warning  = 5000
  }

  notify_no_data    = false
  renotify_interval = 60
  include_tags      = true

  tags = [
    "env:${var.environment}",
    "service:${var.service_name}",
    "team:coda",
  ]
}
