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

Azure Functions v4 InProcess模式下host.json采样设置未生效求助

Azure Functions v4 (.NET6 InProcess) Application Insights 依赖项采样配置无效问题

问题场景

使用.NET6 InProcess模式的Azure Functions v4,尝试通过host.json配置Application Insights仅对依赖项进行采样收集,但配置未生效:

当前host.json配置:

{
  "version": "2.0",
  "logging": {
    "applicationInsights": {
      "samplingSettings": {
        "isEnabled": true,
        "includedTypes": "Dependency"
      }
    }
  }
}

预期仅收集依赖项数据,但实际无效果。尝试设置enableDependencyTracking: false会完全停止依赖项收集,不符合需求。项目配置如下:

<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>
    <TargetFramework>net6.0</TargetFramework>
    <AzureFunctionsVersion>v4</AzureFunctionsVersion>
  </PropertyGroup>
  <ItemGroup>
    <PackageReference Include="Microsoft.Azure.Functions.Extensions" Version="1.1.0" />
    <PackageReference Include="Microsoft.Azure.WebJobs.Extensions.ServiceBus" Version="5.11.0" />
    <PackageReference Include="Microsoft.NET.Sdk.Functions" Version="4.2.0" />
    <PackageReference Include="Microsoft.Extensions.Http" Version="6.0.0" />
    <PackageReference Include="Microsoft.Extensions.Http.Polly" Version="6.0.18" />
    <PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
  </ItemGroup>
  <ItemGroup>
    <None Update="host.json">
      <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
    </None>
    <None Update="local.settings.json">
      <CopyToOutputDirectory>Always</CopyToOutputDirectory>
      <CopyToPublishDirectory>Never</CopyToPublishDirectory>
    </None>
  </ItemGroup>
</Project>

排查与解决方法

1. 修正采样配置逻辑

includedTypes的作用是指定哪些类型日志会进入采样评估流程,而非仅保留该类型数据。默认采样会关联请求链路,若父请求被采样排除,依赖项作为子项也会被过滤。要实现仅收集依赖项,需明确排除其他类型并确保依赖项采样率:

{
  "version": "2.0",
  "logging": {
    "applicationInsights": {
      "samplingSettings": {
        "isEnabled": true,
        "includedTypes": "Dependency",
        "excludedTypes": "Request;Trace;Exception;Event",
        "percentage": 100
      },
      "enableDependencyTracking": true
    }
  }
}
  • 明确排除请求、追踪等其他日志类型
  • 设置percentage:100确保所有依赖项都被采样(可按需调整比例)

2. 升级依赖包版本

当前使用的Microsoft.NET.Sdk.Functions 4.2.0版本存在Application Insights采样逻辑的已知问题,建议升级至较新版本(如4.11.0及以上),同步更新相关依赖:

<PackageReference Include="Microsoft.NET.Sdk.Functions" Version="4.11.0" />
<PackageReference Include="Microsoft.Azure.WebJobs.Extensions.ServiceBus" Version="5.17.0" />

3. 检查环境采样设置冲突

  • 本地调试时,需确保local.settings.json未强制关闭采样:
{
  "Values": {
    "APPINSIGHTS_INSTRUMENTATIONKEY": "你的密钥",
    "FUNCTIONS_WORKER_RUNTIME": "dotnet",
    "APPINSIGHTS_SAMPLING_PERCENTAGE": "100"
  }
}
  • 部署到Azure后,检查Application Insights门户中的采样设置是否覆盖了host.json配置,门户设置优先级更高。

4. 确认HttpClient依赖追踪正确性

若使用自定义HttpClient发起请求,必须通过依赖注入方式创建实例,而非直接new HttpClient(),否则依赖追踪无法正常捕获请求:

public class MyFunction
{
    private readonly HttpClient _httpClient;
    public MyFunction(HttpClient httpClient)
    {
        _httpClient = httpClient;
    }

    [FunctionName("MyFunction")]
    public async Task Run([ServiceBusTrigger("queue", Connection = "ServiceBusConnection")] string myQueueItem)
    {
        await _httpClient.GetAsync("https://example.com");
    }
}

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.16 17:32:43