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

如何通过Terraform将GCP告警策略的严重性设为Error

问题

我通过Terraform为Google Cloud Function配置告警策略,用于捕获错误场景。当前代码能过滤出ERROR级别的日志,但在GCP控制台查看该告警策略时,其严重性显示为No severity,需要将其设置为Error级别。

我的Terraform代码如下:

resource "google_monitoring_alert_policy" "my_cf_alert_policy" {
    display_name = "${var.ENVIRONMENT}_my_cf_error_alert"
    notification_channels = [
        google_monitoring_notification_channel.email_channel.name
        ]
    combiner     = "OR"
    conditions {
        display_name = "send_alert_when_severity=ERROR"
        condition_matched_log {
            filter = "resource.type=\"cloud_function\" resource.labels.function_name=\"${google_cloudfunctions_function.my_cf.name}\" severity=\"ERROR\""
        }
    }

    alert_strategy {
        auto_close = "604800s"
        notification_rate_limit {
            period = "300s"
        }
    }
}
解决方案

要让告警策略在控制台显示Error严重性,只需在google_monitoring_alert_policy资源块中添加severity字段并设置为ERROR即可。

修改后的完整代码:

resource "google_monitoring_alert_policy" "my_cf_alert_policy" {
    display_name = "${var.ENVIRONMENT}_my_cf_error_alert"
    notification_channels = [
        google_monitoring_notification_channel.email_channel.name
        ]
    combiner     = "OR"
    # 设置告警策略的显示严重性
    severity = "ERROR"
    conditions {
        display_name = "send_alert_when_severity=ERROR"
        condition_matched_log {
            filter = "resource.type=\"cloud_function\" resource.labels.function_name=\"${google_cloudfunctions_function.my_cf.name}\" severity=\"ERROR\""
        }
    }

    alert_strategy {
        auto_close = "604800s"
        notification_rate_limit {
            period = "300s"
        }
    }
}

补充说明

  • severity字段的可选值与控制台显示对应关系:
    • CRITICAL → Critical
    • ERROR → Error
    • WARNING → Warning
    • INFO → Info
    • DEFAULT → No severity
  • 你之前的日志过滤条件仅筛选触发告警的日志级别,但未配置告警策略本身的显示严重性,因此控制台默认显示No severity。添加severity = "ERROR"后即可修正该问题。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.30 18:12:32