在azure服务总线中使用python识别计划消息计数

在azure服务总线中使用python识别计划消息计数,python,python-2.7,azure,azureservicebus,Python,Python 2.7,Azure,Azureservicebus,我正在使用Python监视Azure服务总线实现的状态,目前正在使用下面的代码来识别消息计数。我使用此消息计数来确保队列没有建立,这有助于确定从队列中使用的工作人员角色是否正常工作 QueueList = bus_service.list_queues() for queue in QueueList: queueDetails = bus_service.get_queue(queue.name) api.Metric.send(metric="ServiceBus.%s.Me

我正在使用Python监视Azure服务总线实现的状态,目前正在使用下面的代码来识别消息计数。我使用此消息计数来确保队列没有建立,这有助于确定从队列中使用的工作人员角色是否正常工作

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)
这段代码使用queueDetails.message_计数非常有效,我们将结果馈送到监控工具,前提是这些消息是活动消息并立即发出。当某个消息被安排在以后的某个时间发出,并且被设置为计划消息而不是活动消息时,就会出现问题。这使我们无法进行监视,因为看起来有些消息没有发送,而实际上它们只是被延迟了

我在powershell中对此进行了研究,发现powershell使用Microsoft.ServiceBus.dll可以返回通用消息计数,但也可以将其分解为活动、计划、死信等

我知道PythonSDK只是API的包装器,但我想知道除了使用powershell之外,是否还有人有其他想法。尽管我将powerhsell作为最后手段牢记在心,但如果可能的话,我希望继续使用python


谢谢大家的帮助。希望这篇文章有足够的细节。长时间StackOverflow用户,第一次海报。

根据您的描述,根据我的理解,我认为您希望从具有资源管理的Service Bus的队列REST API的响应中获取messageCount&countDetails,如下所示

对于Python编程,首先需要通过pip命令pip install azure==2.0.0rc msrest msrestazure azure mgmt servicebus安装一些必需的软件包

这是我的示例代码

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(
#            'user@domain.com',      # 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

同时,如果您获得了有关执行上述操作的权限的一些错误信息,请参阅以解决它。

以下是如何在v7中获得它的

ServiceBusAdministrationClient中有列表队列运行时属性,该属性返回队列运行时属性,其中包含消息总数等

请看一下这里的文档

谢谢@peter pan msft。看起来队列详细信息在ASM SDK中不可用,并且仅在ARM中可用。虽然上面的代码可以工作,但我的问题是订阅当前未使用active directory域。当我尝试使用服务主体方法或凭据时,这是一个问题。@KyleBriggs有三种方法:使用服务主体、AD用户/密码或ADAL for Python。如果未使用Azure广告,请详细更新内容。或者,您可能需要创建一个新的线程来讨论如何为ARM使用其他域。