Kyle Briggs
Kyle Briggs

Reputation: 103

Identifying scheduled message count using python in azure service bus

I am using Python to monitor the status of our Azure Service Bus implementation, and currently using the code below to identify the message count. I use this message count to ensure that the queue is not building up, which could help identify whether the worker role (which consumes from the queue) is not functioning properly.

QueueList = bus_service.list_queues()
for queue in QueueList:
    queueDetails = bus_service.get_queue(queue.name)
    api.Metric.send(metric="ServiceBus.%s.MessageCount" % (bus_service.service_namespace), points=queueDetails.message_count, host=queue.name)

This code works great using the "queueDetails.message_count", and we feed to results to a monitoring tool, provided the messages are active messages and go out immediately. The problem arises when something is scheduled to go out at a later time, and set as a scheduled message not an active message. This throws off our monitoring since it looks like there are messages that were no sent, when in reality they are just delayed.

I have played around with this in powershell, and see that powershell, using the Microsoft.ServiceBus.dll can return a generic "message count", but also, has the ability to break that down into active, scheduled, dead letter, etc.

I know that the python SDK is just a wrapper around the API, but I am wondering if anyone has any ideas, other than "use powershell". Although I am keeping powerhsell in mind as a last resort, I would like to continue using python if possible.

Thanks for your help everyone. Hope this post has enough details. Long time StackOverflow user, first time poster.

Upvotes: 0

Views: 1410

Answers (2)

rakshith91
rakshith91

Reputation: 752

Here's how you get it in v7 https://pypi.org/project/azure-servicebus/7.0.0/#description

You have list_queues_runtime_properties in the ServiceBusAdministrationClient that returns the QueueRuntimeProperties which has total_message_count etc.

Please take a look at the documentation here https://learn.microsoft.com/en-us/python/api/azure-servicebus/azure.servicebus.management.queueruntimeproperties?view=azure-python

Upvotes: 0

Peter Pan
Peter Pan

Reputation: 24138

According to your description, based on my understanding, I think you want to get the messageCount & countDetails from the response of the Queue REST API Get of Service Bus with Resource Management, as below.

enter image description here

For programming in Python, firstly, you need to install some required packages via pip command pip install azure==2.0.0rc msrest msrestazure azure-mgmt-servicebus.

Here is my sample code.

from azure.common.credentials import ServicePrincipalCredentials
from azure.mgmt.servicebus import ServiceBusManagementClient

subscription_id = '<your-subscription-id>'

credentials = ServicePrincipalCredentials(
    client_id = '<your-client-id-registed-on-AzureAD>',
    secret = '<your-client-secret>',
    tenant = '<your-tenant-id>'
)

# Or using User/Password.
# For more details, see http://azure-sdk-for-python.readthedocs.io/en/latest/quickstart_authentication.html
# credentials = UserPassCredentials(
#            '[email protected]',      # Your user
#            'my_password',          # Your password
# )

servicebus_client = ServiceBusManagementClient(credentials, subscription_id)

resource_group = '<your-servicebus-resource-group>'
namespace_name = '<your-servicebus-namespace-name>'
queue_name = '<your-queue-name>'

queue = servicebus_client.queues.get(resource_group, namespace_name, queue_name)
print queue.message_count, queue.count_details

Meanwhile, if you got some error information about permission for doing the above operations, please refer to https://github.com/JamborYao/ArmManagement to solve it.

Upvotes: 1

Related Questions