From d451059340392aaf06741851106f41fdb24cd3aa Mon Sep 17 00:00:00 2001 From: Yun-Ting Date: Tue, 27 Sep 2022 17:25:10 -0700 Subject: [PATCH 1/8] initial commit --- .../ProcessMetrics.cs | 96 +++++++++++++++---- 1 file changed, 79 insertions(+), 17 deletions(-) diff --git a/src/OpenTelemetry.Instrumentation.Process/ProcessMetrics.cs b/src/OpenTelemetry.Instrumentation.Process/ProcessMetrics.cs index 563ff11452..42434e43c3 100644 --- a/src/OpenTelemetry.Instrumentation.Process/ProcessMetrics.cs +++ b/src/OpenTelemetry.Instrumentation.Process/ProcessMetrics.cs @@ -14,8 +14,11 @@ // limitations under the License. // +using System; +using System.Collections.Generic; using System.Diagnostics.Metrics; using System.Reflection; +using System.Threading; using Diagnostics = System.Diagnostics; namespace OpenTelemetry.Instrumentation.Process; @@ -24,38 +27,97 @@ 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() + public ProcessMetrics(ProcessInstrumentationOptions options) { + ThreadSafeInstrumentValues threadSafeInstrumentValues = new(); + // TODO: change to ObservableUpDownCounter MeterInstance.CreateObservableGauge( "process.memory.usage", - () => - { - CurrentProcess.Refresh(); - return CurrentProcess.WorkingSet64; - }, + () => threadSafeInstrumentValues.GetMemoryUsage(), unit: "By", description: "The amount of physical memory in use."); // TODO: change to ObservableUpDownCounter MeterInstance.CreateObservableGauge( "process.memory.virtual", - () => - { - CurrentProcess.Refresh(); - return CurrentProcess.VirtualMemorySize64; - }, + () => threadSafeInstrumentValues.GetVirtualMemoryUsage(), unit: "By", description: "The amount of committed virtual memory."); } - /// - /// Initializes a new instance of the class. - /// - /// The options to define the metrics. - public ProcessMetrics(ProcessInstrumentationOptions options) + private class ThreadSafeInstrumentValues + { + private readonly ThreadLocal> threadIdToInstrumentValues = new(() => + { + return new Dictionary(); + }); + + internal double GetMemoryUsage() + { + if (!this.threadIdToInstrumentValues.Value.ContainsKey(Environment.CurrentManagedThreadId)) + { + this.threadIdToInstrumentValues.Value.Add(Environment.CurrentManagedThreadId, new InstrumentsValues()); + } + + this.threadIdToInstrumentValues.Value.TryGetValue(Environment.CurrentManagedThreadId, out var instrumentValues); + return instrumentValues.GetMemoryUsage(); + } + + internal double GetVirtualMemoryUsage() + { + if (!this.threadIdToInstrumentValues.Value.ContainsKey(Environment.CurrentManagedThreadId)) + { + this.threadIdToInstrumentValues.Value.Add(Environment.CurrentManagedThreadId, new InstrumentsValues()); + } + + this.threadIdToInstrumentValues.Value.TryGetValue(Environment.CurrentManagedThreadId, out var instrumentsValues); + return instrumentsValues.GetVirtualMemoryUsage(); + } + } + + private class InstrumentsValues { + private readonly Diagnostics.Process currentProcess = Diagnostics.Process.GetCurrentProcess(); + private double? memoryUsage; + private double? virtualMemoryUsage; + + internal InstrumentsValues() + { + this.memoryUsage = null; + this.virtualMemoryUsage = null; + } + + internal double GetMemoryUsage() + { + if (!this.memoryUsage.HasValue) + { + this.Snapshot(); + } + + var value = (double)this.memoryUsage; + this.memoryUsage = null; + return value; + } + + internal double GetVirtualMemoryUsage() + { + if (!this.virtualMemoryUsage.HasValue) + { + this.Snapshot(); + } + + var value = (double)this.virtualMemoryUsage; + this.virtualMemoryUsage = null; + return value; + } + + private void Snapshot() + { + this.currentProcess.Refresh(); + this.memoryUsage = this.currentProcess.WorkingSet64; + this.virtualMemoryUsage = this.currentProcess.PagedMemorySize64; + } } } From 5d688f1ea63569c7b6e0e8d75588cdc11c591526 Mon Sep 17 00:00:00 2001 From: Yun-Ting Lin Date: Wed, 28 Sep 2022 15:17:44 -0700 Subject: [PATCH 2/8] comments --- .../ProcessMetrics.cs | 52 ++++--------------- 1 file changed, 10 insertions(+), 42 deletions(-) diff --git a/src/OpenTelemetry.Instrumentation.Process/ProcessMetrics.cs b/src/OpenTelemetry.Instrumentation.Process/ProcessMetrics.cs index 42434e43c3..820e25f3bc 100644 --- a/src/OpenTelemetry.Instrumentation.Process/ProcessMetrics.cs +++ b/src/OpenTelemetry.Instrumentation.Process/ProcessMetrics.cs @@ -14,8 +14,6 @@ // limitations under the License. // -using System; -using System.Collections.Generic; using System.Diagnostics.Metrics; using System.Reflection; using System.Threading; @@ -23,63 +21,33 @@ namespace OpenTelemetry.Instrumentation.Process; -internal class ProcessMetrics +internal sealed 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 ThreadLocal CurrentThreadInstrumentsValues = new(() => new InstrumentsValues()); + public ProcessMetrics(ProcessInstrumentationOptions options) { - ThreadSafeInstrumentValues threadSafeInstrumentValues = new(); - // TODO: change to ObservableUpDownCounter MeterInstance.CreateObservableGauge( "process.memory.usage", - () => threadSafeInstrumentValues.GetMemoryUsage(), + () => CurrentThreadInstrumentsValues.Value.GetMemoryUsage(), unit: "By", description: "The amount of physical memory in use."); // TODO: change to ObservableUpDownCounter MeterInstance.CreateObservableGauge( "process.memory.virtual", - () => threadSafeInstrumentValues.GetVirtualMemoryUsage(), + () => CurrentThreadInstrumentsValues.Value.GetVirtualMemoryUsage(), unit: "By", description: "The amount of committed virtual memory."); } - private class ThreadSafeInstrumentValues - { - private readonly ThreadLocal> threadIdToInstrumentValues = new(() => - { - return new Dictionary(); - }); - - internal double GetMemoryUsage() - { - if (!this.threadIdToInstrumentValues.Value.ContainsKey(Environment.CurrentManagedThreadId)) - { - this.threadIdToInstrumentValues.Value.Add(Environment.CurrentManagedThreadId, new InstrumentsValues()); - } - - this.threadIdToInstrumentValues.Value.TryGetValue(Environment.CurrentManagedThreadId, out var instrumentValues); - return instrumentValues.GetMemoryUsage(); - } - - internal double GetVirtualMemoryUsage() - { - if (!this.threadIdToInstrumentValues.Value.ContainsKey(Environment.CurrentManagedThreadId)) - { - this.threadIdToInstrumentValues.Value.Add(Environment.CurrentManagedThreadId, new InstrumentsValues()); - } - - this.threadIdToInstrumentValues.Value.TryGetValue(Environment.CurrentManagedThreadId, out var instrumentsValues); - return instrumentsValues.GetVirtualMemoryUsage(); - } - } - - private class InstrumentsValues + private sealed class InstrumentsValues { - private readonly Diagnostics.Process currentProcess = Diagnostics.Process.GetCurrentProcess(); + private static readonly Diagnostics.Process CurrentProcess = Diagnostics.Process.GetCurrentProcess(); private double? memoryUsage; private double? virtualMemoryUsage; @@ -115,9 +83,9 @@ internal double GetVirtualMemoryUsage() private void Snapshot() { - this.currentProcess.Refresh(); - this.memoryUsage = this.currentProcess.WorkingSet64; - this.virtualMemoryUsage = this.currentProcess.PagedMemorySize64; + CurrentProcess.Refresh(); + this.memoryUsage = CurrentProcess.WorkingSet64; + this.virtualMemoryUsage = CurrentProcess.PagedMemorySize64; } } } From 13434f599fcfc618d00ad5bb4a3872f5823c5c95 Mon Sep 17 00:00:00 2001 From: Yun-Ting Date: Tue, 4 Oct 2022 17:29:02 -0700 Subject: [PATCH 3/8] comments --- .../MeterProviderBuilderExtensions.cs | 2 +- .../ProcessMetrics.cs | 37 ++++++++----------- 2 files changed, 17 insertions(+), 22 deletions(-) diff --git a/src/OpenTelemetry.Instrumentation.Process/MeterProviderBuilderExtensions.cs b/src/OpenTelemetry.Instrumentation.Process/MeterProviderBuilderExtensions.cs index 516945ebd1..223808b4a4 100644 --- a/src/OpenTelemetry.Instrumentation.Process/MeterProviderBuilderExtensions.cs +++ b/src/OpenTelemetry.Instrumentation.Process/MeterProviderBuilderExtensions.cs @@ -41,7 +41,7 @@ public static MeterProviderBuilder AddProcessInstrumentation( configure?.Invoke(options); var instrumentation = new ProcessMetrics(options); - builder.AddMeter(ProcessMetrics.MeterInstance.Name); + builder.AddMeter(instrumentation.MeterInstance.Name); return builder.AddInstrumentation(() => instrumentation); } } diff --git a/src/OpenTelemetry.Instrumentation.Process/ProcessMetrics.cs b/src/OpenTelemetry.Instrumentation.Process/ProcessMetrics.cs index 820e25f3bc..766dd3c8bc 100644 --- a/src/OpenTelemetry.Instrumentation.Process/ProcessMetrics.cs +++ b/src/OpenTelemetry.Instrumentation.Process/ProcessMetrics.cs @@ -16,7 +16,6 @@ using System.Diagnostics.Metrics; using System.Reflection; -using System.Threading; using Diagnostics = System.Diagnostics; namespace OpenTelemetry.Instrumentation.Process; @@ -24,39 +23,35 @@ namespace OpenTelemetry.Instrumentation.Process; internal sealed class ProcessMetrics { internal static readonly AssemblyName AssemblyName = typeof(ProcessMetrics).Assembly.GetName(); - internal static readonly Meter MeterInstance = new(AssemblyName.Name, AssemblyName.Version.ToString()); + internal readonly Meter MeterInstance = new(AssemblyName.Name, AssemblyName.Version.ToString()); - private static readonly ThreadLocal CurrentThreadInstrumentsValues = new(() => new InstrumentsValues()); + private readonly InstrumentsValues instrumentsValues; public ProcessMetrics(ProcessInstrumentationOptions options) { + this.instrumentsValues = new InstrumentsValues(); + // TODO: change to ObservableUpDownCounter - MeterInstance.CreateObservableGauge( + this.MeterInstance.CreateObservableGauge( "process.memory.usage", - () => CurrentThreadInstrumentsValues.Value.GetMemoryUsage(), + () => this.instrumentsValues.GetMemoryUsage(), unit: "By", - description: "The amount of physical memory in use."); + description: "The amount of physical memory allocated for this process."); // TODO: change to ObservableUpDownCounter - MeterInstance.CreateObservableGauge( + this.MeterInstance.CreateObservableGauge( "process.memory.virtual", - () => CurrentThreadInstrumentsValues.Value.GetVirtualMemoryUsage(), + () => this.instrumentsValues.GetVirtualMemoryUsage(), unit: "By", - description: "The amount of committed virtual memory."); + description: "The amount of virtual memory allocated for this process that cannot be shared with other processes."); } private sealed class InstrumentsValues { - private static readonly Diagnostics.Process CurrentProcess = Diagnostics.Process.GetCurrentProcess(); + private readonly Diagnostics.Process currentProcess = Diagnostics.Process.GetCurrentProcess(); private double? memoryUsage; private double? virtualMemoryUsage; - internal InstrumentsValues() - { - this.memoryUsage = null; - this.virtualMemoryUsage = null; - } - internal double GetMemoryUsage() { if (!this.memoryUsage.HasValue) @@ -64,7 +59,7 @@ internal double GetMemoryUsage() this.Snapshot(); } - var value = (double)this.memoryUsage; + var value = this.memoryUsage.Value; this.memoryUsage = null; return value; } @@ -76,16 +71,16 @@ internal double GetVirtualMemoryUsage() this.Snapshot(); } - var value = (double)this.virtualMemoryUsage; + var value = this.virtualMemoryUsage.Value; this.virtualMemoryUsage = null; return value; } private void Snapshot() { - CurrentProcess.Refresh(); - this.memoryUsage = CurrentProcess.WorkingSet64; - this.virtualMemoryUsage = CurrentProcess.PagedMemorySize64; + this.currentProcess.Refresh(); + this.memoryUsage = this.currentProcess.WorkingSet64; + this.virtualMemoryUsage = this.currentProcess.PrivateMemorySize64; } } } From 7646e7e19538a324c2fb9815ab938caf9efc26e1 Mon Sep 17 00:00:00 2001 From: Yun-Ting Date: Fri, 7 Oct 2022 13:12:42 -0700 Subject: [PATCH 4/8] minor refactoring --- .../ProcessMetrics.cs | 69 ++++++++----------- 1 file changed, 29 insertions(+), 40 deletions(-) diff --git a/src/OpenTelemetry.Instrumentation.Process/ProcessMetrics.cs b/src/OpenTelemetry.Instrumentation.Process/ProcessMetrics.cs index 766dd3c8bc..f4e1c74283 100644 --- a/src/OpenTelemetry.Instrumentation.Process/ProcessMetrics.cs +++ b/src/OpenTelemetry.Instrumentation.Process/ProcessMetrics.cs @@ -25,62 +25,51 @@ internal sealed class ProcessMetrics internal static readonly AssemblyName AssemblyName = typeof(ProcessMetrics).Assembly.GetName(); internal readonly Meter MeterInstance = new(AssemblyName.Name, AssemblyName.Version.ToString()); - private readonly InstrumentsValues instrumentsValues; + private readonly Diagnostics.Process currentProcess = Diagnostics.Process.GetCurrentProcess(); + private double? memoryUsage; + private double? virtualMemoryUsage; public ProcessMetrics(ProcessInstrumentationOptions options) { - this.instrumentsValues = new InstrumentsValues(); - // TODO: change to ObservableUpDownCounter this.MeterInstance.CreateObservableGauge( "process.memory.usage", - () => this.instrumentsValues.GetMemoryUsage(), + () => + { + if (!this.memoryUsage.HasValue) + { + this.Snapshot(); + } + + var value = this.memoryUsage.Value; + this.memoryUsage = null; + return value; + }, unit: "By", description: "The amount of physical memory allocated for this process."); // TODO: change to ObservableUpDownCounter this.MeterInstance.CreateObservableGauge( "process.memory.virtual", - () => this.instrumentsValues.GetVirtualMemoryUsage(), + () => + { + if (!this.virtualMemoryUsage.HasValue) + { + this.Snapshot(); + } + + var value = this.virtualMemoryUsage.Value; + this.virtualMemoryUsage = null; + return value; + }, unit: "By", description: "The amount of virtual memory allocated for this process that cannot be shared with other processes."); } - private sealed class InstrumentsValues + private void Snapshot() { - private readonly Diagnostics.Process currentProcess = Diagnostics.Process.GetCurrentProcess(); - private double? memoryUsage; - private double? virtualMemoryUsage; - - internal double GetMemoryUsage() - { - if (!this.memoryUsage.HasValue) - { - this.Snapshot(); - } - - var value = this.memoryUsage.Value; - this.memoryUsage = null; - return value; - } - - internal double GetVirtualMemoryUsage() - { - if (!this.virtualMemoryUsage.HasValue) - { - this.Snapshot(); - } - - var value = this.virtualMemoryUsage.Value; - this.virtualMemoryUsage = null; - return value; - } - - private void Snapshot() - { - this.currentProcess.Refresh(); - this.memoryUsage = this.currentProcess.WorkingSet64; - this.virtualMemoryUsage = this.currentProcess.PrivateMemorySize64; - } + this.currentProcess.Refresh(); + this.memoryUsage = this.currentProcess.WorkingSet64; + this.virtualMemoryUsage = this.currentProcess.PrivateMemorySize64; } } From 6bfac03ec17a111d21342d6954feab1f696b9b59 Mon Sep 17 00:00:00 2001 From: Yun-Ting Date: Fri, 7 Oct 2022 15:16:52 -0700 Subject: [PATCH 5/8] tests --- .../ProcessMetricsTests.cs | 87 +++++++++++++++++++ 1 file changed, 87 insertions(+) diff --git a/test/OpenTelemetry.Instrumentation.Process.Tests/ProcessMetricsTests.cs b/test/OpenTelemetry.Instrumentation.Process.Tests/ProcessMetricsTests.cs index e227506a81..dbe95da9a6 100644 --- a/test/OpenTelemetry.Instrumentation.Process.Tests/ProcessMetricsTests.cs +++ b/test/OpenTelemetry.Instrumentation.Process.Tests/ProcessMetricsTests.cs @@ -15,6 +15,7 @@ // using System.Collections.Generic; +using System.Diagnostics.Metrics; using System.Linq; using OpenTelemetry.Metrics; using Xunit; @@ -42,4 +43,90 @@ public void ProcessMetricsAreCaptured() var virtualMemoryMetric = exportedItems.FirstOrDefault(i => i.Name == "process.memory.virtual"); Assert.NotNull(virtualMemoryMetric); } + + [Fact] + public void CheckValidGaugeValueWhen2MeterProviderInstancesHaveTheSameMeterName() + { + var exportedItemsA = new List(); + var exportedItemsB = new List(); + + using var meterProviderA = Sdk.CreateMeterProviderBuilder() + .AddProcessInstrumentation() + .AddInMemoryExporter(exportedItemsA) + .Build(); + + using var meterProviderB = Sdk.CreateMeterProviderBuilder() + .AddProcessInstrumentation() + .AddInMemoryExporter(exportedItemsB) + .Build(); + + meterProviderA.ForceFlush(MaxTimeToAllowForFlush); + meterProviderB.ForceFlush(MaxTimeToAllowForFlush); + + var metricA = exportedItemsA.FirstOrDefault(i => i.Name == "process.memory.usage"); + var metricB = exportedItemsB.FirstOrDefault(i => i.Name == "process.memory.usage"); + + Assert.True(GetValue(metricA) > 0); + Assert.True(GetValue(metricB) > 0); + } + + [Fact] + public void UnifiedMetricStreamIdentityWhen2MeterProviderInstancesHaveTheSameMeterName() + { + var exportedItemsA = new List(); + var exportedItemsB = new List(); + + Meter m1 = new("myMeter"); + Meter m2 = new("myMeter"); + + m1.CreateObservableCounter( + "myGaugeName", + () => { return 1D; }, + unit: "1", + description: "test"); + + m1.CreateObservableCounter( + "myGaugeName", + () => { return 2D; }, + unit: "1", + description: "test"); + + using var meterProviderA = Sdk.CreateMeterProviderBuilder() + .AddMeter("myMeter") + .AddInMemoryExporter(exportedItemsA) + .Build(); + + using var meterProviderB = Sdk.CreateMeterProviderBuilder() + .AddMeter("myMeter") + .AddInMemoryExporter(exportedItemsB) + .Build(); + + meterProviderA.ForceFlush(); + meterProviderB.ForceFlush(); + + var metricA = exportedItemsA.FirstOrDefault(i => i.Name == "myGaugeName"); + var metricB = exportedItemsB.FirstOrDefault(i => i.Name == "myGaugeName"); + + Assert.Equal(GetValue(metricA), GetValue(metricB)); + } + + private static double GetValue(Metric metric) + { + Assert.NotNull(metric); + double sum = 0; + + foreach (ref readonly var metricPoint in metric.GetMetricPoints()) + { + if (metric.MetricType.IsGauge()) + { + sum += metricPoint.GetGaugeLastValueDouble(); + } + else if (metric.MetricType.IsDouble()) + { + sum += metricPoint.GetSumDouble(); + } + } + + return sum; + } } From c27b0d999170dcdda37aa7c3a33a365db9a595cf Mon Sep 17 00:00:00 2001 From: Yun-Ting Date: Mon, 10 Oct 2022 12:10:18 -0700 Subject: [PATCH 6/8] fix --- .../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 dbe95da9a6..5936e950c9 100644 --- a/test/OpenTelemetry.Instrumentation.Process.Tests/ProcessMetricsTests.cs +++ b/test/OpenTelemetry.Instrumentation.Process.Tests/ProcessMetricsTests.cs @@ -85,7 +85,7 @@ public void UnifiedMetricStreamIdentityWhen2MeterProviderInstancesHaveTheSameMet unit: "1", description: "test"); - m1.CreateObservableCounter( + m2.CreateObservableCounter( "myGaugeName", () => { return 2D; }, unit: "1", From ec9a4304a5c70c23298fabd671ba47cf081bb170 Mon Sep 17 00:00:00 2001 From: Yun-Ting Date: Mon, 10 Oct 2022 12:13:19 -0700 Subject: [PATCH 7/8] name --- .../ProcessMetricsTests.cs | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/test/OpenTelemetry.Instrumentation.Process.Tests/ProcessMetricsTests.cs b/test/OpenTelemetry.Instrumentation.Process.Tests/ProcessMetricsTests.cs index 5936e950c9..cc896a095c 100644 --- a/test/OpenTelemetry.Instrumentation.Process.Tests/ProcessMetricsTests.cs +++ b/test/OpenTelemetry.Instrumentation.Process.Tests/ProcessMetricsTests.cs @@ -80,13 +80,13 @@ public void UnifiedMetricStreamIdentityWhen2MeterProviderInstancesHaveTheSameMet Meter m2 = new("myMeter"); m1.CreateObservableCounter( - "myGaugeName", + "myCounterName", () => { return 1D; }, unit: "1", description: "test"); m2.CreateObservableCounter( - "myGaugeName", + "myCounterName", () => { return 2D; }, unit: "1", description: "test"); @@ -104,8 +104,8 @@ public void UnifiedMetricStreamIdentityWhen2MeterProviderInstancesHaveTheSameMet meterProviderA.ForceFlush(); meterProviderB.ForceFlush(); - var metricA = exportedItemsA.FirstOrDefault(i => i.Name == "myGaugeName"); - var metricB = exportedItemsB.FirstOrDefault(i => i.Name == "myGaugeName"); + var metricA = exportedItemsA.FirstOrDefault(i => i.Name == "myCounterName"); + var metricB = exportedItemsB.FirstOrDefault(i => i.Name == "myCounterName"); Assert.Equal(GetValue(metricA), GetValue(metricB)); } From ea77d50d8ea17b0bf225546851881d87f4e7e448 Mon Sep 17 00:00:00 2001 From: Yun-Ting Date: Mon, 10 Oct 2022 13:29:01 -0700 Subject: [PATCH 8/8] update --- .../ProcessMetricsTests.cs | 40 ------------------- 1 file changed, 40 deletions(-) diff --git a/test/OpenTelemetry.Instrumentation.Process.Tests/ProcessMetricsTests.cs b/test/OpenTelemetry.Instrumentation.Process.Tests/ProcessMetricsTests.cs index cc896a095c..7adc9fa789 100644 --- a/test/OpenTelemetry.Instrumentation.Process.Tests/ProcessMetricsTests.cs +++ b/test/OpenTelemetry.Instrumentation.Process.Tests/ProcessMetricsTests.cs @@ -70,46 +70,6 @@ public void CheckValidGaugeValueWhen2MeterProviderInstancesHaveTheSameMeterName( Assert.True(GetValue(metricB) > 0); } - [Fact] - public void UnifiedMetricStreamIdentityWhen2MeterProviderInstancesHaveTheSameMeterName() - { - var exportedItemsA = new List(); - var exportedItemsB = new List(); - - Meter m1 = new("myMeter"); - Meter m2 = new("myMeter"); - - m1.CreateObservableCounter( - "myCounterName", - () => { return 1D; }, - unit: "1", - description: "test"); - - m2.CreateObservableCounter( - "myCounterName", - () => { return 2D; }, - unit: "1", - description: "test"); - - using var meterProviderA = Sdk.CreateMeterProviderBuilder() - .AddMeter("myMeter") - .AddInMemoryExporter(exportedItemsA) - .Build(); - - using var meterProviderB = Sdk.CreateMeterProviderBuilder() - .AddMeter("myMeter") - .AddInMemoryExporter(exportedItemsB) - .Build(); - - meterProviderA.ForceFlush(); - meterProviderB.ForceFlush(); - - var metricA = exportedItemsA.FirstOrDefault(i => i.Name == "myCounterName"); - var metricB = exportedItemsB.FirstOrDefault(i => i.Name == "myCounterName"); - - Assert.Equal(GetValue(metricA), GetValue(metricB)); - } - private static double GetValue(Metric metric) { Assert.NotNull(metric);