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

使用Cloud Function Python创建Alert Policy出现位置参数错误

问题原因
  • 你使用的google-cloud-monitoring Python库的create_alert_policy方法仅支持最多2个位置参数:第一个是类实例自身(self),第二个是可选的请求配置对象,不能直接把项目名和告警策略作为两个独立的位置参数传入。
  • 你的代码还存在语法问题:告警策略配置中conditionAbsent下的filter键没有加引号,会触发语法报错。
解决方法

将create_alert_policy的入参改为关键字参数形式传入,同时修正未加引号的filter键即可,修改后的代码如下:

from google.cloud import monitoring_v3
import json

client = monitoring_v3.AlertPolicyServiceClient()

alert_policy = {
        "displayName": "This is a test",
        "combiner": "OR",
        "conditions": [
            {
                "displayName": "This is a test",
                "conditionAbsent": {
                    "duration": "3900s",
                    "filter": "resource.type = \"l7_lb_rule\" AND metric.type = \"logging.googleapis.com/user/metric_name_here\""
                }
            }
        ],
    }

name = 'projects/my-project'
# 改为关键字参数传入
client.create_alert_policy(
    name=name,
    alert_policy=alert_policy
)

你也可以选择把所有参数封装到一个request字典中作为唯一的位置参数传入,写法如下:

request = {
    "name": name,
    "alert_policy": alert_policy
}
client.create_alert_policy(request)

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.10.07 08:15:01