You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在Terraform中为Google Monitoring告警策略过滤特定Cloud Tasks队列ID

实现Cloud Tasks特定队列的告警过滤

要过滤特定Cloud Tasks队列ID,只需在告警条件的filter字段中添加队列ID的匹配条件,利用Google Cloud Monitoring的过滤语法结合资源标签即可实现。

修改后的Terraform代码

固定队列ID的情况

如果目标队列ID是固定值,直接在filter中追加条件:

resource "google_monitoring_alert_policy" "cloud-tasks-slides-queue-is-too-large" {
    for_each = {for alert in local.metric_based_alerts: alert.displayname => alert}

    display_name = each.value.displayname
    combiner = "OR"
    conditions {
      display_name = "Condition"
      condition_threshold {
          # 追加队列ID过滤条件
          filter          = "cloudtasks.googleapis.com/queue/depth AND resource.label.queue_id = \"你的队列ID\""
          comparison      = each.value.comparison
          threshold_value = each.value.threshold_value
          duration        = each.value.duration
          trigger {
              count = 1
          }
      }
    }
    notification_channels = each.value.notification_channels
  }

动态传入队列ID的情况

如果需要通过local.metric_based_alerts动态指定不同队列的告警,可先在local变量中添加queue_id字段,再通过插值引用:

# 示例local变量定义(需根据实际调整)
locals {
  metric_based_alerts = [
    {
      displayname          = "Slides队列深度告警"
      comparison           = "COMPARISON_GT"
      threshold_value      = 100
      duration             = "60s"
      notification_channels = ["projects/your-project/notificationChannels/xxx"]
      queue_id             = "target-queue-1" # 添加队列ID字段
    }
  ]
}

resource "google_monitoring_alert_policy" "cloud-tasks-slides-queue-is-too-large" {
    for_each = {for alert in local.metric_based_alerts: alert.displayname => alert}

    display_name = each.value.displayname
    combiner = "OR"
    conditions {
      display_name = "Condition"
      condition_threshold {
          # 动态引用队列ID
          filter          = "cloudtasks.googleapis.com/queue/depth AND resource.label.queue_id = \"${each.value.queue_id}\""
          comparison      = each.value.comparison
          threshold_value = each.value.threshold_value
          duration        = each.value.duration
          trigger {
              count = 1
          }
      }
    }
    notification_channels = each.value.notification_channels
  }

关键说明

  • Google Cloud Monitoring的过滤语法支持通过resource.label.queue_id匹配Cloud Tasks队列的ID,该标签是Cloud Tasks队列的内置标签。
  • 若需要同时匹配多个队列,可使用IN操作符,例如:resource.label.queue_id IN ("queue-1", "queue-2")

内容的提问来源于stack exchange,提问作者Omer Eshed

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.30 05:45:05