From 7f3621a04154ba99ea75c4e4a5dd0ca9c805a100 Mon Sep 17 00:00:00 2001 From: Yun-Ting Date: Tue, 11 Oct 2022 17:37:30 -0700 Subject: [PATCH 01/13] draft --- .../ProcessMetrics.cs | 70 ++++++++++++++++++- 1 file changed, 67 insertions(+), 3 deletions(-) diff --git a/src/OpenTelemetry.Instrumentation.Process/ProcessMetrics.cs b/src/OpenTelemetry.Instrumentation.Process/ProcessMetrics.cs index 30c9b2a7d8..5d3934babf 100644 --- a/src/OpenTelemetry.Instrumentation.Process/ProcessMetrics.cs +++ b/src/OpenTelemetry.Instrumentation.Process/ProcessMetrics.cs @@ -14,9 +14,10 @@ // limitations under the License. // +using System; using System.Collections.Generic; -using System.Diagnostics.Metrics; -using System.Reflection; +using System.Diagnostics.Metrics; +using System.Reflection; using Diagnostics = System.Diagnostics; namespace OpenTelemetry.Instrumentation.Process; @@ -31,9 +32,18 @@ internal sealed class ProcessMetrics private double? virtualMemoryUsage; private double? userProcessorTime; private double? privilegedProcessorTime; + private int? numberOfThreads; + private IEnumerable> cpuUtilization; + + // vars for calculating CPU utilization + private DateTime collectionTimeStamp; + private double collectionUserProcessorTime; + private double collectionPrivilegedProcessorTime; public ProcessMetrics(ProcessInstrumentationOptions options) { + this.collectionTimeStamp = this.currentProcess.StartTime; + // TODO: change to ObservableUpDownCounter this.MeterInstance.CreateObservableGauge( "process.memory.usage", @@ -89,7 +99,40 @@ public ProcessMetrics(ProcessInstrumentationOptions options) }; }, unit: "s", - description: "Total CPU seconds broken down by different states."); + description: "Total CPU seconds broken down by different states."); + + this.MeterInstance.CreateObservableGauge( + "process.cpu.utilization", + () => + { + if (this.cpuUtilization == null) + { + this.Snapshot(); + } + + var value = this.cpuUtilization; + this.cpuUtilization = null; + return value; + }, + 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."); + + // TODO: change to ObservableUpDownCounter + this.MeterInstance.CreateObservableGauge( + "process.threads", + () => + { + if (!this.numberOfThreads.HasValue) + { + this.Snapshot(); + } + + var value = this.numberOfThreads.Value; + this.numberOfThreads = null; + return value; + }, + unit: "{threads}", + description: "Process threads count."); } private void Snapshot() @@ -99,5 +142,26 @@ private void Snapshot() this.virtualMemoryUsage = this.currentProcess.PrivateMemorySize64; this.userProcessorTime = this.currentProcess.UserProcessorTime.TotalSeconds; this.privilegedProcessorTime = this.currentProcess.PrivilegedProcessorTime.TotalSeconds; + this.cpuUtilization = this.GetCpuUtilization(); + this.numberOfThreads = this.currentProcess.Threads.Count; + } + + private IEnumerable> GetCpuUtilization() + { + var userProcessorUtilization = (this.currentProcess.UserProcessorTime.TotalSeconds - this.collectionUserProcessorTime) / + ((DateTime.UtcNow - this.collectionTimeStamp.ToUniversalTime()).TotalSeconds * Environment.ProcessorCount); + + var privilegedProcessorUtilization = (this.currentProcess.PrivilegedProcessorTime.TotalSeconds - this.collectionPrivilegedProcessorTime) / + ((DateTime.UtcNow - this.collectionTimeStamp.ToUniversalTime()).TotalSeconds * Environment.ProcessorCount); + + this.collectionTimeStamp = DateTime.UtcNow; + this.collectionUserProcessorTime = this.currentProcess.UserProcessorTime.TotalSeconds; + this.collectionPrivilegedProcessorTime = this.currentProcess.PrivilegedProcessorTime.TotalSeconds; + + return new[] + { + new Measurement(userProcessorUtilization, new KeyValuePair("state", "user")), + new Measurement(privilegedProcessorUtilization, new KeyValuePair("state", "system")), + }; } } From b1e5cdfefaf46dfadc1c4f8885f44cbdc2f8d331 Mon Sep 17 00:00:00 2001 From: Yun-Ting Date: Tue, 11 Oct 2022 18:15:00 -0700 Subject: [PATCH 02/13] test --- .../ProcessMetricsTests.cs | 45 ++++++++++++++++++- 1 file changed, 44 insertions(+), 1 deletion(-) diff --git a/test/OpenTelemetry.Instrumentation.Process.Tests/ProcessMetricsTests.cs b/test/OpenTelemetry.Instrumentation.Process.Tests/ProcessMetricsTests.cs index 82631a34ab..454f76f529 100644 --- a/test/OpenTelemetry.Instrumentation.Process.Tests/ProcessMetricsTests.cs +++ b/test/OpenTelemetry.Instrumentation.Process.Tests/ProcessMetricsTests.cs @@ -37,13 +37,18 @@ public void ProcessMetricsAreCaptured() meterProvider.ForceFlush(MaxTimeToAllowForFlush); - Assert.True(exportedItems.Count == 3); + Assert.True(exportedItems.Count == 5); var physicalMemoryMetric = exportedItems.FirstOrDefault(i => i.Name == "process.memory.usage"); Assert.NotNull(physicalMemoryMetric); var virtualMemoryMetric = exportedItems.FirstOrDefault(i => i.Name == "process.memory.virtual"); Assert.NotNull(virtualMemoryMetric); var cpuTimeMetric = exportedItems.FirstOrDefault(i => i.Name == "process.cpu.time"); Assert.NotNull(cpuTimeMetric); + 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); + } [Fact] @@ -83,6 +88,44 @@ public void CpuTimeMetricsAreCaptured() Assert.True(systemTimeCaptured); } + [Fact] + public void CpuUtilizationMetricsAreCaptured() + { + var exportedItems = new List(); + using var meterProvider = Sdk.CreateMeterProviderBuilder() + .AddProcessInstrumentation() + .AddInMemoryExporter(exportedItems) + .Build(); + + meterProvider.ForceFlush(MaxTimeToAllowForFlush); + + var cpuUtilizationMetric = exportedItems.FirstOrDefault(i => i.Name == "process.cpu.utilization"); + Assert.NotNull(cpuUtilizationMetric); + Assert.True(GetValue(cpuUtilizationMetric) >= 0 && GetValue(cpuUtilizationMetric) <= 1); + + var userCpuUtilizationCaptured = false; + var systemCpuUtilizationCaptured = false; + + var iter = cpuUtilizationMetric.GetMetricPoints().GetEnumerator(); + while (iter.MoveNext() && (!userCpuUtilizationCaptured || !systemCpuUtilizationCaptured)) + { + foreach (var tag in iter.Current.Tags) + { + if (tag.Key == "state" && tag.Value.ToString() == "user") + { + userCpuUtilizationCaptured = true; + } + else if (tag.Key == "state" && tag.Value.ToString() == "system") + { + systemCpuUtilizationCaptured = true; + } + } + } + + Assert.True(userCpuUtilizationCaptured); + Assert.True(systemCpuUtilizationCaptured); + } + [Fact] public void CheckValidGaugeValueWhen2MeterProviderInstancesHaveTheSameMeterName() { From 180f8d63cdbd7cba641840f7b7cd0c9353a06805 Mon Sep 17 00:00:00 2001 From: Yun-Ting Date: Tue, 11 Oct 2022 18:17:04 -0700 Subject: [PATCH 03/13] update --- .../ProcessMetricsTests.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/OpenTelemetry.Instrumentation.Process.Tests/ProcessMetricsTests.cs b/test/OpenTelemetry.Instrumentation.Process.Tests/ProcessMetricsTests.cs index 454f76f529..2131454e4b 100644 --- a/test/OpenTelemetry.Instrumentation.Process.Tests/ProcessMetricsTests.cs +++ b/test/OpenTelemetry.Instrumentation.Process.Tests/ProcessMetricsTests.cs @@ -89,7 +89,7 @@ public void CpuTimeMetricsAreCaptured() } [Fact] - public void CpuUtilizationMetricsAreCaptured() + public void CpuUtilizationMetricsAreCapturedAndInRange() { var exportedItems = new List(); using var meterProvider = Sdk.CreateMeterProviderBuilder() From ff0bffedfb1ca0dde9c0e2be1b5b5dc9bcd5f929 Mon Sep 17 00:00:00 2001 From: Yun-Ting Date: Tue, 11 Oct 2022 18:18:55 -0700 Subject: [PATCH 04/13] sanity check --- .../ProcessMetrics.cs | 38 +++++++++---------- .../ProcessMetricsTests.cs | 10 ++--- 2 files changed, 24 insertions(+), 24 deletions(-) diff --git a/src/OpenTelemetry.Instrumentation.Process/ProcessMetrics.cs b/src/OpenTelemetry.Instrumentation.Process/ProcessMetrics.cs index 5d3934babf..42e124ae58 100644 --- a/src/OpenTelemetry.Instrumentation.Process/ProcessMetrics.cs +++ b/src/OpenTelemetry.Instrumentation.Process/ProcessMetrics.cs @@ -14,10 +14,10 @@ // limitations under the License. // -using System; +using System; using System.Collections.Generic; -using System.Diagnostics.Metrics; -using System.Reflection; +using System.Diagnostics.Metrics; +using System.Reflection; using Diagnostics = System.Diagnostics; namespace OpenTelemetry.Instrumentation.Process; @@ -99,22 +99,22 @@ public ProcessMetrics(ProcessInstrumentationOptions options) }; }, unit: "s", - description: "Total CPU seconds broken down by different states."); - - this.MeterInstance.CreateObservableGauge( - "process.cpu.utilization", - () => - { - if (this.cpuUtilization == null) - { - this.Snapshot(); - } - - var value = this.cpuUtilization; - this.cpuUtilization = null; - return value; - }, - unit: "1", + description: "Total CPU seconds broken down by different states."); + + this.MeterInstance.CreateObservableGauge( + "process.cpu.utilization", + () => + { + if (this.cpuUtilization == null) + { + this.Snapshot(); + } + + var value = this.cpuUtilization; + this.cpuUtilization = null; + return value; + }, + 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."); // TODO: change to ObservableUpDownCounter diff --git a/test/OpenTelemetry.Instrumentation.Process.Tests/ProcessMetricsTests.cs b/test/OpenTelemetry.Instrumentation.Process.Tests/ProcessMetricsTests.cs index 2131454e4b..9d560e3885 100644 --- a/test/OpenTelemetry.Instrumentation.Process.Tests/ProcessMetricsTests.cs +++ b/test/OpenTelemetry.Instrumentation.Process.Tests/ProcessMetricsTests.cs @@ -90,15 +90,15 @@ public void CpuTimeMetricsAreCaptured() [Fact] public void CpuUtilizationMetricsAreCapturedAndInRange() - { + { var exportedItems = new List(); using var meterProvider = Sdk.CreateMeterProviderBuilder() .AddProcessInstrumentation() .AddInMemoryExporter(exportedItems) - .Build(); - - meterProvider.ForceFlush(MaxTimeToAllowForFlush); - + .Build(); + + meterProvider.ForceFlush(MaxTimeToAllowForFlush); + var cpuUtilizationMetric = exportedItems.FirstOrDefault(i => i.Name == "process.cpu.utilization"); Assert.NotNull(cpuUtilizationMetric); Assert.True(GetValue(cpuUtilizationMetric) >= 0 && GetValue(cpuUtilizationMetric) <= 1); From 16a884a080550d2ee705d8c9286372cff1b7b732 Mon Sep 17 00:00:00 2001 From: Yun-Ting Date: Tue, 11 Oct 2022 18:27:57 -0700 Subject: [PATCH 05/13] style --- .../ProcessMetricsTests.cs | 2 -- 1 file changed, 2 deletions(-) diff --git a/test/OpenTelemetry.Instrumentation.Process.Tests/ProcessMetricsTests.cs b/test/OpenTelemetry.Instrumentation.Process.Tests/ProcessMetricsTests.cs index 9d560e3885..9a45b9c669 100644 --- a/test/OpenTelemetry.Instrumentation.Process.Tests/ProcessMetricsTests.cs +++ b/test/OpenTelemetry.Instrumentation.Process.Tests/ProcessMetricsTests.cs @@ -15,7 +15,6 @@ // using System.Collections.Generic; -using System.Diagnostics.Metrics; using System.Linq; using OpenTelemetry.Metrics; using Xunit; @@ -48,7 +47,6 @@ public void ProcessMetricsAreCaptured() Assert.NotNull(cpuUtilizationMetric); var threadMetric = exportedItems.FirstOrDefault(i => i.Name == "process.threads"); Assert.NotNull(cpuTimeMetric); - } [Fact] From d30c5b735b4d22ab1e774ef098f7b3b157e2a33c Mon Sep 17 00:00:00 2001 From: Yun-Ting Date: Thu, 13 Oct 2022 11:05:02 -0700 Subject: [PATCH 06/13] comments --- examples/runtime-instrumentation/Program.cs | 2 +- .../runtime-instrumentation.csproj | 1 + .../ProcessMetrics.cs | 26 +++++++++---------- 3 files changed, 15 insertions(+), 14 deletions(-) diff --git a/examples/runtime-instrumentation/Program.cs b/examples/runtime-instrumentation/Program.cs index b4bdd9fc56..27048998f3 100644 --- a/examples/runtime-instrumentation/Program.cs +++ b/examples/runtime-instrumentation/Program.cs @@ -22,7 +22,7 @@ public class Program public static void Main() { using var meterProvider = Sdk.CreateMeterProviderBuilder() - .AddRuntimeInstrumentation() + .AddProcessInstrumentation() .AddPrometheusExporter(options => { options.StartHttpListener = true; diff --git a/examples/runtime-instrumentation/runtime-instrumentation.csproj b/examples/runtime-instrumentation/runtime-instrumentation.csproj index eb026e5dca..507f312a0a 100644 --- a/examples/runtime-instrumentation/runtime-instrumentation.csproj +++ b/examples/runtime-instrumentation/runtime-instrumentation.csproj @@ -8,5 +8,6 @@ + diff --git a/src/OpenTelemetry.Instrumentation.Process/ProcessMetrics.cs b/src/OpenTelemetry.Instrumentation.Process/ProcessMetrics.cs index 42e124ae58..ca0d35fc32 100644 --- a/src/OpenTelemetry.Instrumentation.Process/ProcessMetrics.cs +++ b/src/OpenTelemetry.Instrumentation.Process/ProcessMetrics.cs @@ -36,13 +36,13 @@ internal sealed class ProcessMetrics private IEnumerable> cpuUtilization; // vars for calculating CPU utilization - private DateTime collectionTimeStamp; - private double collectionUserProcessorTime; - private double collectionPrivilegedProcessorTime; + private DateTime lastCollectionTimeStamp; + private double lastCollectionUserProcessorTime; + private double lastCollectionPrivilegedProcessorTime; public ProcessMetrics(ProcessInstrumentationOptions options) { - this.collectionTimeStamp = this.currentProcess.StartTime; + this.lastCollectionTimeStamp = this.currentProcess.StartTime; // TODO: change to ObservableUpDownCounter this.MeterInstance.CreateObservableGauge( @@ -148,20 +148,20 @@ private void Snapshot() private IEnumerable> GetCpuUtilization() { - var userProcessorUtilization = (this.currentProcess.UserProcessorTime.TotalSeconds - this.collectionUserProcessorTime) / - ((DateTime.UtcNow - this.collectionTimeStamp.ToUniversalTime()).TotalSeconds * Environment.ProcessorCount); + var userProcessorUtilization = (this.userProcessorTime - this.lastCollectionUserProcessorTime) / + ((DateTime.UtcNow - this.lastCollectionTimeStamp.ToUniversalTime()).TotalSeconds * Environment.ProcessorCount); - var privilegedProcessorUtilization = (this.currentProcess.PrivilegedProcessorTime.TotalSeconds - this.collectionPrivilegedProcessorTime) / - ((DateTime.UtcNow - this.collectionTimeStamp.ToUniversalTime()).TotalSeconds * Environment.ProcessorCount); + var privilegedProcessorUtilization = (this.privilegedProcessorTime - this.lastCollectionPrivilegedProcessorTime) / + ((DateTime.UtcNow - this.lastCollectionTimeStamp.ToUniversalTime()).TotalSeconds * Environment.ProcessorCount); - this.collectionTimeStamp = DateTime.UtcNow; - this.collectionUserProcessorTime = this.currentProcess.UserProcessorTime.TotalSeconds; - this.collectionPrivilegedProcessorTime = this.currentProcess.PrivilegedProcessorTime.TotalSeconds; + this.lastCollectionTimeStamp = DateTime.UtcNow; + this.lastCollectionUserProcessorTime = this.currentProcess.UserProcessorTime.TotalSeconds; + this.lastCollectionPrivilegedProcessorTime = this.currentProcess.PrivilegedProcessorTime.TotalSeconds; return new[] { - new Measurement(userProcessorUtilization, new KeyValuePair("state", "user")), - new Measurement(privilegedProcessorUtilization, new KeyValuePair("state", "system")), + new Measurement(userProcessorUtilization.Value, new KeyValuePair("state", "user")), + new Measurement(privilegedProcessorUtilization.Value, new KeyValuePair("state", "system")), }; } } From 83c57628b4711be767c3006f0fc0da5a995ad9d8 Mon Sep 17 00:00:00 2001 From: Yun-Ting Date: Thu, 13 Oct 2022 16:18:21 -0700 Subject: [PATCH 07/13] comments --- .../ProcessMetrics.cs | 60 +++++++++---------- .../ProcessMetricsTests.cs | 3 +- 2 files changed, 30 insertions(+), 33 deletions(-) diff --git a/src/OpenTelemetry.Instrumentation.Process/ProcessMetrics.cs b/src/OpenTelemetry.Instrumentation.Process/ProcessMetrics.cs index ca0d35fc32..377f866488 100644 --- a/src/OpenTelemetry.Instrumentation.Process/ProcessMetrics.cs +++ b/src/OpenTelemetry.Instrumentation.Process/ProcessMetrics.cs @@ -28,34 +28,32 @@ internal sealed class ProcessMetrics internal readonly Meter MeterInstance = new(AssemblyName.Name, AssemblyName.Version.ToString()); private readonly Diagnostics.Process currentProcess = Diagnostics.Process.GetCurrentProcess(); - private double? memoryUsage; - private double? virtualMemoryUsage; - private double? userProcessorTime; - private double? privilegedProcessorTime; + private double? memoryUsageBytes; + private double? virtualMemoryUsageBytes; + private double? userProcessorTimeSeconds; + private double? privilegedProcessorTimeSeconds; private int? numberOfThreads; private IEnumerable> cpuUtilization; // vars for calculating CPU utilization - private DateTime lastCollectionTimeStamp; + private DateTime lastCollectionTimeStampUtc = DateTime.UtcNow; private double lastCollectionUserProcessorTime; private double lastCollectionPrivilegedProcessorTime; public ProcessMetrics(ProcessInstrumentationOptions options) { - this.lastCollectionTimeStamp = this.currentProcess.StartTime; - // TODO: change to ObservableUpDownCounter this.MeterInstance.CreateObservableGauge( "process.memory.usage", () => { - if (!this.memoryUsage.HasValue) + if (!this.memoryUsageBytes.HasValue) { this.Snapshot(); } - var value = this.memoryUsage.Value; - this.memoryUsage = null; + var value = this.memoryUsageBytes.Value; + this.memoryUsageBytes = null; return value; }, unit: "By", @@ -66,13 +64,13 @@ public ProcessMetrics(ProcessInstrumentationOptions options) "process.memory.virtual", () => { - if (!this.virtualMemoryUsage.HasValue) + if (!this.virtualMemoryUsageBytes.HasValue) { this.Snapshot(); } - var value = this.virtualMemoryUsage.Value; - this.virtualMemoryUsage = null; + var value = this.virtualMemoryUsageBytes.Value; + this.virtualMemoryUsageBytes = null; return value; }, unit: "By", @@ -82,20 +80,20 @@ public ProcessMetrics(ProcessInstrumentationOptions options) "process.cpu.time", () => { - if (!this.userProcessorTime.HasValue || !this.privilegedProcessorTime.HasValue) + if (!this.userProcessorTimeSeconds.HasValue || !this.privilegedProcessorTimeSeconds.HasValue) { this.Snapshot(); } - var userProcessorTimeValue = this.userProcessorTime.Value; - var privilegedProcessorTimeValue = this.privilegedProcessorTime.Value; - this.userProcessorTime = null; - this.privilegedProcessorTime = null; + var userProcessorTimeSecondsValue = this.userProcessorTimeSeconds.Value; + var privilegedProcessorTimeSecondsValue = this.privilegedProcessorTimeSeconds.Value; + this.userProcessorTimeSeconds = null; + this.privilegedProcessorTimeSeconds = null; return new[] { - new Measurement(userProcessorTimeValue, new KeyValuePair("state", "user")), - new Measurement(privilegedProcessorTimeValue, new KeyValuePair("state", "system")), + new Measurement(userProcessorTimeSecondsValue, new KeyValuePair("state", "user")), + new Measurement(privilegedProcessorTimeSecondsValue, new KeyValuePair("state", "system")), }; }, unit: "s", @@ -138,30 +136,30 @@ public ProcessMetrics(ProcessInstrumentationOptions options) private void Snapshot() { this.currentProcess.Refresh(); - this.memoryUsage = this.currentProcess.WorkingSet64; - this.virtualMemoryUsage = this.currentProcess.PrivateMemorySize64; - this.userProcessorTime = this.currentProcess.UserProcessorTime.TotalSeconds; - this.privilegedProcessorTime = this.currentProcess.PrivilegedProcessorTime.TotalSeconds; + 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> GetCpuUtilization() { - var userProcessorUtilization = (this.userProcessorTime - this.lastCollectionUserProcessorTime) / - ((DateTime.UtcNow - this.lastCollectionTimeStamp.ToUniversalTime()).TotalSeconds * Environment.ProcessorCount); + var userProcessorUtilization = (this.userProcessorTimeSeconds - this.lastCollectionUserProcessorTime) / + ((DateTime.UtcNow - this.lastCollectionTimeStampUtc).TotalSeconds * Environment.ProcessorCount); - var privilegedProcessorUtilization = (this.privilegedProcessorTime - this.lastCollectionPrivilegedProcessorTime) / - ((DateTime.UtcNow - this.lastCollectionTimeStamp.ToUniversalTime()).TotalSeconds * Environment.ProcessorCount); + var privilegedProcessorUtilization = (this.privilegedProcessorTimeSeconds - this.lastCollectionPrivilegedProcessorTime) / + ((DateTime.UtcNow - this.lastCollectionTimeStampUtc).TotalSeconds * Environment.ProcessorCount); - this.lastCollectionTimeStamp = DateTime.UtcNow; + this.lastCollectionTimeStampUtc = DateTime.UtcNow; this.lastCollectionUserProcessorTime = this.currentProcess.UserProcessorTime.TotalSeconds; this.lastCollectionPrivilegedProcessorTime = this.currentProcess.PrivilegedProcessorTime.TotalSeconds; return new[] { - new Measurement(userProcessorUtilization.Value, new KeyValuePair("state", "user")), - new Measurement(privilegedProcessorUtilization.Value, new KeyValuePair("state", "system")), + new Measurement(Math.Min(userProcessorUtilization.Value, 1D), new KeyValuePair("state", "user")), + new Measurement(Math.Min(privilegedProcessorUtilization.Value, 1D), new KeyValuePair("state", "system")), }; } } diff --git a/test/OpenTelemetry.Instrumentation.Process.Tests/ProcessMetricsTests.cs b/test/OpenTelemetry.Instrumentation.Process.Tests/ProcessMetricsTests.cs index 9a45b9c669..c269f58aa8 100644 --- a/test/OpenTelemetry.Instrumentation.Process.Tests/ProcessMetricsTests.cs +++ b/test/OpenTelemetry.Instrumentation.Process.Tests/ProcessMetricsTests.cs @@ -87,7 +87,7 @@ public void CpuTimeMetricsAreCaptured() } [Fact] - public void CpuUtilizationMetricsAreCapturedAndInRange() + public void CpuUtilizationMetricsAreCaptured() { var exportedItems = new List(); using var meterProvider = Sdk.CreateMeterProviderBuilder() @@ -99,7 +99,6 @@ public void CpuUtilizationMetricsAreCapturedAndInRange() var cpuUtilizationMetric = exportedItems.FirstOrDefault(i => i.Name == "process.cpu.utilization"); Assert.NotNull(cpuUtilizationMetric); - Assert.True(GetValue(cpuUtilizationMetric) >= 0 && GetValue(cpuUtilizationMetric) <= 1); var userCpuUtilizationCaptured = false; var systemCpuUtilizationCaptured = false; From 5d970e08200b2bf263d5f9f21ed4d1107d4da11d Mon Sep 17 00:00:00 2001 From: Yun-Ting Date: Thu, 13 Oct 2022 16:27:54 -0700 Subject: [PATCH 08/13] revert test files --- examples/runtime-instrumentation/Program.cs | 2 +- examples/runtime-instrumentation/runtime-instrumentation.csproj | 1 - 2 files changed, 1 insertion(+), 2 deletions(-) diff --git a/examples/runtime-instrumentation/Program.cs b/examples/runtime-instrumentation/Program.cs index 27048998f3..b4bdd9fc56 100644 --- a/examples/runtime-instrumentation/Program.cs +++ b/examples/runtime-instrumentation/Program.cs @@ -22,7 +22,7 @@ public class Program public static void Main() { using var meterProvider = Sdk.CreateMeterProviderBuilder() - .AddProcessInstrumentation() + .AddRuntimeInstrumentation() .AddPrometheusExporter(options => { options.StartHttpListener = true; diff --git a/examples/runtime-instrumentation/runtime-instrumentation.csproj b/examples/runtime-instrumentation/runtime-instrumentation.csproj index 507f312a0a..eb026e5dca 100644 --- a/examples/runtime-instrumentation/runtime-instrumentation.csproj +++ b/examples/runtime-instrumentation/runtime-instrumentation.csproj @@ -8,6 +8,5 @@ - From 05d81e8a5a5d6703450e008536a6083cddd35125 Mon Sep 17 00:00:00 2001 From: Yun-Ting Date: Thu, 13 Oct 2022 16:31:12 -0700 Subject: [PATCH 09/13] name --- .../ProcessMetrics.cs | 20 +++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/src/OpenTelemetry.Instrumentation.Process/ProcessMetrics.cs b/src/OpenTelemetry.Instrumentation.Process/ProcessMetrics.cs index 377f866488..28114920d4 100644 --- a/src/OpenTelemetry.Instrumentation.Process/ProcessMetrics.cs +++ b/src/OpenTelemetry.Instrumentation.Process/ProcessMetrics.cs @@ -36,9 +36,9 @@ internal sealed class ProcessMetrics private IEnumerable> cpuUtilization; // vars for calculating CPU utilization - private DateTime lastCollectionTimeStampUtc = DateTime.UtcNow; - private double lastCollectionUserProcessorTime; - private double lastCollectionPrivilegedProcessorTime; + private DateTime lastCollectedTimeStampUtc = DateTime.UtcNow; + private double lastCollectedUserProcessorTime; + private double lastCollectedPrivilegedProcessorTime; public ProcessMetrics(ProcessInstrumentationOptions options) { @@ -146,15 +146,15 @@ private void Snapshot() private IEnumerable> GetCpuUtilization() { - var userProcessorUtilization = (this.userProcessorTimeSeconds - this.lastCollectionUserProcessorTime) / - ((DateTime.UtcNow - this.lastCollectionTimeStampUtc).TotalSeconds * Environment.ProcessorCount); + var userProcessorUtilization = (this.userProcessorTimeSeconds - this.lastCollectedUserProcessorTime) / + ((DateTime.UtcNow - this.lastCollectedTimeStampUtc).TotalSeconds * Environment.ProcessorCount); - var privilegedProcessorUtilization = (this.privilegedProcessorTimeSeconds - this.lastCollectionPrivilegedProcessorTime) / - ((DateTime.UtcNow - this.lastCollectionTimeStampUtc).TotalSeconds * Environment.ProcessorCount); + var privilegedProcessorUtilization = (this.privilegedProcessorTimeSeconds - this.lastCollectedPrivilegedProcessorTime) / + ((DateTime.UtcNow - this.lastCollectedTimeStampUtc).TotalSeconds * Environment.ProcessorCount); - this.lastCollectionTimeStampUtc = DateTime.UtcNow; - this.lastCollectionUserProcessorTime = this.currentProcess.UserProcessorTime.TotalSeconds; - this.lastCollectionPrivilegedProcessorTime = this.currentProcess.PrivilegedProcessorTime.TotalSeconds; + this.lastCollectedTimeStampUtc = DateTime.UtcNow; + this.lastCollectedUserProcessorTime = this.currentProcess.UserProcessorTime.TotalSeconds; + this.lastCollectedPrivilegedProcessorTime = this.currentProcess.PrivilegedProcessorTime.TotalSeconds; return new[] { From 70f387ec9d289ac3dcbc07df6b8d8cf240ce7d60 Mon Sep 17 00:00:00 2001 From: Yun-Ting Date: Thu, 13 Oct 2022 16:58:56 -0700 Subject: [PATCH 10/13] cache denominator --- .../ProcessMetrics.cs | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/src/OpenTelemetry.Instrumentation.Process/ProcessMetrics.cs b/src/OpenTelemetry.Instrumentation.Process/ProcessMetrics.cs index 28114920d4..e5e5242775 100644 --- a/src/OpenTelemetry.Instrumentation.Process/ProcessMetrics.cs +++ b/src/OpenTelemetry.Instrumentation.Process/ProcessMetrics.cs @@ -146,11 +146,9 @@ private void Snapshot() private IEnumerable> GetCpuUtilization() { - var userProcessorUtilization = (this.userProcessorTimeSeconds - this.lastCollectedUserProcessorTime) / - ((DateTime.UtcNow - this.lastCollectedTimeStampUtc).TotalSeconds * Environment.ProcessorCount); - - var privilegedProcessorUtilization = (this.privilegedProcessorTimeSeconds - this.lastCollectedPrivilegedProcessorTime) / - ((DateTime.UtcNow - this.lastCollectedTimeStampUtc).TotalSeconds * Environment.ProcessorCount); + var temp = (DateTime.UtcNow - this.lastCollectedTimeStampUtc).TotalSeconds * Environment.ProcessorCount; + var userProcessorUtilization = (this.userProcessorTimeSeconds - this.lastCollectedUserProcessorTime) / temp; + var privilegedProcessorUtilization = (this.privilegedProcessorTimeSeconds - this.lastCollectedPrivilegedProcessorTime) / temp; this.lastCollectedTimeStampUtc = DateTime.UtcNow; this.lastCollectedUserProcessorTime = this.currentProcess.UserProcessorTime.TotalSeconds; From d25e0b7a199a5f4a3d92a7ed132117d0ea4e637b Mon Sep 17 00:00:00 2001 From: Yun-Ting Date: Fri, 14 Oct 2022 12:44:11 -0700 Subject: [PATCH 11/13] update init values --- src/OpenTelemetry.Instrumentation.Process/ProcessMetrics.cs | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/OpenTelemetry.Instrumentation.Process/ProcessMetrics.cs b/src/OpenTelemetry.Instrumentation.Process/ProcessMetrics.cs index e5e5242775..745cbb76fb 100644 --- a/src/OpenTelemetry.Instrumentation.Process/ProcessMetrics.cs +++ b/src/OpenTelemetry.Instrumentation.Process/ProcessMetrics.cs @@ -36,12 +36,16 @@ internal sealed class ProcessMetrics private IEnumerable> cpuUtilization; // vars for calculating CPU utilization - private DateTime lastCollectedTimeStampUtc = DateTime.UtcNow; + private DateTime lastCollectedTimeStampUtc; private double lastCollectedUserProcessorTime; private double lastCollectedPrivilegedProcessorTime; public ProcessMetrics(ProcessInstrumentationOptions options) { + this.lastCollectedTimeStampUtc = DateTime.UtcNow; + this.lastCollectedUserProcessorTime = this.currentProcess.UserProcessorTime.TotalSeconds; + this.lastCollectedUserProcessorTime = this.currentProcess.PrivilegedProcessorTime.TotalSeconds; + // TODO: change to ObservableUpDownCounter this.MeterInstance.CreateObservableGauge( "process.memory.usage", From 3166c0743083bbf3337544b8df7d73eb7c53f7a5 Mon Sep 17 00:00:00 2001 From: Yun-Ting Date: Fri, 14 Oct 2022 13:45:25 -0700 Subject: [PATCH 12/13] fix --- .../ProcessMetrics.cs | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/OpenTelemetry.Instrumentation.Process/ProcessMetrics.cs b/src/OpenTelemetry.Instrumentation.Process/ProcessMetrics.cs index 745cbb76fb..e0df71977f 100644 --- a/src/OpenTelemetry.Instrumentation.Process/ProcessMetrics.cs +++ b/src/OpenTelemetry.Instrumentation.Process/ProcessMetrics.cs @@ -36,15 +36,15 @@ internal sealed class ProcessMetrics private IEnumerable> cpuUtilization; // vars for calculating CPU utilization - private DateTime lastCollectedTimeStampUtc; + private DateTime lastCollectionTimeUtc; private double lastCollectedUserProcessorTime; private double lastCollectedPrivilegedProcessorTime; public ProcessMetrics(ProcessInstrumentationOptions options) { - this.lastCollectedTimeStampUtc = DateTime.UtcNow; + this.lastCollectionTimeUtc = DateTime.UtcNow; this.lastCollectedUserProcessorTime = this.currentProcess.UserProcessorTime.TotalSeconds; - this.lastCollectedUserProcessorTime = this.currentProcess.PrivilegedProcessorTime.TotalSeconds; + this.lastCollectedPrivilegedProcessorTime = this.currentProcess.PrivilegedProcessorTime.TotalSeconds; // TODO: change to ObservableUpDownCounter this.MeterInstance.CreateObservableGauge( @@ -150,11 +150,11 @@ private void Snapshot() private IEnumerable> GetCpuUtilization() { - var temp = (DateTime.UtcNow - this.lastCollectedTimeStampUtc).TotalSeconds * Environment.ProcessorCount; + var temp = (DateTime.UtcNow - this.lastCollectionTimeUtc).TotalSeconds * Environment.ProcessorCount; var userProcessorUtilization = (this.userProcessorTimeSeconds - this.lastCollectedUserProcessorTime) / temp; var privilegedProcessorUtilization = (this.privilegedProcessorTimeSeconds - this.lastCollectedPrivilegedProcessorTime) / temp; - this.lastCollectedTimeStampUtc = DateTime.UtcNow; + this.lastCollectionTimeUtc = DateTime.UtcNow; this.lastCollectedUserProcessorTime = this.currentProcess.UserProcessorTime.TotalSeconds; this.lastCollectedPrivilegedProcessorTime = this.currentProcess.PrivilegedProcessorTime.TotalSeconds; From 8ebb7e071ea54fdc83c98b55a53cdf5648bc1af5 Mon Sep 17 00:00:00 2001 From: Yun-Ting Date: Fri, 14 Oct 2022 13:47:55 -0700 Subject: [PATCH 13/13] name --- src/OpenTelemetry.Instrumentation.Process/ProcessMetrics.cs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/OpenTelemetry.Instrumentation.Process/ProcessMetrics.cs b/src/OpenTelemetry.Instrumentation.Process/ProcessMetrics.cs index e0df71977f..cd4e743149 100644 --- a/src/OpenTelemetry.Instrumentation.Process/ProcessMetrics.cs +++ b/src/OpenTelemetry.Instrumentation.Process/ProcessMetrics.cs @@ -150,9 +150,9 @@ private void Snapshot() private IEnumerable> GetCpuUtilization() { - var temp = (DateTime.UtcNow - this.lastCollectionTimeUtc).TotalSeconds * Environment.ProcessorCount; - var userProcessorUtilization = (this.userProcessorTimeSeconds - this.lastCollectedUserProcessorTime) / temp; - var privilegedProcessorUtilization = (this.privilegedProcessorTimeSeconds - this.lastCollectedPrivilegedProcessorTime) / temp; + var elapsedTimeForAllCpus = (DateTime.UtcNow - this.lastCollectionTimeUtc).TotalSeconds * Environment.ProcessorCount; + var userProcessorUtilization = (this.userProcessorTimeSeconds - this.lastCollectedUserProcessorTime) / elapsedTimeForAllCpus; + var privilegedProcessorUtilization = (this.privilegedProcessorTimeSeconds - this.lastCollectedPrivilegedProcessorTime) / elapsedTimeForAllCpus; this.lastCollectionTimeUtc = DateTime.UtcNow; this.lastCollectedUserProcessorTime = this.currentProcess.UserProcessorTime.TotalSeconds;