升级至Azure Functions隔离模式与.NET8后Application Insights链路异常
问题描述
将Azure Functions升级为隔离模式并迁移至.NET 8后,发现Application Insights中Service Bus请求的Operation links属性为空,示例值:
Operation links [{"operation_Id":"00000000000000000000000000000000","id":"0000000000000000"}]
这导致端到端事务视图中多个请求被错误合并。此前.NET 6进程内模式下,Service Bus请求的事务追踪是正常的。
当前Program.cs中的Application Insights配置:
.ConfigureServices((context, services) => { services.AddApplicationInsightsTelemetryWorkerService(); services.ConfigureFunctionsApplicationInsights(); services.Configure<LoggerFilterOptions>(options => { // The Application Insights SDK adds a default logging filter that instructs ILogger to capture only Warning and more severe logs. Application Insights requires an explicit override. // Log levels can also be configured using appsettings.json. For more information, see https://learn.microsoft.com/en-us/azure/azure-monitor/app/worker-service#ilogger-logs LoggerFilterRule? ruleToRemove = options.Rules.FirstOrDefault(rule => rule.ProviderName == "Microsoft.Extensions.Logging.ApplicationInsights.ApplicationInsightsLoggerProvider"); if (ruleToRemove is not null) { options.Rules.Remove(ruleToRemove); } }); }
项目.csproj配置:
<Project Sdk="Microsoft.NET.Sdk"> <PropertyGroup> <TargetFramework>net8.0</TargetFramework> <AzureFunctionsVersion>v4</AzureFunctionsVersion> <OutputType>Exe</OutputType> <ImplicitUsings>enabled</ImplicitUsings> <AnalysisLevel>latest-recommended</AnalysisLevel> <Nullable>enable</Nullable> </PropertyGroup> <ItemGroup> <PackageReference Include="Microsoft.Azure.Functions.Analyzers" Version="1.0.0" /> <PackageReference Include="Azure.Data.Tables" Version="12.9.1" /> <PackageReference Include="Azure.Storage.Blobs" Version="12.22.1" /> <PackageReference Include="Microsoft.ApplicationInsights.WorkerService" Version="2.22.0" /> <PackageReference Include="Microsoft.Azure.Functions.Extensions" Version="1.1.0" /> <PackageReference Include="Microsoft.Azure.Functions.Worker.ApplicationInsights" Version="1.4.0" /> <PackageReference Include="Microsoft.Azure.NotificationHubs" Version="4.2.0" /> <PackageReference Include="NetTopologySuite" Version="2.5.0" /> <PackageReference Include="Microsoft.Azure.Functions.Worker.Extensions.Http" Version="3.2.0" /> <PackageReference Include="Microsoft.Azure.Functions.Worker.Sdk" Version="1.18.0" /> <PackageReference Include="Microsoft.Azure.Functions.Worker" Version="1.23.0" /> <PackageReference Include="Microsoft.Azure.Functions.Worker.Extensions.ServiceBus" Version="5.22.0" /> </ItemGroup> <ItemGroup> <None Update="host.json"> <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory> </None> <None Update="local.settings.json"> <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory> <CopyToPublishDirectory>Never</CopyToPublishDirectory> </None> </ItemGroup> <ItemGroup> <Using Include="System.Threading.ExecutionContext" Alias="ExecutionContext" /> </ItemGroup> <ItemGroup> <None Include="local.settings.json" /> </ItemGroup> </Project>
解决方案
1. 升级相关NuGet包到最新兼容版本
当前使用的Microsoft.Azure.Functions.Worker.ApplicationInsights(1.4.0)和Microsoft.ApplicationInsights.WorkerService(2.22.0)版本较旧,存在隔离模式下Service Bus追踪关联的已知问题。建议升级到最新稳定版:
Microsoft.ApplicationInsights.WorkerService升级到2.24.0+Microsoft.Azure.Functions.Worker.ApplicationInsights升级到1.10.0+- 同步升级
Microsoft.Azure.Functions.Worker、Microsoft.Azure.Functions.Worker.Sdk和Microsoft.Azure.Functions.Worker.Extensions.ServiceBus到最新v4兼容版本
2. 调整Application Insights配置顺序
在Program.cs中,确保ConfigureFunctionsApplicationInsights在AddApplicationInsightsTelemetryWorkerService之前调用,这是隔离模式下正确的配置顺序:
.ConfigureServices((context, services) => { // 先配置Functions专属AI设置 services.ConfigureFunctionsApplicationInsights(); // 再添加Worker Service AI服务 services.AddApplicationInsightsTelemetryWorkerService(); services.Configure<LoggerFilterOptions>(options => { LoggerFilterRule? ruleToRemove = options.Rules.FirstOrDefault(rule => rule.ProviderName == "Microsoft.Extensions.Logging.ApplicationInsights.ApplicationInsightsLoggerProvider"); if (ruleToRemove is not null) { options.Rules.Remove(ruleToRemove); } }); }
3. 配置host.json启用分布式追踪
在host.json中添加或确认以下配置,确保分布式追踪正确传递:
{ "version": "2.0", "logging": { "applicationInsights": { "samplingSettings": { "isEnabled": true, "excludedTypes": "Request" }, "enableDependencyTracking": true, "enableLiveMetricsFilters": true } }, "extensions": { "serviceBus": { "clientOptions": { "enableDistributedTracing": true } } } }
4. 手动验证追踪上下文传递
如果上述步骤无效,可以在Service Bus触发的函数中手动获取并验证追踪上下文:
using Microsoft.Azure.Functions.Worker; using Microsoft.Azure.Functions.Worker.Extensions.ServiceBus; using Microsoft.ApplicationInsights; using Microsoft.ApplicationInsights.DataContracts; public class ServiceBusFunction { private readonly TelemetryClient _telemetryClient; public ServiceBusFunction(TelemetryClient telemetryClient) { _telemetryClient = telemetryClient; } [Function(nameof(ProcessServiceBusMessage))] public void ProcessServiceBusMessage( [ServiceBusTrigger("queue-name", Connection = "ServiceBusConnection")] string message, FunctionContext context) { // 获取当前请求的追踪上下文 var operationId = context.TraceContext.TraceId; var parentId = context.TraceContext.ParentId; // 手动创建关联的请求遥测(如果自动关联失效) using var operation = _telemetryClient.StartOperation<RequestTelemetry>("ProcessServiceBusMessage"); operation.Telemetry.Context.Operation.Id = operationId; operation.Telemetry.Context.Operation.ParentId = parentId; // 函数业务逻辑... } }
这些步骤应该能修复Operation links为空的问题,恢复端到端事务追踪的正确性。
内容的提问来源于stack exchange,提问作者Julian Hüppauff
相关产品推荐
相关产品推荐

