带有 TimerTrigger 的 Azure Function 显示执行计数为 0,尽管有函数 运行

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

我在 MS Azure 中有一个带有单个定时器触发函数的函数应用程序 运行ning。

背景

问题

问题

这是我在 Azure Function Portal Overview 选项卡中获取 Metrics 的解决方法,如果满足,则根据要求的条件发出警报:

首先,我检查了我的计时器触发器 (*/15 * * * * *) Azure Function App 运行 每 15 秒在本地成功一次:

在Visual Studio中将Azure Functions配置为Application Insights,然后发布到Azure Portal Function App。

记录所有指标,函数执行计数,内存工作集,服务器请求时间,服务器响应时间,而 运行 Azure Function App Timer Trigger 在每个15 秒,如下面的屏幕截图所示:


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

创建警报:

第 1 步: 使用 Function App > Select Alerts监控部分。

第 2 步:

单击创建菜单 > 警报规则 > 在搜索栏中键入计数,然后 Select 将指标命名为 <YourFunctionName> Count

在 Selecting 指标后,制定您的要求条件以获取警报,因为您可以在下面的屏幕截图中看到我为每 5 个条件制定警报规则分钟,如果函数计数低于 < 15 次。

点击完成后,你会看到:

您将在通知栏收到警报规则创建成功通知:

运行 函数并检查 Azure Application Insights 资源中监视部分的警报页面。

要获取更多详细信息,例如每分钟的函数执行次数和时间,请单击颜色条,您可以在单击它时看到下面的屏幕截图:

参考:Monitor Via Application Insights - Alert Rules

注:

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

请检查以下几个步骤:

  • 确保在发布 Function App 时配置了 Application Insights,如第二张图片所示。

  • 在 Azure Portal Function App > Application Insights 选项下,您必须看到这种消息:Your app is connected to App Insights Resource: <yourFunctionName>

  • 如果该消息不可见,或者您可以这样交叉检查: Azure 门户 > Function App > Application Insights under Settings > Select 选项 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.

  • 确保配置下的所有设置如 AzureWebJobsStorageWEBSITE_CONTENTAZUREFILECONNECTION 具有相同的存储帐户连接 string/values 和 AppInsights Instrumentation Key 及其 connection string 值通过检查与该函数应用关联的 Application Insights 资源进行验证。

  • 此外,请访问此 Msft Documentation 关于在 Application Insights 中启用遥测。

更新答案:

在 Azure Functions Python Stack - Application Insights 的解决方法之前,存在一些限制:

  • 对于位置、运行时堆栈、OS、发布类型、资源组或订阅等参数,您在创建 Azure 时选择的上述参数不支持 Application Insights code-less 监控门户中的功能应用程序。

示例: 我 Selected 南印度位置,其中消费和高级托管计划不适用于我的订阅或所选位置。如果我需要那个位置,我需要通过 AI SDK 进行配置,如下所示:

这是我在 Azure Function Portal Overview 选项卡中获取 Metrics 的解决方法,如果满足,则根据要求的条件发出警报:

首先,我检查了我的计时器触发器 (*/15 * * * * *) Azure Function App 运行 每 15 秒在本地成功一次:

  • 然后在 Azure 门户(美国西部 2)中创建函数应用程序以及 Application Insights 的集成并从 VS Code 部署到 Azure。
  • 在 运行 来自 Azure 门户的函数之后,记录所有指标、函数执行计数、内存工作集、服务器请求时间、服务器响应时间,同时 运行 Azure 函数应用程序计时器每 15 秒触发一次,如下面的屏幕截图所示:


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

创建警报:

第 1 步: 转到关联的 Applcation Insights 带有 Function App 的资源 > Select Alerts 来自 Monitoring 节.

第 2 步:

单击创建菜单 > 警报规则 > 在搜索栏中键入计数,然后 Select 将指标命名为 <YourFunctionName> Count

在 Selecting 指标后,制定您的要求条件以获取警报,因为您可以在下面的屏幕截图中看到我为每 5 个条件制定警报规则分钟,如果函数计数低于 < 15 次。

运行 函数并检查 Azure Application Insights 资源中监视部分的警报页面。


Azure Functions 中使用的代码Python 堆栈:

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 * * * * *"
}
]
}