Skip to content

Instantly share code, notes, and snippets.

@SebastianAchatz
Last active October 28, 2018 18:06
Show Gist options
  • Save SebastianAchatz/3b2bb9385100069bf7f59ca6803385e2 to your computer and use it in GitHub Desktop.
Save SebastianAchatz/3b2bb9385100069bf7f59ca6803385e2 to your computer and use it in GitHub Desktop.
Code Sample STTS - MonitorDeadLetterQueues
using System;
using System.Threading.Tasks;
using Microsoft.ApplicationInsights;
using Microsoft.ApplicationInsights.Extensibility;
using Microsoft.Azure.ServiceBus.Management;
using Microsoft.Azure.WebJobs;
using Microsoft.Extensions.Logging;
namespace MonitorDeadLetterQueue
{
public static class MonitorDeadLetterQueues
{
private static readonly TelemetryClient TelemetryClient = new TelemetryClient();
private static string key = TelemetryConfiguration.Active.InstrumentationKey
= Environment.GetEnvironmentVariable("APPINSIGHTS_INSTRUMENTATIONKEY",
EnvironmentVariableTarget.Process);
[FunctionName("MonitorDeadLetterQueues")]
public static async Task Run([TimerTrigger("0 */1 * * * *")]TimerInfo myTimer, ILogger log)
{
var serviceBusConnectionString = Environment.GetEnvironmentVariable("MonitorServiceBusQueues");
var managementClient = new ManagementClient(serviceBusConnectionString);
var queues = await managementClient.GetQueuesAsync();
foreach (var queue in queues)
{
var queueName = queue.Path;
var queueRuntimeInfo = await managementClient.GetQueueRuntimeInfoAsync(queueName);
var deadLetterMessagesCount = queueRuntimeInfo.MessageCountDetails.DeadLetterMessageCount;
TelemetryClient.TrackMetric($"DeadLetterQueue length - {queueName}", deadLetterMessagesCount);
log.LogInformation($"Queue: {queueName} (DeadLetters: {deadLetterMessagesCount})");
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment