You need to enable JavaScript to run this app. Azure Functions Monitor显示ResultCode为0 如何获取状态码告警及修复指标缺失
优惠活动
大模型
产品
解决方案
定价
更多

Azure Functions Monitor显示ResultCode为0 如何获取状态码告警及修复指标缺失

Azure Functions 自定义状态码上报与监控指标缺失解决方案

一、ResultCode 始终为0/配置HTTP绑定报错问题解决

你当前的核心配置错误是Timer触发器和EventHub触发器的函数,本身不支持绑定HTTP输出绑定,你参考的是HTTP触发器的文档,和非HTTP触发的函数逻辑不兼容,这是你配置后直接报错的原因。
系统原生的ResultCode字段对非HTTP触发的函数,仅标识函数系统级执行状态:0代表函数执行过程中未抛出未捕获异常,非0代表函数运行崩溃,不会映射你业务逻辑里的自定义状态码,因此无需强制复用该字段,按以下方案实现自定义状态码上报和告警即可:

  1. 先清理错误配置:删除function.json中$return类型为http的输出绑定配置,修正后的Timer触发器配置参考如下:
{
  "scriptFile": "__init__.py",
  "bindings": [
    {
      "name": "mytimer",
      "type": "timerTrigger",
      "direction": "in",
      "schedule": "0 */10 * * * *"
    }
  ]
}
  1. 代码层调整:函数返回值改为None,业务执行完成后打印结构化日志,将自定义状态码作为固定字段输出,示例代码如下:
import logging
import azure.functions as func

def main(mytimer: func.TimerRequest) -> None:
    status_code = 200
    status_msg = ""
    try:
        logging.info('Getting pre-requisite data from Azure RM and CosmosDB ...')
        azure_nsg_list = get_full_nsg_list()
        cosmosdb_nsg_entities_list = get_list_of_entities()
        nsg_stack_reference_list = get_nsg_number_references()
        logging.info('Checking for unmanaged Network Security Groups ..')
        unmanaged_nsg_list = [item for item in azure_nsg_list if item not in cosmosdb_nsg_entities_list]
        if unmanaged_nsg_list:
            logging.info('Unmanaged NSGs found, adding to CosmosDB ..')
            for nsg in unmanaged_nsg_list:
                logging.info('Adding NSG %s ..', nsg)
                create_azure_table_entity()
            logging.info('Finished adding to CosmosDB ..')
            status_msg = "All found NSGs have been added to CosmosDB."
            status_code = 200
        else:
            logging.info('No unmanaged NSGs found ...')
            status_msg = "No unmanaged NSGs found ..."
            status_code = 204 # 建议替换原来的101,101是标准HTTP切换协议状态码,不适合做业务标识
    except Exception as e:
        status_code = 500
        status_msg = f"Execution failed: {str(e)}"
        logging.error(status_msg)
    # 打印结构化状态日志,方便后续检索
    logging.info(f"[FUNCTION_EXECUTION_STATUS] status_code={status_code}, status_msg={status_msg}")
  1. 告警配置:进入函数关联的Application Insights,配置基于日志查询的告警规则,查询语句参考:
traces
| where message contains "[FUNCTION_EXECUTION_STATUS]"
| extend status_code = toint(extract("status_code=([0-9]+)", 1, message))
| where status_code >= 400
| project timestamp, cloud_RoleName, operation_Name, status_code, status_msg = extract("status_msg=(.+)", 1, message)

设置当查询结果条数>=1时触发告警即可,EventHub触发器的两个函数也可以复用这套逻辑。

二、监控指标缺失问题解决

指标不全主要和你当前的配置、组件版本有关,按以下步骤排查修复:

  • 关闭遥测采样:你当前host.json中开启了全局采样,仅排除了Request类型,其他类型的遥测会被随机丢弃,导致指标不全,修改采样配置关闭采样:
"samplingSettings": {
  "isEnabled": false
}
  • 升级扩展包版本:你当前使用的ExtensionBundle版本为2.x区间,属于较老的版本,存在监控遥测上报的已知bug,升级到稳定的3.x版本:
"extensionBundle": {
  "id": "Microsoft.Azure.Functions.ExtensionBundle",
  "version": "[3.*, 4.0.0)"
}
  • 额外排查项:检查Application Insights实例配额是否用尽、函数托管身份是否有Application Insights数据上报权限,确保无权限限制导致的遥测丢失。

内容的提问来源于stack exchange,提问作者Marco

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.07 18:00:04