Azure App Service中.NET 6 Web API无法关闭Application Insights采样
问题描述
我有一个运行在Azure App Service上的.NET 6 ASP.NET Web API应用,未启用固定采样,也没有applicationinsights.json文件。已在ConfigureServices方法中添加代码禁用Adaptive Sampling,代码片段如下:
if (!string.IsNullOrEmpty(configuration["ApplicationInsights:InstrumentationKey"])) { services.AddApplicationInsightsTelemetry(options => { if (webHostEnvironment.IsDevelopment()) { options.DeveloperMode = true; options.EnableDebugLogger = false; } options.EnableAdaptiveSampling = false; options.InstrumentationKey = configuration["ApplicationInsights:InstrumentationKey"]; }); services.AddApplicationInsightsTelemetryProcessor<FilterProcessor>(); services.AddSingleton<ITelemetryInitializer, NopContextInitializer>(); }
Azure端Application Insights的Ingestion Sampling设置如下:
运行以下Kusto查询时仍能看到采样现象:
union requests,dependencies,pageViews,browserTimings,exceptions,traces | where timestamp > ago(1d) | summarize RetainedPercentage = 100/avg(itemCount) by bin(timestamp, 1h), itemType
该应用此前为.NET Core 3.1版本时就无法关闭采样,升级到.NET 6后问题依旧,请问可能的原因是什么?
可能的原因及排查建议
- 固定采样未禁用:Application Insights SDK默认会启用固定采样(Fixed Sampling),仅禁用自适应采样不足以完全关闭采样。需要在配置选项中额外添加
options.EnableFixedSampling = false,确保两种采样机制都被关闭。 - 自定义遥测处理器/初始化器的影响:你添加的
FilterProcessor或NopContextInitializer可能在处理遥测时手动修改了itemCount属性,或是过滤数据后调整了计数,导致出现类似采样的现象。检查这两个自定义类的代码,确认是否存在相关逻辑。 - App Service环境变量强制采样:Azure App Service的应用设置中可能存在
APPINSIGHTS_SAMPLING_PERCENTAGE这类环境变量,强制启用了采样。前往App Service的“配置”->“应用程序设置”页面,检查并删除或设置为100这类强制采样的配置项。 - SDK版本存在bug:旧版本的Microsoft.ApplicationInsights.AspNetCore包可能存在采样逻辑的缺陷,即使配置禁用也会生效。检查当前使用的SDK版本,升级至最新稳定版后重试。
- Ingestion Sampling配置未同步:虽然截图显示禁用了Ingestion Sampling,但可能存在配置延迟或区域同步问题。尝试重新保存设置,或先启用再禁用一次,触发配置刷新。
- 遥测数据来源混杂:查询结果中的采样现象可能来自其他关联到该Application Insights资源的服务(如前端监控、其他微服务)。在Kusto查询中添加
cloud_RoleName过滤,仅查看当前Web API的遥测数据,验证是否仍存在采样:union requests,dependencies,pageViews,browserTimings,exceptions,traces | where timestamp > ago(1d) and cloud_RoleName == "你的应用角色名称" | summarize RetainedPercentage = 100/avg(itemCount) by bin(timestamp, 1h), itemType
内容的提问来源于stack exchange,提问作者ashwanth sreedharan
相关产品推荐
相关产品推荐

