3

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

anoopjohn
  • 518
  • 4
  • 18
  • My workaround for a similar problem was to add custom log entries in my function app, so every time the function app is triggered I record the number of active messages in my queue (service bus) as an application log. I then have a custom kql query that looks for those log entries and renders them in an Azure DevOps dashboard – Milen Jan 02 '22 at 10:52

1 Answers1

2

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

VenkateshDodda
  • 4,723
  • 1
  • 3
  • 12