Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Programmatically get the amount of instances running for a Function App

I'm running an Azure Function app on Consumption Plan and I want to monitor the amount of instances currently running. Using REST API endpoint of format

https://management.azure.com/subscriptions/{subscr}/resourceGroups/{rg}
       /providers/Microsoft.Web/sites/{appname}/instances?api-version=2015-08-01

I'm able to retrieve the instances. However, the result doesn't match the information that I see in Application Insights / Live Metrics Stream.

For example, right now App Insights shows 4 servers online, while API call returns just one (the GUID of this 1 instance is also among App Insights guids).

Who can I trust? Is there a better way to get instance count (e.g. from App Insights)?

UPDATE: It looks like data from REST API are wrong.

I was sending 10000 messages to the queue, logging each function call with respective instance ID which processed the request.

While messages keep coming in and the backlog grows, instance count from REST API seems to be correct (scaled from 1 to 12). After sending stops, the reported instance count rapidly goes down (eventually back to 1, while processors are still busy).

But based on the speed and the execution logs I can tell that the actual instance count kept growing and ended up at 15 instances at the moment of last message processed.

UPDATE2: It looks like SDK refuses to report more than 20 servers. The metric flats out at 20, while App Insights kept steady growth and is already showing 41.

like image 209
Mikhail Shilkov Avatar asked Nov 08 '22 18:11

Mikhail Shilkov


1 Answers

Who can I trust? Is there a better way to get instance count (e.g. from App Insights)?

Based on my understanding we need to use Rest API endpoint to retrieve the instance, App Insights could be configured for multiple WebApps, so the number of servers online in the App Insights may be for multiple WebApps.

Updated:

Based on my test, the number of the application insight may be not real time.

During my test if the WebApp Function scale out then I could get multiple instances with Rest API, and I also can check the number of servers online in the App Insights.

https://management.azure.com/subscriptions/{subscriptionId}/resourceGroups/{resourcegroup}/providers/Microsoft.Web/sites/{functionname}/instances?api-version=2016-08-01

But after I finished the test, I could get the number of the instance with Rest API is 1, based on my understanding, it is right result. At the same time I check it in the Application Insight the number of the servers online is the max number during my test.

And after a while, the number of server online in the application insight also became 1.

So If we want to get the number of intance for Azure function, my suggestion is that using REST API to do that.

Update2:

According to the DavidEbbo mentioned that the REST API is not always reliable.

Unfortunately, the REST API is not always reliable. Specifically, when a Function App scales across multiple scale units, only the instances from the 'home' scale unit are reflected. You probably will not see this in a smallish test, but likely will if you start scaling out widely (say over 20 instances).

like image 167
Tom Sun - MSFT Avatar answered Nov 15 '22 06:11

Tom Sun - MSFT