-
-
Save SebastianAchatz/3b2bb9385100069bf7f59ca6803385e2 to your computer and use it in GitHub Desktop.
Code Sample STTS - MonitorDeadLetterQueues
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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