in src/ApplicationInsights.Kubernetes/TelemetryInitializers/SimplePerformanceCounterTelemetryInitializer.cs [34:68]
public void Initialize(ITelemetry telemetry)
{
// Short circuit.
if (_options.DisablePerformanceCounters)
{
return;
}
if (telemetry is ISupportMetrics metricsTelemetry)
{
// Write CPU metrics
TimeSpan endCPUTime = GetCPUTime();
if (_lastCPUSample != default)
{
if (_cpuWatch.ElapsedMilliseconds > 0)
{
// A very simple but not that accurate evaluation of how much CPU the process is take out of a core.
double cpuPercentage = (endCPUTime - _lastCPUSample).TotalMilliseconds / (_cpuWatch.ElapsedMilliseconds) / ProcessorCount;
SetMetrics(metricsTelemetry, ProcessProperty.CPUPrecent, cpuPercentage);
}
else
{
_cpuWatch.Start();
}
}
// Snap CPU usage
_lastCPUSample = endCPUTime;
_cpuWatch.Restart();
// Write memory metrics
long memory = GetMemory();
SetMetrics(metricsTelemetry, ProcessProperty.Memory, memory);
}
}