resource "datadog_monitor" "large_amount_sqs_messages" {
  for_each = var.create_monitors ? local.lambdas_sqs_queues : {}

  evaluation_delay    = 900 # Recommended for AWS metrics
  include_tags        = false
  on_missing_data     = "show_and_notify_no_data"
  require_full_window = false
  monitor_thresholds {
    critical = each.value.message_amount_threshold
  }
  name    = "AMPSV Team | ${var.name_prefix} | ${each.key} | Large amount of messages in {{queuename}}"
  type    = "query alert"
  tags = concat(
    [local.managed_by_terraform_datadog_tag],
    [local.email_notifications_datadog_tag],
    ["team:aoma-core-team"],
    ["env:${var.environment}"]
  )
  query   = <<-EOT
    max(last_15m):avg:aws.sqs.approximate_number_of_messages_not_visible{queuename:${each.value.queue_prefix}-${var.environment}*.fifo} by {queuename} + avg:aws.sqs.approximate_number_of_messages_visible{queuename:${each.value.queue_prefix}-${var.environment}*.fifo} by {queuename} > ${each.value.message_amount_threshold}
    EOT
  message = <<-EOT
    Queue: {{queuename}}
    Threshold: {{threshold}}
    Current amount of messages: {{value}}

    {{#is_alert}}
    There is a big amount of visible and/or invisible messages in a queue, this might indicate that consumer of those messages is down. Please investigate.
    {{/is_alert}}
    {{#is_no_data}}
    There is no data to evaluate this monitor, this might indicate problem with AWS or monitor itself. Please investigate.
    {{/is_no_data}}
    EOT
}

resource "datadog_monitor" "old_message_present_in_sqs" {
  for_each = var.create_monitors ? local.lambdas_sqs_queues : {}

  evaluation_delay    = 900 # Recommended for AWS metrics
  include_tags        = false
  on_missing_data     = "show_and_notify_no_data"
  require_full_window = false
  monitor_thresholds {
    critical = each.value.old_message_threshold
  }
  name    = "AMPSV Team | ${var.name_prefix} | ${each.key} | Message with big age present in {{queuename}}"
  type    = "query alert"
  tags = concat(
    [local.managed_by_terraform_datadog_tag],
    [local.email_notifications_datadog_tag],
    ["team:aoma-core-team"],
    ["env:${var.environment}"]
  )
  query   = <<-EOT
    avg(last_5m):max:aws.sqs.approximate_age_of_oldest_message{queuename:${each.value.queue_prefix}-${var.environment}*.fifo} by {queuename} > ${each.value.old_message_threshold}
    EOT
  message = <<-EOT
    Queue: {{queuename}}
    Threshold: {{threshold}} seconds
    Current age of oldest message: {{value}} seconds

    {{#is_alert}}
    There is a message that has a big age in a queue. This might indicate that consumer cannot process this message. Please investigate.
    {{/is_alert}}
    {{#is_no_data}}
    There is no data to evaluate this monitor, this might indicate problem with AWS or monitor itself. Please investigate.
    {{/is_no_data}}
    EOT
}
