在Azure Service Bus中获取邮件统计信息

时间:2016-07-19 15:06:05

标签: c# azure azureservicebus azure-servicebus-topics azure-servicebus-subscriptions

我正在编写一个实用程序来监控我们的Azure Service Bus主题和订阅。

我可以获取主题详细信息,例如姓名,排队消息计数和死信消息计数,但我想获取已处理的消息数。

以下是我正在使用的代码:

var sub = namespaceManager.GetSubscription(topicPath, subscriptionName);

var name = sub.Name;
var pending= sub.MessageCountDetails.ActiveMessageCount;
var deadletter = sub.MessageCountDetails.DeadLetterMessageCount

似乎GetSubscription不包含任何属性来获取处理的邮件数。

以前有人试过这样做吗?

3 个答案:

答案 0 :(得分:2)

要从Azure Servicebus实体获取消息统计信息,我使用Visual Studio App Insights。这是一个监控应用程序的工具。基本上,您的应用程序会向App Insights发送事件,并且您可以从Azure门户create dashboards向您提供有关应用程序的实时信息。

要监控Azure Servicebus实体,我从我的应用程序发送自定义事件:

  • 您可以查看pricing,有一个免费的计划,允许您每月发送多达5百万个自定义事件。如果您需要发送超过5百万个事件,则可以在将事件发送到App Insights之前为每个Servicebus实体或聚合计数创建App Insights。
  • 您可以访问原始数据7天,汇总数据90天。

  • 如果您使用Power BI,则可以配置continuous export数据(不要认为它在免费套餐中可用)。

  • 其他很酷的事情,您可以发送例外情况和create alert from App Insigths,只要收到App Insigths的例外情况,就会向您发送电子邮件。

如果你从webjob / worker角色/控制台app / windows服务处理servicebus消息,这篇文章可能是一个神的起点:

因此,在从Azure门户创建App Insights后,您将获得 InstrumentationKey

您可以从nuget安装ApplicationInsights

要将事件发送到App Insights,您需要实例化TelemetryClient。 Microsoft建议每个应用程序只有一次遥测客户端实例,并在应用程序停止或重新启动时刷新TelemetryClient:

var telemetryClient = new TelemetryClient()
    { InstrumentationKey = "MyInstrumentationKey" };

所以这是一个非常基本的例子,但你会明白:

// Get the message
BrokeredMessage message = ...

try
{
    // Process you message
    ...

    // Delete the message from the queue when it is ok.
    message.Complete();

    // Create and send an event to app insights
    var eventTelemetry = new EventTelemetry { Name = "MyQueueName" };
    eventTelemetry.Metrics["MessageCount"] = 1;
    telemetryClient.TrackEvent(eventTelemetry);
}
catch (Exception ex)
{
    // Send back the message to the queue ??? depends if you'd like to re-process it
    message.Abandon();

    // Send the exception to app insights
    telemetryClient.TrackException(ex);
}

使用此代码,您将在App Insights中创建一个名为MyQueueName的新事件。您可以创建仪表板并过滤此事件并显示 MessageCount指标。我使用度量标准,因为在更复杂的情况下,您可以每x分钟发送一个事件,并将MessageCount设置为在此时间间隔内处理的消息数。

我在这里使用的是App洞察,但我很确定你可以使用其他工具做同样的事情:

希望这会对你有帮助!

答案 1 :(得分:1)

借助最新的Azure Monitor Metrics,可以获取主题中的邮件总数,传入消息,传出消息。对于您来说,外发邮件数就是已处理邮件的数量。

答案 2 :(得分:0)

自创建实体以来处理过的消息数量?还是从某个日期开始?那么多次处理的消息(交付次数> 1)因为客户端未能按时或其他原因完成?这个计数并不是直截了当的,这就是为什么没有开箱即用的原因。

相关问题