How do I get the count of messages in a particular Azure Storage queue in Azure Log Analytics

Viewed 663

I am trying to get the count of messages in a particular queue from Azure Storage account using Log Analytics to ultimately publish on a dashboard.

Thanks in advance for your help

1 Answers

We have tested this in our local environment, below statements are based on our analysis.

  • Using some kql queries in log analytics you can pull logs only on the below operations that were performed on the storage account
  1. StorageRead
  2. StorageWrite
  3. StorageDelete

Azure Monitor provides several ways to interact with metrics, including charting them in the Azure portal, accessing them through the REST API, or querying them by using PowerShell or the Azure CLI. All the platform metrics that were collected automatically are available with consolidated metric pipeline.

  • Using platform metrics in Azure portal we can get the over all metrics (QueueCapacity,QueueCount,QueueMessageCount ) at account level as show below.

enter image description here

  • If you want to pull the count of Message per queue using Log analytics it is not possible.

Alternatively, You can create a Timer trigger function to monitor the length of the queue as explained in this blog post.

  • We have tested this in our local environment which is working fine.

Here is the code in function.cs file :

using System;
    using Microsoft.Azure.WebJobs;
    using Microsoft.Azure.WebJobs.Host;
    using Microsoft.Extensions.Logging;
    using Azure.Storage.Queues;
    
    namespace FunctionApp3
    {
        public static class Function1
        {
            [FunctionName("Function1")]
            public static void Run([TimerTrigger("0 */1 * * * *")]TimerInfo myTimer, ILogger log)
            {
                log.LogInformation($"C# Timer trigger function executed at: {DateTime.Now}");
                var value = Environment.GetEnvironmentVariable("AzureWebJobsStorage");
                var queueNames = "<queueName>";
                var queueClient = new QueueClient(value, queueNames);
                int count = 0;
                foreach (var message in queueClient.ReceiveMessages(maxMessages:32).Value)
                {
                    count=count+1;
                }
                log.LogInformation($"current queueLength :{count}");
            }
        }
    }
 

Here are the setting in our local.setting.json file :

    {
        "IsEncrypted": false,
      "Values": {
        "AzureWebJobsStorage": "StrgAccountConnectionString"
        "FUNCTIONS_WORKER_RUNTIME": "dotnet"
      }
    }

Here is the sample output for reference:

enter image description here

Related