Skip to content Skip to sidebar Skip to footer

Expected Google.protobuf.duration Got Str

I'm trying to create an alert with Cloud Functions. I have the following test alert json directly inserted into my code: alert_policy = {'conditions':[{'condition_absent':{'duratio

Solution 1:

See: MetricAbsence

It needs to be a string "900s" (probably).

Google's (increasingly?) exposing underlying Protobuf types in its APIs and these can be confusing to grok. In this case, the underlying type Duration, is one of Google's so-called Well-Known Types. Ironically named because they're often not that well-known ;-)

Google's APIs Explorer is an excellent tool for this type of diagnosis. It is exhaustive and current:

Example

In this case, I start with a Python dictionary, json.dumps to convert it to a string and then from_json it to create a monitoring_v3.AlertPolicy that's needed by 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)

Then:

gcloud alpha monitoring policies list \
--project=${PROJECT} \
--format="value(displayName)"test

Post a Comment for "Expected Google.protobuf.duration Got Str"