module "lambda-publishing-changelog-state-recorder" {
  source                      = "git@github.com:theorchard/terraform-lambda.git?ref=3.1.9"
  environment                 = var.environment
  lambda_name                 = var.state_recorder_lambda_name
  lambda_description          = "Lambda triggered by metadataChange Kafka topic and writes metadata changes to a database"
  lambda_function_timeout     = "900"
  use_container_image         = true
  vpc_subnet_ids              = module.vpc_info.default_private_subnet_ids
  vpc_enabled                 = true
  vpc_id                      = module.vpc_info.vpc_id
  datadog_advanced_enabled    = true
  application_family          = var.application_family
  lambda_function_memory_size = "2048"
  splitio_enabled             = true

  msk_event_enabled                     = true
  event_source_mapping_msk_cluster_name = var.trigger_msk_cluster_name
  event_source_mapping_msk_cluster_uuid = var.trigger_msk_cluster_uuid
  event_source_mapping_batch_size       = 50
  msk_topics                            = [var.pub_metadataChange_topic]

  event_source_mapping_starting_position = "LATEST"

  lambda_function_environment_variables = {
    Environment       = var.environment
    DD_LAMBDA_HANDLER = "app.handler"
    SENTRY_DSN        = module.lambda_publishing_changelog_state_recorder_lambda_name_sentry_project.sentry_key_dsn_public_output
  }

  lambda_function_reserved_concurrent_executions = 1
}

module "lambda_publishing_changelog_state_recorder_lambda_name_sentry_project" {
  source                       = "git@github.com:theorchard/terraform-sentry.git//?ref=4.1.2"
  service_name                 = var.state_recorder_lambda_name
  secrets_manager_service_name = var.state_recorder_lambda_name
  environment                  = var.environment
  platform                     = "node"
  application_family           = var.application_family
}

module "datadog_lambda_publishing_changelog_state_recorder_dashboards" {
  source                            = "git@github.com:theorchard/terraform-datadog.git//modules/lambda?ref=6.10.7"
  service_name                      = var.state_recorder_lambda_name
  environment                       = var.environment
  lambda_invocation_monitor_enabled = false
  lambda_error_monitor_enabled      = true
  notification_endpoints            = var.slack_alert_channel
}

locals {
  # Manually build filters for the offsetLag metric, we want the Consumer Ids for each MSK Source Event
  state_rec_consumer_group_ids = join(" OR ", [for c in module.lambda-publishing-changelog-state-recorder.msk_event_source_mapping_uuid : "consumer_group:${c}"])
}

resource "datadog_monitor" "lambda_state_recorder_offset_lag_monitor" {
  name               = "${var.environment}-${var.state_recorder_lambda_name}-offset-lag"
  type               = "metric alert"
  message            = "{{#is_alert}} The Lambda offset lag is too high {{/is_alert}} {{#is_alert_recovery}} Offset lag is recovering {{/is_alert_recovery}} in {{[aws.kafka.sum_offset_lag].topic}} \n Notify: ${var.slack_alert_channel}"
  query              = "avg(last_30m):aws.kafka.sum_offset_lag{environment:${var.environment} AND ${local.state_rec_consumer_group_ids}} by {topic} > 200"

  monitor_thresholds {
    critical = 200
    warning  = 175
  }

  notify_no_data = true
  no_data_timeframe = 60
  notify_audit = false
  renotify_interval = 30
  timeout_h = 0
  require_full_window = false
}
