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

Azure Functions如何配置Application Insights过滤可用性测试日志

.NET 3.1 Azure函数过滤Application Insights可用性测试日志实现方案

Azure Functions v3(对应.NET 3.1运行时)完全支持自定义ITelemetryProcessor实现遥测过滤,核心逻辑和ASP.NET Core一致,仅需要适配函数的依赖注入注册方式即可,无需额外第三方组件。

前置准备

  • 确认项目已安装匹配Azure Functions v3版本的Application Insights SDK包:Microsoft.Azure.WebJobs.Logging.ApplicationInsights
  • 无需修改现有可用性测试的探测配置,所有过滤逻辑在函数实例侧完成

方案1:完全过滤所有可用性测试相关日志

  1. 实现自定义遥测处理器,识别可用性测试来源的遥测项直接丢弃,不上传到Application Insights
    示例代码:
    using Microsoft.ApplicationInsights.Channel;
    using Microsoft.ApplicationInsights.Extensibility;
    
    public class AvailabilityTestTelemetryFilter : ITelemetryProcessor
    {
        private readonly ITelemetryProcessor _next;
    
        public AvailabilityTestTelemetryFilter(ITelemetryProcessor next)
        {
            _next = next;
        }
    
        public void Process(ITelemetry item)
        {
            // 所有可用性测试产生的遥测都会携带固定的合成源标记
            if (item.Context.Operation.SyntheticSource != null 
                && item.Context.Operation.SyntheticSource.Equals("Application Insights Availability Monitoring", StringComparison.OrdinalIgnoreCase))
            {
                // 不传递给后续处理器,即丢弃该条日志
                return;
            }
            _next.Process(item);
        }
    }
    
  2. 在函数启动类中注册自定义处理器,.NET 3.1进程内模型Azure函数的注册方式如下:
    using Microsoft.Azure.Functions.Extensions.DependencyInjection;
    using Microsoft.ApplicationInsights.Extensibility;
    // 替换为你自己的函数命名空间
    using YourFunctionNamespace;
    
    [assembly: FunctionsStartup(typeof(Startup))]
    namespace YourFunctionNamespace
    {
        public class Startup : FunctionsStartup
        {
            public override void Configure(IFunctionsHostBuilder builder)
            {
                // 注册自定义遥测过滤器
                builder.Services.AddApplicationInsightsTelemetryProcessor<AvailabilityTestTelemetryFilter>();
            }
        }
    }
    

部署完成后,所有可用性测试触发的请求日志、依赖日志、跟踪日志都不会再上传到Application Insights。

方案2:仅保留可用性测试失败结果日志(推荐)

只需要调整上述自定义遥测处理器的判断逻辑,识别到是可用性测试来源的遥测时,仅保留标记为失败的条目即可,修改后的Process方法示例:

public void Process(ITelemetry item)
{
    // 判断是否为可用性测试产生的遥测
    if (item.Context.Operation.SyntheticSource != null 
        && item.Context.Operation.SyntheticSource.Equals("Application Insights Availability Monitoring", StringComparison.OrdinalIgnoreCase))
    {
        bool isFailedTelemetry = false;
        // 判定失败规则可根据自身业务调整
        if (item is Microsoft.ApplicationInsights.DataContracts.RequestTelemetry request)
        {
            isFailedTelemetry = request.ResponseCode != "200" || !request.Success.GetValueOrDefault(true);
        }
        else if (item is Microsoft.ApplicationInsights.DataContracts.ExceptionTelemetry)
        {
            isFailedTelemetry = true;
        }
        else if (item is Microsoft.ApplicationInsights.DataContracts.DependencyTelemetry dependency)
        {
            isFailedTelemetry = !dependency.Success.GetValueOrDefault(true);
        }

        if (!isFailedTelemetry)
        {
            // 成功的可用性测试日志直接丢弃
            return;
        }
    }
    _next.Process(item);
}

注意:如果你的可用性检查端点自定义了失败状态码规则,可自行调整isFailedTelemetry的判断逻辑,匹配实际业务判定标准即可。

补充说明

  • 过滤逻辑在函数实例本地执行,不会产生额外的遥测上传流量
  • 该配置不会影响可用性测试本身的探测能力:Application Insights侧的可用性全局指标(可用率、各探测点成功率等)是由微软的公网探测节点独立计算上报的,和函数侧是否上传请求日志无关
  • 如果你使用的是.NET 3.1隔离进程模型Azure函数,核心过滤逻辑完全一致,仅需要把处理器注册逻辑迁移到Program.cs的主机服务配置中即可

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.27 14:45:33