Skip to content

Instantly share code, notes, and snippets.

@anderbakk
Last active July 20, 2017 19:09
Show Gist options
  • Select an option

  • Save anderbakk/dc4027c90266e9855baf6469449fc04f to your computer and use it in GitHub Desktop.

Select an option

Save anderbakk/dc4027c90266e9855baf6469449fc04f to your computer and use it in GitHub Desktop.
Retrieve all Azure Service Bus queues and report the total number of active messages and dead letters. Read more here: http://www.anderbakk.com/simple-monitoring-of-azure-service-bus/
using System.Configuration;
using System.Threading.Tasks;
using Microsoft.ApplicationInsights;
using Microsoft.ApplicationInsights.Extensibility;
using Microsoft.Azure.WebJobs;
using Microsoft.Azure.WebJobs.Host;
using Microsoft.ServiceBus;
namespace FunctionApp
{
public static class MonitoringFunctions
{
[FunctionName("ReportMessageCount")]
public static async Task ReportMessageCount([TimerTrigger("0 */5 * * * *")]TimerInfo timer, TraceWriter log)
{
var namespaceManager = NamespaceManager.CreateFromConnectionString(ConfigurationManager.AppSettings["ConnString"]);
var queues = await namespaceManager.GetQueuesAsync();
var activeMessageCount = 0L;
var deadLetterMessageCount = 0L;
foreach (var queueDescription in queues)
{
var activeMessages = queueDescription.MessageCountDetails.ActiveMessageCount;
var deadLetterMessages = queueDescription.MessageCountDetails.DeadLetterMessageCount;
activeMessageCount += activeMessages;
deadLetterMessageCount += deadLetterMessages;
}
var telemetryClient = new TelemetryClient(new TelemetryConfiguration(ConfigurationManager.AppSettings["APPINSIGHTS_INSTRUMENTATIONKEY"]));
telemetryClient.TrackMetric(new MetricTelemetry { Name = "active_msg_sum", Sum = activeMessageCount });
telemetryClient.TrackMetric(new MetricTelemetry { Name = "dead_msg_sum", Sum = deadLetterMessageCount });
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment