【问题标题】:Azure Function with TimerTrigger shows execution count of 0 despite function running尽管函数正在运行,但带有 TimerTrigger 的 Azure 函数显示执行计数为 0
【发布时间】:2022-02-17 16:39:05
【问题描述】:

我有一个在 MS Azure 中运行的函数应用,其中包含一个计时器触发的函数。

背景

  • 计时器每 15 秒运行一次,使用以下 NCRONTAB 表达式:*/15 * * * * *
  • 单击 Azure 门户中的“监视器”选项卡会显示许多在预期时间间隔(每 15 秒)内成功执行
  • 我有证据证明该函数确实按预期运行(行为是在每次执行时调用一个 Web API,我可以从该 API 的另一端看到它确实被调用了)。

问题

  • 在整个函数应用的“概述”选项卡上的 Azure 门户中,显示“函数执行计数”的指标小部件始终显示 0 次执行。
  • 我已尝试设置 Azure Monitor 警报规则,只要此函数应用中的单个函数在过去 5 分钟内未运行至少 15 次,我就会收到通知。警报规则处于被激活/触发的持久状态,大概是因为整体函数执行计数始终为 0。

问题

  • 什么可能导致“函数执行计数”指标不更新?
  • 如果它显示 0 是预期行为,那么在计时器触发器未按预期触发(或错误)时为特定功能实现警报的最佳做法是什么?

【问题讨论】:

    标签: azure azure-functions azure-monitoring


    【解决方案1】:

    这是我为在 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

    • 确保配置下的所有设置(如 AzureWebJobsStorageWEBSITE_CONTENTAZUREFILECONNECTION)具有相同的存储帐户连接字符串/值,并且通过检查与该函数应用关联的 Application Insights 资源来验证 AppInsights Instrumentation Key 及其 connection string 值.

    • 另外,请访问Msft Documentation,了解如何在 Application Insights 中启用遥测。

    更新答案:

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

    【讨论】:

    • 这很有帮助,但它没有回答我的问题,即为什么对于我之前部署的函数应用程序,“函数执行计数”始终为 0。
    • 请检查您是否已在 Visual Studio 中配置了正确的 Application Insights 资源以防止发布 Function App!
    • 您好@George,请查看更新后的答案。
    • 嗨@HariKrishnaRajoli-MT,感谢您的帮助!通过在特定于单个功能的 App Insights 资源中设置警报,我已经能够根据需要运行警报。但是,即使在为我的 Function App 重新选择 App Insights 资源后,“Function App Overview”选项卡上的“Function Execution Count”仍始终显示 0,除非我从 Function App 的“代码/测试”面板中手动触发该函数.
    • 另外,我使用 VS Code 进行部署,而不是 Visual Studio,因此我无法在部署之前验证这些指标。
    猜你喜欢
    • 2021-12-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-03-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多