George
George

Reputation: 395

Azure Function with TimerTrigger shows execution count of 0 despite function running

I have a function app with a single, timer-triggered function running in MS Azure.

Background

Problem

Question

Upvotes: 3

Views: 2169

Answers (1)

anon
anon

Reputation:

Here is the workaround I did to get the Metrics in Azure Function Portal Overview Tab and the alerts as per the required condition if met:

Firstly, I checked my Timer Trigger (*/15 * * * * *) Azure Function App running successful in locally for every 15 seconds:

enter image description here

Configured the Azure Functions to Application Insights in Visual Studio and then Published to the Azure Portal Function App.

enter image description here

All the metrics, Function Execution Count, Memory working Set, Server Request Time, Server Response Times are recorded while running the Azure Function App Timer Trigger at every 15 seconds as you can see in the screenshot below:

FunctionOverviewMetricsTab


what is the best practice for implementing an alert for a specific function on a timer trigger not firing as expected (or erroring)?

To create a Alert:

Step 1: Go to the Associated Application Insights Resource with the Function App > Select Alerts from Monitoring Section.

Step 2:

Click on Create Menu > Alert Rule > Type as Count in the Search Bar and Select the Metric named as <YourFunctionName> Count

enter image description here

After Selecting the Metric, Make your requirement condition to get alerts as you can see the screenshot below that I made alert rule to the condition for every 5 minutes if function count is below < 15 times.

enter image description here

After clicking on done, you will see like:

enter image description here

enter image description here

You'll get the alert rule creation successful notification in the notification bar:

enter image description here

Run the Function and check the Alerts Page from Monitoring Section in the Azure Application Insights Resource.

enter image description here

To get more details like Function execution count per minute and the timings, Click on the color bar as you can see the screenshot below when you clicked on it:

enter image description here

Reference: Monitor Via Application Insights - Alert Rules

Note:

my question of why for a function app I previous deployed, the "Function Execution Count" is always ever 0.

Please check few steps given below:

  • Make Sure You have configured the Application Insights while publishing the Function App as shown in 2nd Image.

  • In the Azure Portal Function App > Under Application Insights option, You have to see this kind of Message: Your app is connected to App Insights Resource: <yourFunctionName> enter image description here

  • If that message not visible or you can cross check by doing like this: Azure Portal > Function App > Application Insights under Settings > Select the option Change your Resource > Select Existing Resource > Select Your Subscription - Then all the function apps will be appeared in the table, select the deployed function app which you want to track the logs and metrics.

enter image description here

  • Make Sure all the settings under Configuration like AzureWebJobsStorage and WEBSITE_CONTENTAZUREFILECONNECTION are having same storage account connection string/values and the AppInsights Instrumentation Key and its connection string values verify by checking the Application Insights Resources associated with that function app.

enter image description here

  • Also, Please visit this Msft Documentation about enabling the Telemetry in Application Insights.

Updated Answer:

Before the workaround for Azure Functions Python Stack - Application Insights, there are some limitations:

  • For the parameters like locations, runtime stack, OS, publish type, resource group or subscription, Application Insights code-less monitoring isn't supported with your selections of the mentioned parameters while creating Azure Function App in the Portal.

Example: I Selected South India location where Consumption and Premium Hosting plans are not available for my Subscription or the selected Location. If I need for that location, I need to configure through AI SDK as shown below:

AIRestrictiononlocations

Here is the workaround I did to get the Metrics in Azure Function Portal Overview Tab and the alerts as per the required condition if met:

Firstly, I checked my Timer Trigger (*/15 * * * * *) Azure Function App running successful in locally for every 15 seconds:

enter image description here

  • And Then Created the Function App in Azure Portal (West US2) along with the integration of Application Insights and deployed to Azure from VS Code.
  • After running the Function from the Azure Portal, All the metrics, Function Execution Count, Memory working Set, Server Request Time, Server Response Times are recorded while running the Azure Function App Timer Trigger at every 15 seconds as you can see in screenshot below:

enter image description here


what is the best practice for implementing an alert for a specific function on a timer trigger not firing as expected (or erroring)?

To create a Alert:

Step 1: Go to the Associated Application Insights Resource with the Function App > Select Alerts from Monitoring Section.

Step 2:

Click on Create Menu > Alert Rule > Type as Count in the Search Bar and Select the Metric named as <YourFunctionName> Count

After Selecting the Metric, Make your requirement condition to get alerts as you can see the screenshot below that I made alert rule to the condition for every 5 minutes if function count is below < 15 times.

enter image description here

Run the Function and check the Alerts Page from Monitoring Section in the Azure Application Insights Resource.

enter image description here

enter image description here


Code used in Azure Functions Python Stack:

host.json:

{
"version": "2.0",
"logging": {
"applicationInsights": {
"samplingSettings": {
"isEnabled": true,
"excludedTypes": "Request"
}
}
},
"extensionBundle": {
"id": "Microsoft.Azure.Functions.ExtensionBundle",
"version": "[2.*, 3.0.0)"
}
}

requirements.txt

azure-functions

local.settings.json:

{
"IsEncrypted": false,
"Values": {
"AzureWebJobsStorage": "<YourStorageAccountConnectionString",
"FUNCTIONS_WORKER_RUNTIME": "python"
}
}

init.py

import  datetime
import  logging
import  azure.functions  as  func

def  main(mytimer: func.TimerRequest) -> None:
utc_timestamp = datetime.datetime.utcnow().replace(
tzinfo=datetime.timezone.utc).isoformat()

if  mytimer.past_due:

logging.info('The timer is past due!')
logging.info('Python timer trigger function ran at %s', utc_timestamp)

function.json:

{
"scriptFile": "__init__.py",
"bindings": [
{
"name": "mytimer",
"type": "timerTrigger",
"direction": "in",
"schedule": "*/15 * * * * *"
}
]
}

Upvotes: 1

Related Questions