使用Cloud Function Python创建Alert Policy出现位置参数错误
问题原因
- 你使用的
google-cloud-monitoringPython库的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
相关产品推荐
相关产品推荐

