Search code examples
gogoogle-cloud-platformmetricsgoogle-cloud-pubsub

how to specify topic name when to list metric descriptor in gcp


I am using monitoring "cloud.google.com/go/monitoring/apiv3" and "google.golang.org/genproto/googleapis/monitoring/v3" and request is

req := &monitoringpb.ListMetricDescriptorsRequest{
        Name:   fmt.Sprintf("projects/%s", t.projectId),
        Filter: "?",
}

Solution

  • Yes we can specify the filter. And for Pub Sub Topic Name, i have used the below and it works.

    "filter": 'metric.type = "pubsub.googleapis.com/topic/send_message_operation_count" AND resource.type = "pubsub_topic" AND resource.labels.topic_id = "<topic name>"'
    
    Filter: `metric.type="pubsub.googleapis.com/topic/send_message_operation_count" AND resource.type = "pubsub_topic" AND resource.labels.topic_id = "<topic_name>"`
    

    Here are a few documentation links which can help on Metric details related to pub sub, listing its descriptors or you may also try out with API Explorer to check with the required filter for undelivered messages

    Tried with below script in Python and it gives me results(project name and topic name ,interval to be changed according to your requirement):

    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)