apm-agent-dotnet
apm-agent-dotnet copied to clipboard
PerformanceCounter .ctor causes hang in the monitored app
In the SystemTotalCpuProvider.cs
constructor we have the following lines:
try
{
_processorTimePerfCounter = new PerformanceCounter(categoryName, "% Processor Time", "_Total");
}
catch...
We debugged in an environment where the call to new PerformanceCounter
never ends. The execution simply stops in that call.
This causes the application to hang. This only happened when the application was running within IIS express.
This only happened with:
- ASP.NET Core (both .NET5 and 6) with in-process hosting model (but not with out of process or simply running from cmd)
- ASP.NET Classic on .NET Framework
In both cases the agent runs in w3wp (everything is in-process) on IIS express.
To things could be changed:
- [x] this
PerformanceCounter
in the current code is instantiated even if we disable metrics collection (because it's in theSystemTotalCpuProvider
ctor and we only check if a specific metric is disabled later). - [ ] fix the root cause of the issue or at avoid the hang.
https://github.com/elastic/apm-agent-dotnet/pull/1725 changes how we initialize PerformanceCounter
- after that PR the issue can be workaround-ed by setting the DisableMetrics
setting.
A short update on this: a new agent version was released (1.16.0
).
With that version, by setting DisableMetrics to system.cpu.total.norm.pct
(or adding it to the list, if you already disabled other metrics) will solve the issue. The side-effect is that the agent won't collect system wide CPU usage metrics, but other than that the agent will run without any other limitation.