azure - 如何从azure函数的appinsight中排除异常?

标签 azure azure-functions azure-application-insights

我们的 azure 函数中有这个 host.json 文件:

{
  "version": "2.0",
  "functions": [ "xxx" ],
  "logging": {
    "applicationInsights": {
      "samplingExcludedTypes": "Request",
      "samplingSettings": {
        "isEnabled": true
      }
    }
  }
}

现在我们希望排除自动记录的异常(因为我们自己在 try catch block 中处理异常,所以不希望它记录两次)。 但是,根据此问题,我不确定抽样排除类型是否是正确使用的属性: https://github.com/MicrosoftDocs/azure-docs/issues/47219 , exceptTypes 是要使用的类型 我应该这样做吗:

"logging": {
    "applicationInsights": {
      "samplingSettings": {
        "isEnabled": true,
        "excludedTypes": "Request;Exception"
      }
    }
  }

最佳答案

通常我会说你需要的是 Telemetry Processor或遥测初始化程序,根据语言,删除所有异常遥测。但不幸的是,这在 Azure 函数中不起作用,它是 not supported .

但是,我们可以利用采样设置,通过强制采样遥测数据来防止发送遥测数据。当启用自适应采样时它确实可以工作(我只是没有尝试其他类型的采样),这是 Azure 函数的默认采样行为。为此,我们可以设置 ProactiveSamplingDecision 属性,如下所示:

    public class DropExceptionTelemetry : ITelemetryInitializer
    {
        public void Initialize(ITelemetry telemetry)
        {
            if (!(telemetry is ExceptionTelemetry item)) return;

            item.ProactiveSamplingDecision = SamplingDecision.SampledOut;
        }
    }

另外,不要忘记使用 DI 添加此初始值设定项:

    public class Startup : FunctionsStartup
    {
        public override void Configure(IFunctionsHostBuilder builder)
        {
            builder.Services.AddSingleton<ITelemetryInitializer, DropExceptionTelemetry>();
            builder.Services.AddLogging();
        }
    }

要使其正常工作,请确保在 exludedTypes 属性中监听异常

    "applicationInsights": {
      "samplingSettings": {
        "excludedTypes": "Event;Request"
      }
    }

关于azure - 如何从azure函数的appinsight中排除异常?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64355380/

相关文章:

azure - 如何使用数组中的字段作为过滤器从 Cosmos DB 获取文档?

node.js - Azure函数上下文日志未正确记录,不同步

Azure Application Insights,如何通过 Azure CLI 更改每日上限

c# - 尽管线程池中有足够的线程,但请求仍在 Azure AppService 中排队

python - Azure 存储访问被拒绝

azure - 从 Azure 函数调用中枚举所有函数描述符

c# - 我可以在 Azure Functions 2.0 中同时使用 dotnet 和 node 吗?

c# - 如何知道应用程序洞察遥测客户端是否正常运行?

c# - 适用于本地 Web 应用程序的 Azure Application Insight 遥测

powershell - 用于创建 Azure 服务总线队列和主题的 Azure powershell cmdlet