resource "datadog_monitor" "ksqldb_query_deploy_errors_monitor" {
  count   = var.datadog_monitor_enabled ? 1 : 0
  name    = "${var.environment}-${var.service_name}: Query Deploy Errors monitor"
  type    = "log alert"
  message = <<EOF
Error_code: {{log.attributes.error_code}}

Error_type: {{log.attributes.@type}}

Error: {{log.message}}

Query File: {{log.attributes.query_file}}

Notify: @slack-kafka-data-highway-alerts
EOF

  query = "logs(\"source:${var.environment}-${var.service_name} @@type:statement_error\").index(\"*\").rollup(\"count\").last(\"10m\") >= ${var.datadog_monitor_threshold}"
  monitor_thresholds {
    critical = var.datadog_monitor_threshold
  }

  enable_logs_sample  = false
  notify_no_data      = false
  notify_audit        = false
  renotify_interval   = 60
  timeout_h           = 0
  require_full_window = false

  tags = local.combined_resource_tags
}

resource "datadog_monitor" "ksqldb_stream_processing_errors_monitor" {
  count   = var.datadog_monitor_enabled ? 1 : 0
  name    = "${var.environment}-${var.service_name}: Stream Processing Errors monitor"
  type    = "log alert"
  message = "Notify: @slack-kafka-data-highway-alerts"

  query = "logs(\"source:${var.environment}-${var.service_name} processing.* ERROR\").index(\"*\").rollup(\"count\").last(\"5m\") >= ${var.datadog_monitor_threshold}"

  monitor_thresholds {
    critical = var.datadog_monitor_threshold
  }

  enable_logs_sample  = true
  notify_no_data      = false
  notify_audit        = false
  renotify_interval   = 60
  timeout_h           = 0
  require_full_window = false

  tags = local.combined_resource_tags
}
