'How can I monitor number of instances of a function app when it scales out?

I am looking into "Metrics" tab (Platform Features -> Metrics) in Azure portal for my function app. I can see interesting metrics like CPU time, request count, etc. but there is no metric that would show the number of instances that the app has scaled out to.

enter image description here

Is there a way to get the number of instances of the app across time?



Solution 1:[1]

One way is to use an App Insights query. This will give you the number of distinct instances running every 30 seconds for the last 24 hours.

You can edit the granularity and the time span as you choose but bear in mind that the larger granularity the less accurate the query will be as instances can spin up and wind down at any time.

let grainTime = 30sec;

traces
| where timestamp >= ago(24h)
| summarize ['rate/minute'] = dcount(cloud_RoleInstance) by bin(timestamp, grainTime)
| render timechart

You can then pin this to your dashboard!

Solution 2:[2]

After selecting any metric from the given options we can add another filter. As shown below.

Add Filter

Then we can add the "Instance" property and choose all the instances currently running for the function app. As shown below.

select the instances

Solution 3:[3]

As a preview feature, now we can have scale controller emit logs with reasoning to help understand why and how the application have scaled at various points. You will have to add a function configuration as SCALE_CONTROLLER_LOGGING_ENABLED=AppInsights:Verbose. Then you can query your scale controller logs to know reason and instance count as in this microsoft docs.

I modified the kusto query in the linked document to have a function scaling graph for past 24 hours

traces 
| where customDimensions.Category == "ScaleControllerLogs"
| where customDimensions.Action == "ScaleResult"
| where customDimensions.AppName == "my-function-app-name"
| extend currentInstanceCount = toint(customDimensions.CurrentInstanceCount)
| make-series rawInstanceCounts = max(currentInstanceCount) default=-1 on timestamp in range(ago(24h), now(), 5m)
| extend instanceCountsForwardFilled = series_fill_forward(rawInstanceCounts, -1)
| project timestamp, instanceCountsForwardFilled
| render timechart 

enter image description here

You may also emit scale controller logs to Blob Storage. In the above example, I chose AppInsights for quick queries. Also to avoid app insights pricing impact, consider disabling the config parameter once you understood the scaling behaviour.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Sam Barber
Solution 2
Solution 3 kiranpradeep