Instantly share code, notes, and snippets.

Embed
What would you like to do?
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