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

Application Insights重复请求日志及依赖关联问题求助

问题分析与解决方案

问题1:API请求生成两条独立的请求日志(APIM + App Service)

这是APIM与App Service的Application Insights集成各自独立生成请求遥测导致的默认行为:

  • APIM接收客户端请求时会记录一条Request遥测
  • App Service的.NET Web API处理请求时也会生成一条Request遥测

解决方法

若希望合并关联或减少重复日志,可调整APIM配置:

  1. 进入APIM实例的Application Insights集成设置
  2. 关闭「Track requests」选项,此时APIM仅会记录调用App Service的依赖遥测,不再生成独立请求遥测
  3. 保持APIM的关联模式为W3C,确保App Service的请求遥测自动关联APIM的依赖遥测

问题2:HttpClient调用的依赖未关联到请求日志

当前手动注入traceparent的方式不符合W3C标准,且SDK注册流程未正确启用依赖追踪能力,导致关联失败:

核心问题点

  1. traceparent格式错误:Activity.Current?.Id的格式为{trace-id}/{parent-id},并非W3C标准的00-{trace-id}-{parent-id}-01格式,直接注入会导致追踪上下文断裂
  2. SDK注册逻辑缺陷:提前构建ServiceProvider可能导致配置未正确生效,且未确保依赖追踪拦截器自动注入
  3. HttpClient未绑定追踪拦截器:未使用官方AddHttpClient扩展方法,导致App Insights无法自动记录HttpClient调用的依赖遥测

修复步骤

1. 修正Application Insights SDK注册代码

简化注册流程,确保依赖追踪与W3C上下文传递启用:

public static IServiceCollection RegisterAppInsightsTelemetry(
    this IServiceCollection serviceCollection,
    Action<ApplicationInsightsServiceOptions>? modifyOptions = null)
{
    var options = new ApplicationInsightsServiceOptions();
    serviceCollection.Configuration.GetSection("ApplicationInsights").Bind(options);
    
    modifyOptions?.Invoke(options);
    
    // 显式启用核心追踪能力
    options.EnableDependencyTracking = true;
    options.EnableW3CDistributedTracing = true;
    options.EnableAdaptiveSampling = false;
    
    serviceCollection.AddApplicationInsightsTelemetry(options);
    
    // 为HttpClient自动添加依赖追踪拦截器
    serviceCollection.AddHttpClient().AddHttpMessageHandler<DependencyTrackingTelemetryHandler>();
    
    return serviceCollection;
}

2. 移除手动注入traceparent的逻辑

App Insights的DependencyTrackingTelemetryHandler会自动为HttpClient请求添加符合标准的traceparent和tracestate头,无需手动处理:

public abstract class HttpService<T> : IHttpService where T : HttpClientConfiguration
{
    private readonly HttpClient _httpClient;
    private readonly IIdentityAccessor _identityAccessor;
    private readonly HttpClientConfiguration _configuration;
    private readonly IServiceAccountResolver _serviceAccountResolver;

    protected HttpService(
        HttpClient httpClient,
        IIdentityAccessor identityAccessor,
        IOptions<T> options,
        IServiceAccountResolver serviceAccountResolver)
    {
        _httpClient = httpClient ?? throw new ArgumentNullException(nameof(httpClient));
        _identityAccessor = identityAccessor;
        _serviceAccountResolver = serviceAccountResolver ?? throw new ArgumentNullException(nameof(serviceAccountResolver));
        _configuration = options?.Value ?? throw new ArgumentNullException(nameof(options));
        
        // 移除原InjectHeaders相关逻辑
    }
}

3. 验证多虚拟应用的配置一致性

确保App Service下所有虚拟应用的ApplicationInsights连接字符串配置正确,且Program.cs中SDK初始化逻辑统一:

var builder = WebApplication.CreateBuilder(args);
builder.Services.RegisterAppInsightsTelemetry();
// 其他服务注册与中间件配置
var app = builder.Build();
app.Run();

4. 验证追踪上下文传递

在Web API中添加测试接口,确认Activity.Current正确获取APIM传递的W3C上下文:

app.MapGet("/trace-check", () =>
{
    var traceId = Activity.Current?.TraceId.ToString();
    var parentId = Activity.Current?.ParentId;
    return Results.Ok(new { TraceId = traceId, ParentId = parentId });
});

调用接口后,检查返回的ParentId是否与APIM生成的traceparent中的parent-id一致。


额外验证项

  • 确保所有服务统一使用W3C分布式追踪模式,避免混合旧版request-id头
  • 检查Application Insights的关联映射,确认traceId在APIM、App Service、依赖服务之间保持一致
  • 确认DependencyTrackingTelemetryHandler已被正确注入到HttpClient管道中

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.15 06:02:29