Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Instrumentation.Process] Dropping Snapshot #718

Merged
merged 7 commits into from
Oct 21, 2022
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
80 changes: 15 additions & 65 deletions src/OpenTelemetry.Instrumentation.Process/ProcessMetrics.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,6 @@ internal sealed class ProcessMetrics
internal readonly Meter MeterInstance = new(AssemblyName.Name, AssemblyName.Version.ToString());

private readonly Diagnostics.Process currentProcess = Diagnostics.Process.GetCurrentProcess();
private double? memoryUsageBytes;
private double? virtualMemoryUsageBytes;
private double? userProcessorTimeSeconds;
private double? privilegedProcessorTimeSeconds;
private int? numberOfThreads;
private IEnumerable<Measurement<double>> cpuUtilization;

// vars for calculating CPU utilization
private DateTime lastCollectionTimeUtc;
Expand All @@ -51,14 +45,8 @@ public ProcessMetrics(ProcessInstrumentationOptions options)
"process.memory.usage",
() =>
{
if (!this.memoryUsageBytes.HasValue)
{
this.Snapshot();
}

var value = this.memoryUsageBytes.Value;
this.memoryUsageBytes = null;
return value;
this.currentProcess.Refresh();
return this.currentProcess.WorkingSet64;
},
unit: "By",
description: "The amount of physical memory allocated for this process.");
Expand All @@ -68,14 +56,8 @@ public ProcessMetrics(ProcessInstrumentationOptions options)
"process.memory.virtual",
() =>
{
if (!this.virtualMemoryUsageBytes.HasValue)
{
this.Snapshot();
}

var value = this.virtualMemoryUsageBytes.Value;
this.virtualMemoryUsageBytes = null;
return value;
this.currentProcess.Refresh();
reyang marked this conversation as resolved.
Show resolved Hide resolved
return this.currentProcess.PrivateMemorySize64;
},
unit: "By",
description: "The amount of virtual memory allocated for this process that cannot be shared with other processes.");
Expand All @@ -84,20 +66,11 @@ public ProcessMetrics(ProcessInstrumentationOptions options)
"process.cpu.time",
() =>
{
if (!this.userProcessorTimeSeconds.HasValue || !this.privilegedProcessorTimeSeconds.HasValue)
{
this.Snapshot();
}

var userProcessorTimeSecondsValue = this.userProcessorTimeSeconds.Value;
var privilegedProcessorTimeSecondsValue = this.privilegedProcessorTimeSeconds.Value;
this.userProcessorTimeSeconds = null;
this.privilegedProcessorTimeSeconds = null;

this.currentProcess.Refresh();
return new[]
{
new Measurement<double>(userProcessorTimeSecondsValue, new KeyValuePair<string, object?>("state", "user")),
new Measurement<double>(privilegedProcessorTimeSecondsValue, new KeyValuePair<string, object?>("state", "system")),
new Measurement<double>(this.currentProcess.UserProcessorTime.TotalSeconds, new KeyValuePair<string, object?>("state", "user")),
new Measurement<double>(this.currentProcess.PrivilegedProcessorTime.TotalSeconds, new KeyValuePair<string, object?>("state", "system")),
};
},
unit: "s",
Expand All @@ -107,14 +80,8 @@ public ProcessMetrics(ProcessInstrumentationOptions options)
"process.cpu.utilization",
() =>
{
if (this.cpuUtilization == null)
{
this.Snapshot();
}

var value = this.cpuUtilization;
this.cpuUtilization = null;
return value;
this.currentProcess.Refresh();
return this.GetCpuUtilization();
},
unit: "1",
description: "Difference in process.cpu.time since the last measurement, divided by the elapsed time and number of CPUs available to the process.");
Expand All @@ -124,44 +91,27 @@ public ProcessMetrics(ProcessInstrumentationOptions options)
"process.threads",
() =>
{
if (!this.numberOfThreads.HasValue)
{
this.Snapshot();
}

var value = this.numberOfThreads.Value;
this.numberOfThreads = null;
return value;
this.currentProcess.Refresh();
return this.currentProcess.Threads.Count;
},
unit: "{threads}",
description: "Process threads count.");
}

private void Snapshot()
{
this.currentProcess.Refresh();
this.memoryUsageBytes = this.currentProcess.WorkingSet64;
this.virtualMemoryUsageBytes = this.currentProcess.PrivateMemorySize64;
this.userProcessorTimeSeconds = this.currentProcess.UserProcessorTime.TotalSeconds;
this.privilegedProcessorTimeSeconds = this.currentProcess.PrivilegedProcessorTime.TotalSeconds;
this.cpuUtilization = this.GetCpuUtilization();
this.numberOfThreads = this.currentProcess.Threads.Count;
}

private IEnumerable<Measurement<double>> GetCpuUtilization()
{
var elapsedTimeForAllCpus = (DateTime.UtcNow - this.lastCollectionTimeUtc).TotalSeconds * Environment.ProcessorCount;
var userProcessorUtilization = (this.userProcessorTimeSeconds - this.lastCollectedUserProcessorTime) / elapsedTimeForAllCpus;
var privilegedProcessorUtilization = (this.privilegedProcessorTimeSeconds - this.lastCollectedPrivilegedProcessorTime) / elapsedTimeForAllCpus;
var userProcessorUtilization = (this.currentProcess.UserProcessorTime.TotalSeconds - this.lastCollectedUserProcessorTime) / elapsedTimeForAllCpus;
var privilegedProcessorUtilization = (this.currentProcess.PrivilegedProcessorTime.TotalSeconds - this.lastCollectedPrivilegedProcessorTime) / elapsedTimeForAllCpus;

this.lastCollectionTimeUtc = DateTime.UtcNow;
this.lastCollectedUserProcessorTime = this.currentProcess.UserProcessorTime.TotalSeconds;
this.lastCollectedPrivilegedProcessorTime = this.currentProcess.PrivilegedProcessorTime.TotalSeconds;

return new[]
{
new Measurement<double>(Math.Min(userProcessorUtilization.Value, 1D), new KeyValuePair<string, object?>("state", "user")),
new Measurement<double>(Math.Min(privilegedProcessorUtilization.Value, 1D), new KeyValuePair<string, object?>("state", "system")),
new Measurement<double>(Math.Min(userProcessorUtilization, 1D), new KeyValuePair<string, object?>("state", "user")),
new Measurement<double>(Math.Min(privilegedProcessorUtilization, 1D), new KeyValuePair<string, object?>("state", "system")),
};
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public void ProcessMetricsAreCaptured()
var cpuUtilizationMetric = exportedItems.FirstOrDefault(i => i.Name == "process.cpu.utilization");
Assert.NotNull(cpuUtilizationMetric);
var threadMetric = exportedItems.FirstOrDefault(i => i.Name == "process.threads");
Assert.NotNull(cpuTimeMetric);
Assert.NotNull(threadMetric);
}

[Fact]
Expand Down Expand Up @@ -158,7 +158,7 @@ private static double GetValue(Metric metric)
{
if (metric.MetricType.IsGauge())
{
sum += metricPoint.GetGaugeLastValueDouble();
sum += metricPoint.GetGaugeLastValueLong();
utpilla marked this conversation as resolved.
Show resolved Hide resolved
}
else if (metric.MetricType.IsDouble())
{
Expand Down