预期 google.protobuf.Duration 得到了 str
expected google.protobuf.Duration got str
我正在尝试使用 Cloud Functions 创建警报。我将以下测试警报 json 直接插入到我的代码中:
alert_policy = {"conditions":[{"condition_absent":{"duration":900s,"filter":"metric.label.state=\"blocked\" AND metric.type=\"agent.googleapis.com/processes/count_by_state\" AND resource.type=\"gce_instance\""},"displayName":"Test_two"}],"displayName":"test","combiner":"OR"}
由于 duration
值,我无法使我的功能正常工作。我尝试以字符串、整数、带 s 的字符串的形式传递,但总是出现错误:
TypeError: Parameter to MergeFrom() must be instance of same class: expected google.protobuf.Duration got str.
或 TypeError: Parameter to MergeFrom() must be instance of same class: expected google.protobuf.Duration got int.
我应该如何传递这个值?
需要是字符串"900s"
(大概)。
Google(越来越多?)在其 API 中公开底层 Protobuf 类型,这些可能会使 grok 感到困惑。在这种情况下,基础类型 Duration, is one of Google's so-called Well-Known Types。具有讽刺意味的是,它们的名字通常并不那么出名 ;-)
Google 的 APIs Explorer 是此类诊断的绝佳工具。它详尽且最新:
例子
在这种情况下,我从 Python 字典开始,json.dumps
将其转换为字符串,然后 from_json
创建一个 monitoring_v3.AlertPolicy
create_alert_policy
import json
import os
from google.cloud import monitoring_v3
PROJECT = os.environ["PROJECT"]
client = monitoring_v3.AlertPolicyServiceClient()
name = "projects/{project}".format(project=PROJECT)
filter = "..."
j = {
"displayName": "test",
"conditions": [{
"displayName": "test",
"condition_absent": {
"filter": filter,
"duration": "900s",
},
}],
"combiner": "OR"
}
policy = monitoring_v3.AlertPolicy.from_json(json.dumps(j))
resp = client.create_alert_policy(name=name,alert_policy=policy)
print(resp)
然后:
gcloud alpha monitoring policies list \
--project=${PROJECT} \
--format="value(displayName)"
test
我正在尝试使用 Cloud Functions 创建警报。我将以下测试警报 json 直接插入到我的代码中:
alert_policy = {"conditions":[{"condition_absent":{"duration":900s,"filter":"metric.label.state=\"blocked\" AND metric.type=\"agent.googleapis.com/processes/count_by_state\" AND resource.type=\"gce_instance\""},"displayName":"Test_two"}],"displayName":"test","combiner":"OR"}
由于 duration
值,我无法使我的功能正常工作。我尝试以字符串、整数、带 s 的字符串的形式传递,但总是出现错误:
TypeError: Parameter to MergeFrom() must be instance of same class: expected google.protobuf.Duration got str.
或 TypeError: Parameter to MergeFrom() must be instance of same class: expected google.protobuf.Duration got int.
我应该如何传递这个值?
需要是字符串"900s"
(大概)。
Google(越来越多?)在其 API 中公开底层 Protobuf 类型,这些可能会使 grok 感到困惑。在这种情况下,基础类型 Duration, is one of Google's so-called Well-Known Types。具有讽刺意味的是,它们的名字通常并不那么出名 ;-)
Google 的 APIs Explorer 是此类诊断的绝佳工具。它详尽且最新:
例子
在这种情况下,我从 Python 字典开始,json.dumps
将其转换为字符串,然后 from_json
创建一个 monitoring_v3.AlertPolicy
create_alert_policy
import json
import os
from google.cloud import monitoring_v3
PROJECT = os.environ["PROJECT"]
client = monitoring_v3.AlertPolicyServiceClient()
name = "projects/{project}".format(project=PROJECT)
filter = "..."
j = {
"displayName": "test",
"conditions": [{
"displayName": "test",
"condition_absent": {
"filter": filter,
"duration": "900s",
},
}],
"combiner": "OR"
}
policy = monitoring_v3.AlertPolicy.from_json(json.dumps(j))
resp = client.create_alert_policy(name=name,alert_policy=policy)
print(resp)
然后:
gcloud alpha monitoring policies list \
--project=${PROJECT} \
--format="value(displayName)"
test