这是我为在 Azure 功能门户概览选项卡中获取指标以及满足所需条件的警报所做的解决方法:
首先,我检查了我的计时器触发器 (*/15 * * * * *) Azure Function App 每 15 秒在本地成功运行一次:
在 Visual Studio 中将 Azure Functions 配置为 Application Insights,然后发布到 Azure Portal Function App。
所有指标、函数执行计数、内存工作集、服务器请求时间、服务器响应时间都会在每 15 秒运行一次 Azure Function App 计时器触发器时记录一次,如下面的屏幕截图所示:
在未按预期触发(或出错)的计时器触发器上为特定功能实现警报的最佳做法是什么?
创建警报:
第 1 步:
转到与 Function App 关联的 Application Insights 资源 > 从 Monitoring 部分中选择 Alerts。
第 2 步:
点击创建菜单 > 警报规则 > 在搜索栏中输入计数并选择名为 <YourFunctionName> Count的指标
选择指标后,设置您的要求条件以获取警报,如下面的屏幕截图所示,如果函数计数低于
点击完成后,你会看到如下:
您会在通知栏中收到警报规则创建成功通知:
运行函数并检查 Azure Application Insights 资源中监控部分的警报页面。
要获取更多详细信息,例如每分钟的函数执行次数和时间,请点击彩条,点击后可以看到下面的屏幕截图:
参考:Monitor Via Application Insights - Alert Rules
注意:
我的问题是,为什么对于我之前部署的函数应用,“函数执行计数”始终为 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 > 选择选项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。
- 确保配置下的所有设置(如
AzureWebJobsStorage 和 WEBSITE_CONTENTAZUREFILECONNECTION)具有相同的存储帐户连接字符串/值,并且通过检查与该函数应用关联的 Application Insights 资源来验证 AppInsights Instrumentation Key 及其 connection string 值.
更新答案:
在 Azure Functions Python Stack - Application Insights 的解决方法之前,存在一些限制:
- 对于位置、运行时堆栈、操作系统、发布类型、资源组或订阅等参数,您在门户中创建 Azure Function App 时选择的上述参数不支持 Application Insights 无代码监控。李>
示例:我选择了印度南部地区,而消费和高级托管计划不适用于我的订阅或所选地区。如果我需要那个位置,我需要通过AI SDK进行配置,如下图:
这是我为在 Azure 功能门户概览选项卡中获取指标以及满足所需条件的警报所做的解决方法:
首先,我检查了我的 Timer Trigger (*/15 * * * * *) Azure Function App 每 15 秒在本地成功运行一次:
- 然后在 Azure 门户(美国西部 2)中创建 Function App 以及 Application Insights 的集成,并从 VS Code 部署到 Azure。
- 从 Azure 门户运行函数后,所有指标、函数执行计数、内存工作集、服务器请求时间、服务器响应时间都会在每 15 秒运行一次 Azure 函数应用程序计时器触发器时记录一次,如您在截图如下:
在未按预期触发(或出错)的计时器触发器上为特定功能实现警报的最佳做法是什么?
创建警报:
第 1 步:
转到与 Function App 关联的 Application Insights 资源 > 从 Monitoring 部分中选择 Alerts。
第 2 步:
点击创建菜单 > 警报规则 > 在搜索栏中键入计数并选择名为 <YourFunctionName> Count 的指标
选择指标后,设置您的要求条件以获取警报,如下面的屏幕截图所示,如果函数计数低于
运行函数并检查 Azure Application Insights 资源中监控部分的警报页面。
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
天蓝色函数
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 * * * * *"
}
]
}