为什么 Application Insights 性能计数器收集导致高 CPU 使用率

Why is Application Insights Performance Counter Collection causing high CPU usage

我们的性能团队执行的日志记录表明这一行特别是正在杀死我们的 CPU

Microsoft.AI.PerfCounterCollector!Microsoft.ApplicationInsights.Extensibility.PerfCounterCollector.Implementation.PerformanceCounterUtility.ExpandInstanceName()

一种理论是用于识别库中性能计数器的正则表达式是递归的

https://adtmag.com/blogs/dev-watch/2016/07/stack-overflow-crash.aspx

我已经检查了 Perf Counter 的名称,没有什么看起来特别不协调的名称,正则表达式应该可以毫无问题地咀嚼它们。当然在很长一段时间内都没有任何问题。

我现在打开了 Applications Insights 诊断日志记录以尝试观察问题(在测试环境中)

有没有其他人观察到这种情况,我们如何缓解这种情况?
我们确保 DeveloperMode 未设置为打开。

您收集的计数器名称中是否使用了实例占位符?如果实例名称在构建时已知,去掉占位符可能会显着提高性能。例如,而不是

\Process(??APP_WIN32_PROC??)\% Processor Time

尝试使用

\Process(w3wp)\% Processor Time

此外,您总共收集了多少个计数器?

这个答案现在很可能无法使用,因为 AppInsights 2 代码有了很大改进,但在我的例子中,它是两次调用 AddApplicationInsightsTelemetry()。每次调用此方法时它都会添加收集器,并且由于 perf 计数器收集器代码内部缺乏同步,它会创建 CPU 个尖峰。

因此避免多次调用 AddApplicationInsightsTelemetry,或使用 AppInsights 2.x。 (最好的办法是两者都做)。