You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Azure Functions多函数的Application Insights无消息接收告警配置咨询

Azure Function单函数消息接收告警配置方案

一、给每个函数添加专属遥测标识

在C#函数代码中,通过TelemetryClient为每个函数添加唯一标识属性,确保Application Insights能区分不同函数的遥测数据:

public class Function1
{
    private readonly TelemetryClient _telemetryClient;

    public Function1(TelemetryClient telemetryClient)
    {
        _telemetryClient = telemetryClient;
        // 为当前函数添加专属标识
        _telemetryClient.Context.Properties["FunctionName"] = "Function1";
    }

    [FunctionName("Function1")]
    public async Task Run([ServiceBusTrigger("topic1", "subscription1", Connection = "ServiceBusConnection")] string mySbMsg, ILogger log)
    {
        log.LogInformation($"Function1 processed message: {mySbMsg}");
        // 记录消息接收事件,确保遥测被捕获
        _telemetryClient.TrackEvent("MessageReceived", new Dictionary<string, string> { { "FunctionName", "Function1" } });
    }
}

对另外两个函数(Function2、Function3)重复上述操作,仅修改FunctionName属性值和Service Bus触发器配置即可。

二、配置自定义日志搜索告警

由于单函数范围无法选择自定义日志搜索信号,我们基于整个Function应用范围,通过Kusto查询筛选单个函数的消息接收情况:

  1. 进入Azure Portal,打开对应的Application Insights资源,左侧菜单选择告警 > 创建 > 新建告警规则
  2. 范围设置:选择你的Azure Function应用作为告警范围
  3. 信号逻辑配置:
    • 信号类型选择自定义日志搜索
    • 日志查询框输入针对单个函数的Kusto语句(以Function1为例):
      customEvents
      | where name == "MessageReceived"
      | where customDimensions.FunctionName == "Function1"
      | where timestamp > ago(15m)
      | summarize count()
      
    • 条件设置为小于,阈值填1(15分钟内无消息时,统计结果为0,满足触发条件)
    • 评估频率设为15分钟,窗口大小设为15分钟
  4. 操作组配置:选择已有的邮件通知操作组,或新建操作组并添加接收告警的邮箱
  5. 完成规则创建:命名规则(如“Function1 15分钟未接收消息告警”),重复此步骤为Function2、Function3分别创建告警规则

替代方案:基于函数执行日志的查询

如果不想添加自定义事件,也可以直接查询函数执行日志,筛选特定函数的执行记录:

requests
| where cloud_RoleName == "<你的Function应用名称>"
| where operation_Name == "Function1"
| where timestamp > ago(15m)
| summarize count()

此方案依赖函数执行日志,需注意排除非消息触发的函数执行(如预热、测试调用)。

注意事项

  • 确保Function应用已开启Application Insights集成,无需额外配置TelemetryClient的连接字符串
  • 自定义事件名称需保持统一(如示例中的MessageReceived),避免查询出错
  • 告警的评估频率和窗口大小需保持一致,确保每次评估的是完整的15分钟周期数据

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.25 16:36:11