Go 在gcp中列出度量描述符时如何指定主题名称
我正在使用监控“cloud.google.com/go/monitoring/apiv3”和“google.golang.org/genproto/googleapis/monitoring/v3”,请求是Go 在gcp中列出度量描述符时如何指定主题名称,go,google-cloud-platform,metrics,google-cloud-pubsub,Go,Google Cloud Platform,Metrics,Google Cloud Pubsub,我正在使用监控“cloud.google.com/go/monitoring/apiv3”和“google.golang.org/genproto/googleapis/monitoring/v3”,请求是 req := &monitoringpb.ListMetricDescriptorsRequest{ Name: fmt.Sprintf("projects/%s", t.projectId), Filter: "?&
req := &monitoringpb.ListMetricDescriptorsRequest{
Name: fmt.Sprintf("projects/%s", t.projectId),
Filter: "?",
}
是的,我们可以指定过滤器。对于Pub子主题名称,我使用了下面的名称,它是有效的
"filter": 'metric.type = "pubsub.googleapis.com/topic/send_message_operation_count" AND resource.type = "pubsub_topic" AND resource.labels.topic_id = "<topic name>"'
“过滤器”:“metric.type=“pubsub.googleapis.com/topic/send\u message\u operation\u count”和resource.type=“pubsub\u topic”和resource.labels.topic\u id=“”
过滤器:`metric.type=“pubsub.googleapis.com/topic/send_message_operation_count”和resource.type=“pubsub_topic”和resource.labels.topic_id=“”`
这里有一些文档链接可以帮助您,或者您也可以尝试使用来检查所需的过滤器
在Python中使用下面的脚本进行了尝试,它给出了结果(项目名称和主题名称,间隔将根据您的需求进行更改):
import argparse
导入操作系统
导入pprint
导入时间
导入uuid
从google.api导入标签_pb2作为ga_标签
从google.api导入度量_pb2作为ga_度量
从google.cloud导入监控_v3
client=monitoring\u v3.MetricServiceClient()
project_name=“projects/”
间隔=监控_v3.TimeInterval()
now=time.time()
秒=整数(现在)
纳米=整数((现在-秒)*10**9)
间隔=监控时间间隔(
{
“结束时间”:{“秒”:秒,“纳米”:纳米},
“开始时间”:{“秒”:(秒-36000000),“纳米”:纳米},
}
)
结果=client.list\u时间序列(
请求={
“名称”:项目名称,
“过滤器”:“metric.type=“pubsub.googleapis.com/topic/send_message_operation_count”和resource.type=“pubsub_topic”和resource.labels.topic_id=“”,
“间隔”:间隔,
“视图”:监控_v3.ListTimeSeriesRequest.TimeSeriesView.FULL,
}
)
对于结果中的结果:
打印(结果)
它是在过滤器中完成的吗?还想再添加一点,ListMetricDescriptorRequest只会为我们提供指定过滤器的指标列表。在本例中,pubsub.googleapis.com/topic/byte_cost等,感谢您的回答。但它似乎不支持在这个指标中添加特定的主题。我只想在一个特定的topicHi Tom下监视未交付的消息,所以如果我理解您的需求是读取特定资源的度量数据(在本例中为pubsub主题),我认为这可以通过引用来完成-此示例显示了监视实例的cpu利用率。与此类似,如果有用于监视主题的未送达消息的内置度量,我们可以使用,否则选项似乎需要创建自己的自定义度量。是的,但我只是尝试使用类似的筛选器,但它不起作用:“筛选器:fmt.Sprintf(“resource.subscription\u id=\%s\”,topic.Name)”@汤姆·王——也许你可以这样试试。过滤器:fmt.Sprintf(“metric.type=\%s\”,metricType)或过滤器:metric.type=“compute.googleapis.com/instance/cpu/utilization”
。对于特定实例,筛选器可以类似于metric.type=“compute.googleapis.com/instance/cpu/usage\u time”和(metric.label.instance\u name=“您的实例id”或metric.label.instance\u name=“您的其他实例id”)。您可以相应地更改发布子资源
Filter: `metric.type="pubsub.googleapis.com/topic/send_message_operation_count" AND resource.type = "pubsub_topic" AND resource.labels.topic_id = "<topic_name>"`
import argparse
import os
import pprint
import time
import uuid
from google.api import label_pb2 as ga_label
from google.api import metric_pb2 as ga_metric
from google.cloud import monitoring_v3
client = monitoring_v3.MetricServiceClient()
project_name = "projects/<project name>"
interval = monitoring_v3.TimeInterval()
now = time.time()
seconds = int(now)
nanos = int((now - seconds) * 10 ** 9)
interval = monitoring_v3.TimeInterval(
{
"end_time": {"seconds": seconds, "nanos": nanos},
"start_time": {"seconds": (seconds - 36000000), "nanos": nanos},
}
)
results = client.list_time_series(
request={
"name": project_name,
"filter": 'metric.type = "pubsub.googleapis.com/topic/send_message_operation_count" AND resource.type = "pubsub_topic" AND resource.labels.topic_id = "<topicname>"',
"interval": interval,
"view": monitoring_v3.ListTimeSeriesRequest.TimeSeriesView.FULL,
}
)
for result in results:
print(result)