From 38c5708781902d4a179ead293ac0b34376c3ed29 Mon Sep 17 00:00:00 2001 From: Yun-Ting Lin Date: Wed, 7 Sep 2022 15:31:30 -0700 Subject: [PATCH] initial --- .../ProcessMetrics.cs | 66 +++++++++++++++---- 1 file changed, 53 insertions(+), 13 deletions(-) diff --git a/src/OpenTelemetry.Instrumentation.Process/ProcessMetrics.cs b/src/OpenTelemetry.Instrumentation.Process/ProcessMetrics.cs index 563ff11452..cf45983e85 100644 --- a/src/OpenTelemetry.Instrumentation.Process/ProcessMetrics.cs +++ b/src/OpenTelemetry.Instrumentation.Process/ProcessMetrics.cs @@ -14,6 +14,7 @@ // limitations under the License. // +using System; using System.Diagnostics.Metrics; using System.Reflection; using Diagnostics = System.Diagnostics; @@ -24,31 +25,27 @@ internal class ProcessMetrics { internal static readonly AssemblyName AssemblyName = typeof(ProcessMetrics).Assembly.GetName(); internal static readonly Meter MeterInstance = new(AssemblyName.Name, AssemblyName.Version.ToString()); - private static readonly Diagnostics.Process CurrentProcess = Diagnostics.Process.GetCurrentProcess(); static ProcessMetrics() { + // Refer to the spec for instrument details: + // https://github.com/open-telemetry/opentelemetry-specification/blob/main/specification/metrics/semantic_conventions/process-metrics.md#metric-instruments + + InstrumentsValues values = new InstrumentsValues(() => (-1, -1)); + // TODO: change to ObservableUpDownCounter MeterInstance.CreateObservableGauge( "process.memory.usage", - () => - { - CurrentProcess.Refresh(); - return CurrentProcess.WorkingSet64; - }, + () => values.GetMemoryUsage(), unit: "By", - description: "The amount of physical memory in use."); + description: "The amount of physical memory in use by the current process."); // TODO: change to ObservableUpDownCounter MeterInstance.CreateObservableGauge( "process.memory.virtual", - () => - { - CurrentProcess.Refresh(); - return CurrentProcess.VirtualMemorySize64; - }, + () => values.GetVirtualMemoryUsage(), unit: "By", - description: "The amount of committed virtual memory."); + description: "The amount of committed virtual memory by the current process."); } /// @@ -58,4 +55,47 @@ static ProcessMetrics() public ProcessMetrics(ProcessInstrumentationOptions options) { } + + private class InstrumentsValues + { + private readonly Diagnostics.Process currentProcess = Diagnostics.Process.GetCurrentProcess(); + + private double? memoryUsage; + private double? virtualMemoryUsage; + + public InstrumentsValues(Func<(double MemoryUsage, double VirtualMemoryUsage)> readValues) + { + this.memoryUsage = null; + this.virtualMemoryUsage = null; + + this.currentProcess.Refresh(); + this.UpdateValues = readValues; + } + + private Func<(double MemoryUsage, double VirtualMemoryUsage)> UpdateValues { get; set; } + + public double GetMemoryUsage() + { + if (!this.memoryUsage.HasValue) + { + (this.memoryUsage, this.virtualMemoryUsage) = this.UpdateValues(); + } + + var value = this.currentProcess.WorkingSet64; + this.memoryUsage = null; + return value; + } + + public double GetVirtualMemoryUsage() + { + if (!this.virtualMemoryUsage.HasValue) + { + (this.memoryUsage, this.virtualMemoryUsage) = this.UpdateValues(); + } + + var value = this.currentProcess.VirtualMemorySize64; + this.virtualMemoryUsage = null; + return value; + } + } }