diff --git a/src/OpenTelemetry.Instrumentation.Process/ProcessMetrics.cs b/src/OpenTelemetry.Instrumentation.Process/ProcessMetrics.cs index 930279d669..563ff11452 100644 --- a/src/OpenTelemetry.Instrumentation.Process/ProcessMetrics.cs +++ b/src/OpenTelemetry.Instrumentation.Process/ProcessMetrics.cs @@ -16,6 +16,7 @@ using System.Diagnostics.Metrics; using System.Reflection; +using Diagnostics = System.Diagnostics; namespace OpenTelemetry.Instrumentation.Process; @@ -23,9 +24,31 @@ 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() { + // TODO: change to ObservableUpDownCounter + MeterInstance.CreateObservableGauge( + "process.memory.usage", + () => + { + CurrentProcess.Refresh(); + return CurrentProcess.WorkingSet64; + }, + unit: "By", + description: "The amount of physical memory in use."); + + // TODO: change to ObservableUpDownCounter + MeterInstance.CreateObservableGauge( + "process.memory.virtual", + () => + { + CurrentProcess.Refresh(); + return CurrentProcess.VirtualMemorySize64; + }, + unit: "By", + description: "The amount of committed virtual memory."); } /// diff --git a/test/OpenTelemetry.Instrumentation.Process.Tests/ProcessMetricsTests.cs b/test/OpenTelemetry.Instrumentation.Process.Tests/ProcessMetricsTests.cs index 701679c659..e227506a81 100644 --- a/test/OpenTelemetry.Instrumentation.Process.Tests/ProcessMetricsTests.cs +++ b/test/OpenTelemetry.Instrumentation.Process.Tests/ProcessMetricsTests.cs @@ -14,8 +14,32 @@ // limitations under the License. // +using System.Collections.Generic; +using System.Linq; +using OpenTelemetry.Metrics; +using Xunit; + namespace OpenTelemetry.Instrumentation.Process.Tests; public class ProcessMetricsTests { + private const int MaxTimeToAllowForFlush = 10000; + + [Fact] + public void ProcessMetricsAreCaptured() + { + var exportedItems = new List(); + using var meterProvider = Sdk.CreateMeterProviderBuilder() + .AddProcessInstrumentation() + .AddInMemoryExporter(exportedItems) + .Build(); + + meterProvider.ForceFlush(MaxTimeToAllowForFlush); + + Assert.True(exportedItems.Count == 2); + 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); + } }