diff --git a/src/OpenTelemetry.Instrumentation.Runtime/RuntimeMetrics.cs b/src/OpenTelemetry.Instrumentation.Runtime/RuntimeMetrics.cs index b267c9c4a6..a67f264633 100644 --- a/src/OpenTelemetry.Instrumentation.Runtime/RuntimeMetrics.cs +++ b/src/OpenTelemetry.Instrumentation.Runtime/RuntimeMetrics.cs @@ -24,14 +24,16 @@ internal sealed class RuntimeMetrics #endif private const int NumberOfGenerations = 3; - private static readonly string[] GenNames = new string[] { "gen0", "gen1", "gen2", "loh", "poh" }; + private static readonly string[] GenNames = ["gen0", "gen1", "gen2", "loh", "poh"]; +#if NET private static bool isGcInfoAvailable; +#endif static RuntimeMetrics() { MeterInstance.CreateObservableCounter( "process.runtime.dotnet.gc.collections.count", - () => GetGarbageCollectionCounts(), + GetGarbageCollectionCounts, description: "Number of garbage collections that have occurred since process start."); MeterInstance.CreateObservableUpDownCounter( @@ -51,19 +53,14 @@ static RuntimeMetrics() "process.runtime.dotnet.gc.committed_memory.size", () => { - if (!IsGcInfoAvailable) - { - return Array.Empty>(); - } - - return new Measurement[] { new(GC.GetGCMemoryInfo().TotalCommittedBytes) }; + return !IsGcInfoAvailable ? Array.Empty>() : [new(GC.GetGCMemoryInfo().TotalCommittedBytes)]; }, unit: "bytes", description: "The amount of committed virtual memory for the managed GC heap, as observed during the latest garbage collection. Committed virtual memory may be larger than the heap size because it includes both memory for storing existing objects (the heap size) and some extra memory that is ready to handle newly allocated objects in the future. The value will be unavailable until at least one garbage collection has occurred."); // GC.GetGCMemoryInfo().GenerationInfo[i].SizeAfterBytes is better but it has a bug in .NET 6. See context in https://github.com/open-telemetry/opentelemetry-dotnet-contrib/issues/496 Func? getGenerationSize = null; - bool isCodeRunningOnBuggyRuntimeVersion = Environment.Version.Major == 6; + var isCodeRunningOnBuggyRuntimeVersion = Environment.Version.Major == 6; if (isCodeRunningOnBuggyRuntimeVersion) { var mi = typeof(GC).GetMethod("GetGenerationSize", BindingFlags.NonPublic | BindingFlags.Static); @@ -82,22 +79,17 @@ static RuntimeMetrics() { if (!IsGcInfoAvailable) { - return Array.Empty>(); + return []; } var generationInfo = GC.GetGCMemoryInfo().GenerationInfo; - Measurement[] measurements = new Measurement[generationInfo.Length]; - int maxSupportedLength = Math.Min(generationInfo.Length, GenNames.Length); - for (int i = 0; i < maxSupportedLength; ++i) + var measurements = new Measurement[generationInfo.Length]; + var maxSupportedLength = Math.Min(generationInfo.Length, GenNames.Length); + for (var i = 0; i < maxSupportedLength; ++i) { - if (isCodeRunningOnBuggyRuntimeVersion) - { - measurements[i] = new((long)getGenerationSize!(i), new KeyValuePair("generation", GenNames[i])); - } - else - { - measurements[i] = new(generationInfo[i].SizeAfterBytes, new KeyValuePair("generation", GenNames[i])); - } + measurements[i] = isCodeRunningOnBuggyRuntimeVersion + ? new((long)getGenerationSize!(i), new KeyValuePair("generation", GenNames[i])) + : new(generationInfo[i].SizeAfterBytes, new KeyValuePair("generation", GenNames[i])); } return measurements; @@ -115,13 +107,13 @@ static RuntimeMetrics() { if (!IsGcInfoAvailable) { - return Array.Empty>(); + return []; } var generationInfo = GC.GetGCMemoryInfo().GenerationInfo; - Measurement[] measurements = new Measurement[generationInfo.Length]; - int maxSupportedLength = Math.Min(generationInfo.Length, GenNames.Length); - for (int i = 0; i < maxSupportedLength; ++i) + var measurements = new Measurement[generationInfo.Length]; + var maxSupportedLength = Math.Min(generationInfo.Length, GenNames.Length); + for (var i = 0; i < maxSupportedLength; ++i) { measurements[i] = new(generationInfo[i].FragmentationAfterBytes, new KeyValuePair("generation", GenNames[i])); } @@ -201,15 +193,17 @@ static RuntimeMetrics() exceptionCounter.Add(1); }; } - +#pragma warning disable SA1313 /// /// Initializes a new instance of the class. /// - /// The options to define the metrics. - public RuntimeMetrics(RuntimeInstrumentationOptions options) + /// The options to define the metrics. + public RuntimeMetrics(RuntimeInstrumentationOptions _1) +#pragma warning restore SA1313 { } +#if NET private static bool IsGcInfoAvailable { get @@ -227,12 +221,13 @@ private static bool IsGcInfoAvailable return isGcInfoAvailable; } } +#endif private static IEnumerable> GetGarbageCollectionCounts() { long collectionsFromHigherGeneration = 0; - for (int gen = NumberOfGenerations - 1; gen >= 0; --gen) + for (var gen = NumberOfGenerations - 1; gen >= 0; --gen) { long collectionsFromThisGeneration = GC.CollectionCount(gen); diff --git a/test/OpenTelemetry.Instrumentation.Runtime.Tests/RuntimeInstrumentationOptionsTests.cs b/test/OpenTelemetry.Instrumentation.Runtime.Tests/RuntimeInstrumentationOptionsTests.cs index 39d8faff0d..4ee8aceb74 100644 --- a/test/OpenTelemetry.Instrumentation.Runtime.Tests/RuntimeInstrumentationOptionsTests.cs +++ b/test/OpenTelemetry.Instrumentation.Runtime.Tests/RuntimeInstrumentationOptionsTests.cs @@ -3,7 +3,7 @@ namespace OpenTelemetry.Instrumentation.Runtime.Tests; -public class RuntimeInstrumentationOptionsTests +internal class RuntimeInstrumentationOptionsTests { /* [Fact] diff --git a/test/OpenTelemetry.Instrumentation.Runtime.Tests/RuntimeMetricsTests.cs b/test/OpenTelemetry.Instrumentation.Runtime.Tests/RuntimeMetricsTests.cs index ace5166232..877c3a4d52 100644 --- a/test/OpenTelemetry.Instrumentation.Runtime.Tests/RuntimeMetricsTests.cs +++ b/test/OpenTelemetry.Instrumentation.Runtime.Tests/RuntimeMetricsTests.cs @@ -113,9 +113,9 @@ public async Task ThreadingRelatedMetricsTest() .Build(); // Bump the count for `thread_pool.completed_items.count` metric - int taskCount = 50; - List tasks = new List(); - for (int i = 0; i < taskCount; i++) + var taskCount = 50; + var tasks = new List(); + for (var i = 0; i < taskCount; i++) { tasks.Add(Task.Run(() => { })); } @@ -137,15 +137,19 @@ public async Task ThreadingRelatedMetricsTest() var queueLengthMetric = exportedItems.FirstOrDefault(i => i.Name == "process.runtime.dotnet.thread_pool.queue.length"); Assert.NotNull(queueLengthMetric); - List timers = new List(); + var timers = new List(); try { // Create 10 timers to bump timer.count metrics. - int timerCount = 10; - TimerCallback timerCallback = _ => { }; - for (int i = 0; i < timerCount; i++) + var timerCount = 10; +#pragma warning disable SA1313 + static void TimerCallback(object? _) { - Timer timer = new Timer(timerCallback, null, 1000, 250); + } +#pragma warning restore SA1313 + for (var i = 0; i < timerCount; i++) + { + var timer = new Timer(TimerCallback, null, 1000, 250); timers.Add(timer); } @@ -157,7 +161,7 @@ public async Task ThreadingRelatedMetricsTest() } finally { - for (int i = 0; i < timers.Count; i++) + for (var i = 0; i < timers.Count; i++) { timers[i].Dispose(); }