Function with Event Hub Trigger doen't follow maxWaitTime in host.json

dotnet_guy 10 Reputation points
2024-07-04T16:33:36.6366667+00:00

Hello,

I am trying to trigger an Azure function after every 10 minutes using the Event Hub. The host.json has max batch size of 100, so if either of the above is satisfied , the function should fire. But the custom metrics (in App Insights table) which are from function C# (using metric.TrackValue) doesn't seem to follow any pattern. Around 20 of them are at the same time, 10 of them a minute later, about 10 metrics are separated by a minute each etc. I think the metric is added as soon as event hub receives the event and doesn't take host.json setting maxWaitTime into consideration.

I am following this article

https://learn.microsoft.com/en-us/azure/azure-functions/functions-bindings-event-hubs?tabs=isolated-process%2Cextensionv5&pivots=programming-language-csharp#hostjson-settings

Please let me know if I'm missing something?

{
	"version": "2.0",
	"logging": {
		......
	}
	"extensions": {
		"eventHubs": {
			"maxEventBatchSize": 100,
			"minEventBatchSize": 1,			
			"maxWaitTime" : "00:10:00",            
            "batchCheckpointFrequency" : 1,
            "prefetchCount" : 100,
			"transportType" : "amqpWebSockets",
			"clientRetryOptions": {
                "mode" : "exponential",
                "tryTimeout" : "00:01:00",
                "delay" : "00:00:00.80",
                "maximumDelay" : "00:01:00",
                "maximumRetries" : 3
            }
		}
	}
}
Azure Functions
Azure Functions
An Azure service that provides an event-driven serverless compute platform.
4,990 questions
Azure Event Hubs
Azure Event Hubs
An Azure real-time data ingestion service.
638 questions
0 comments No comments
{count} votes

2 answers

Sort by: Most helpful
  1. Sina Salam 10,491 Reputation points
    2024-07-06T15:35:50.8066667+00:00

    Hello dotnet_guy,

    Welcome to the Microsoft Q&A and thank you for posting your questions here.

    Problem

    I understand that you are trying to trigger an Azure Function every 10 minutes using Event Hub events. Despite configuring the host.json with settings such as maxEventBatchSize and maxWaitTime, the custom metrics tracked within the function do not follow the expected timing pattern. Then, you suspect that the function is not responding to the maxWaitTime setting, leading to irregular logging of custom metrics.

    Solution

    To solve the issues with triggering an Azure Function using Event Hub and the irregular custom metrics, consider checking the Azure Function's scaling settings, which might affect the timing of event processing, and ensure that your Event Hub and Function App are correctly configured to handle the expected load. However, I provide here some steps to help you out, and ensure you use the reference for more details and information.

    1. Ensure that the host.json file is correctly configured with no syntax errors. I have here a corrected version of your JSON file:
    {
      "version": "2.0",
      "logging": {
        "applicationInsights": {
          "samplingSettings": {
            "isEnabled": true
          }
        }
      },
      "extensions": {
        "eventHubs": {
          "maxEventBatchSize": 100,
          "minEventBatchSize": 1,
          "maxWaitTime": "00:10:00",
          "batchCheckpointFrequency": 1,
          "prefetchCount": 100,
          "transportType": "amqpWebSockets",
          "clientRetryOptions": {
            "mode": "exponential",
            "tryTimeout": "00:01:00",
            "delay": "00:00:00.80",
            "maximumDelay": "00:01:00",
            "maximumRetries": 3
          }
        }
      }
    }
    
    1. Ensure that your Azure Function is designed to process batches of events and log metrics correctly. Also, an example of a function that processes batches of events in relation to the JSON file is here:
         using System;
         using System.Text;
         using System.Threading.Tasks;
         using Microsoft.Azure.EventHubs;
         using Microsoft.Azure.WebJobs;
         using Microsoft.Extensions.Logging;
         using Microsoft.ApplicationInsights;
         using Microsoft.ApplicationInsights.DataContracts;
         public static class EventHubTriggerFunction
         {
             private static readonly TelemetryClient telemetryClient = new TelemetryClient();
             [FunctionName("EventHubTriggerFunction")]
             public static async Task Run(
                 [EventHubTrigger("your-event-hub-name", Connection = "EventHubConnectionAppSetting")] EventData[] events,
                 ILogger log)
             {
                 log.LogInformation($"Processing batch of {events.Length} events at {DateTime.UtcNow}");
                 foreach (var eventData in events)
                 {
                     var messageBody = Encoding.UTF8.GetString(eventData.Body.Array, eventData.Body.Offset, eventData.Body.Count);
                     log.LogInformation($"Processed event: {messageBody}");
                     
                     // Track custom metric
                     telemetryClient.TrackMetric("YourMetricName", 1);
                 }
             }
         }
      
    2. Lastly, add additional logging to your function to trace the flow of events and metric tracking. This can help identify where the irregularities occur. You will also need to verify the timing and distribution of custom metrics in Application Insights. Ensure they align with your maxEventBatchSize and maxWaitTime settings and if the metrics still do not follow the expected pattern, consider adjusting the maxWaitTime or maxEventBatchSize to better suit your requirements.

    References

    Source: Azure Functions Event Hubs bindings. Accessed, 7/6/2024.

    Source: Azure Functions Host JSON reference. Accessed, 7/6/2024.

    Source: Monitor Azure Functions with Application Insights. Accessed, 7/6/2024.

    Source: TelemetryClient Class. Accessed, 7/6/2024.

    Accept Answer

    I hope this is helpful! Do not hesitate to let me know if you have any other questions.

    ** Please don't forget to close up the thread here by upvoting and accept it as an answer if it is helpful ** so that others in the community facing similar issues can easily find the solution.

    Best Regards,

    Sina Salam


  2. Jason Hampson 0 Reputation points
    2024-09-30T15:00:16.32+00:00

    This message may come a bit late, but I imagine you need to increase the value of minEventBatchSize. That condition is being met any time messages arrive so it will not wait for maxWaitTime. The source code seems to have some logic for the minimum size being greater than 1.

    https://github.com/Azure/azure-sdk-for-net/blob/main/sdk/eventhub/Microsoft.Azure.WebJobs.Extensions.EventHubs/src/Listeners/EventHubListener.PartitionProcessor.cs

    0 comments No comments

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.