locals {
  # For the 7 day cost change monitor, only alert if the cost change for a given SKU is greater than 20% and greater than $200.
  # For the 30 day cost change monitor, only alert if the cost change for a given SKU is greater than 20% and greater than $500.
  cost_change_absolute_threshold_7_days  = 200
  cost_change_absolute_threshold_30_days = 500
  cost_change_critical_threshold_pct     = 20

  # For the cost anomaly monitor, only alert if the anomaly for a given SKU is greater than $100.
  cost_anomaly_absolute_threshold = 100
  cost_anomaly_critical_threshold = 2

  # For products which are billed by number of committers or on a monthly cycle, monitor cost change over a 30 day period rather than 7 day.
  # This is because 7-day costs increase over the course of the month as the number of committers increases.
  committer_billing_dimensions  = ["ci_visibility_committers", "code_analysis_sa_sca_bundle", "test_visibility_committers"]
  committer_dimensions_excluded = join(" AND ", [for d in local.committer_billing_dimensions : "NOT dimension:${d}"])
  committer_dimensions_included = join(" OR ", [for d in local.committer_billing_dimensions : "dimension:${d}"])
}

resource "datadog_monitor" "datadog_cost_change_monitor_7d" {
  name                = "Datadog Cost Change Monitor (7 Days)"
  type                = "cost alert"
  query               = "formula(\"query1\").last(\"7d\").change(\"relative\", ${local.cost_change_absolute_threshold_7_days}) > ${local.cost_change_critical_threshold_pct}"
  message             = <<EOT
A signficant increase in costs has been detected for Datadog product {{datadog_product.name}}:{{dimension.name}} over the last 7 days. Notify: @slack-The_Orchard-datadog-usage-alerts.
EOT
  require_full_window = false
  monitor_thresholds {
    critical = local.cost_change_critical_threshold_pct
  }
  variables {
    cloud_cost_query {
      data_source = "cloud_cost"
      name        = "query1"
      query       = "sum:datadog.cost.amortized{${local.committer_dimensions_excluded}} by {datadog_product,dimension}.rollup(sum, daily)"
      aggregator  = "sum"
    }
  }

  tags = local.common_tags
}

resource "datadog_monitor" "datadog_cost_change_monitor_30d" {
  name                = "Datadog Cost Change Monitor (30 Days)"
  type                = "cost alert"
  query               = "formula(\"query1\").last(\"30d\").change(\"relative\", ${local.cost_change_absolute_threshold_30_days}) > ${local.cost_change_critical_threshold_pct}"
  message             = <<EOT
A signficant increase in costs has been detected for Datadog product {{datadog_product.name}}:{{dimension.name}} over the last 30 days. Notify: @slack-The_Orchard-datadog-usage-alerts.
EOT
  require_full_window = false
  monitor_thresholds {
    critical = local.cost_change_critical_threshold_pct
  }
  variables {
    cloud_cost_query {
      data_source = "cloud_cost"
      name        = "query1"
      query       = "sum:datadog.cost.amortized{${local.committer_dimensions_included}} by {datadog_product,dimension}.rollup(sum, daily)"
      aggregator  = "sum"
    }
  }

  tags = local.common_tags
}

resource "datadog_monitor" "datadog_cost_anomaly_monitor" {
  name                = "Datadog Cost Anomaly Monitor"
  type                = "cost alert"
  query               = "formula(\"query1\").last(\"7d\").anomaly(direction=\"above\", threshold=${local.cost_anomaly_absolute_threshold}) >= ${local.cost_anomaly_critical_threshold}"
  message             = <<EOT
Anomalous spend has been detected for Datadog product {{datadog_product.name}}:{{dimension.name}}. Notify: @slack-The_Orchard-datadog-usage-alerts.
EOT
  require_full_window = false
  monitor_thresholds {
    critical = local.cost_anomaly_critical_threshold
  }
  variables {
    cloud_cost_query {
      data_source = "cloud_cost"
      name        = "query1"
      query       = "sum:datadog.cost.amortized{cost_type IN (usage) AND ${local.committer_dimensions_excluded}} by {datadog_product,dimension}.rollup(sum, daily)"
      aggregator  = "sum"
    }
  }

  tags = local.common_tags
}
