How to check how long message stay in Service Bus

Yana Lysenkova 20 Reputation points
2023-06-09T08:16:01.3+00:00

Hello Support team!

In our project we are using azure service bus and azure functions. We are faced performance issues. So we want to check how long the message stay in service bus: average, 50 percentile, 99 percentile.

Is it possible to check this time somehow in Monitor, Metrics, App Insights or another service?

Azure Service Bus
Azure Service Bus
An Azure service that provides cloud messaging as a service and hybrid integration.
653 questions
Azure AI Metrics Advisor
Azure AI Metrics Advisor
An Azure artificial intelligence analytics service that proactively monitors metrics and diagnoses issues.
82 questions
{count} votes

1 answer

Sort by: Most helpful
  1. navba-MSFT 26,890 Reputation points Microsoft Employee
    2023-06-09T11:24:06.6633333+00:00

    @Yana Lysenkova Welcome to Microsoft Q&A Forum, Thank you for posting your query here!

    I understand that you had faced performance issue in your Azure Service Bus resource, so you want to check how long it took to process the message.

    You can rely on the below Azure Service Bus metrics:

    1. Message Metrics
    2. Request Metrics
    3. Connection metrics
    4. Resource Usage metrics

    You can view these metrics by going to Azure Portal. Search for Monitor and select Monitor.
    User's image

    On the left menu, select Service Bus (preview).
    User's image

    Using these metrics you can observe the trend and try to isolate the performance issue.

    More info here.

    App Insights:
    Also if you have the right telemetry tracing enabled within your application to send the logs to Application Insights, you can leverage that. If you use ProcessMessageAsync of ServiceBusProcessor (message handler pattern) to process messages, the message processing is also instrumented. All Service Bus calls done by your service are automatically tracked and correlated with other telemetry items.

    async Task ProcessAsync(ProcessMessageEventArgs args)
    {
        ServiceBusReceivedMessage message = args.Message;
        if (message.ApplicationProperties.TryGetValue("Diagnostic-Id", out var objectId) && objectId is string diagnosticId)
        {
            var activity = new Activity("ServiceBusProcessor.ProcessMessage");
            activity.SetParentId(diagnosticId);
            // If you're using Microsoft.ApplicationInsights package version 2.6-beta or higher, you should call StartOperation<RequestTelemetry>(activity) instead
            using (var operation = telemetryClient.StartOperation<RequestTelemetry>("Process", activity.RootId, activity.ParentId))
            {
                telemetryClient.TrackTrace("Received message");
                try 
                {
                // process message
                }
                catch (Exception ex)
                {
                    telemetryClient.TrackException(ex);
                    operation.Telemetry.Success = false;
                    throw;
                }
    
                telemetryClient.TrackTrace("Done");
            }
        }
    }
    

    In case you make calls to supported external components during message processing, they're also automatically tracked and correlated. Refer to Track custom operations with Application Insights .NET SDK for manual tracking and correlation.

    If you're running any external code in addition to the Application Insights SDK, expect to see longer duration when viewing Application Insights logs as shown below:User's image

    Sharing a few related and useful articles on this topic:
    https://learn.microsoft.com/en-us/azure/service-bus-messaging/service-bus-end-to-end-tracing?tabs=net-standard-sdk-2#service-bus-net-client-autotracing

    https://learn.microsoft.com/en-us/azure/service-bus-messaging/monitor-service-bus?source=recommendations#analyzing-metrics

    https://learn.microsoft.com/en-us/azure/service-bus-messaging/service-bus-insights

    Hope this helps.

    **

    Please do not forget to "Accept the answer” and “up-vote” wherever the information provided helps you, this can be beneficial to other community members.


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.