Search code examples
pythongoogle-cloud-platformstackdrivergoogle-cloud-stackdriver

How can I get the number of undelivered messages (metric api) present in Pubsub using python client library?


I am using Pubsub as a queuing mechanism tool and want to know the count of the messages residing inside the topic of Pubsub. For the same purpose, I have decided to use the Google API metric pubsub.googleapis.com/subscription/num_undelivered_messages but I am unable to figure out how can this be achieved using python client library monitoring_v3.

from google.cloud import monitoring_v3
import time,os
os.environ["GOOGLE_APPLICATION_CREDENTIALS"]="/key.json"

client = monitoring_v3.MetricServiceClient()
project = 'project_name'
project_name = client.project_path(project)
metric_type = "pubsub.googleapis.com/subscription/num_undelivered_messages"

Can you please guide me on how to proceed further and query this google api metric named as num_undelivered_messages?


Solution

  • This works for me but I am not sure if it's the creator's intention.

    from google.cloud import monitoring_v3
    from google.cloud.monitoring_v3 import query
    
    project = "..."
    client = monitoring_v3.MetricServiceClient()
    result = query.Query(
        client,
        project,
        'pubsub.googleapis.com/subscription/num_undelivered_messages',
        minutes=1).as_dataframe()