From b9c9f4b6e714d14a28d25a744679e139b0fb2452 Mon Sep 17 00:00:00 2001 From: xiang17 Date: Wed, 20 Jul 2022 17:57:14 -0700 Subject: [PATCH] [Instrumentation.Runtime] Add `gc.heap.fragmentation.size` back for .NET 7 and later (#524) --- .../CHANGELOG.md | 3 ++ .../RuntimeMetrics.cs | 45 ++++++++++--------- 2 files changed, 26 insertions(+), 22 deletions(-) diff --git a/src/OpenTelemetry.Instrumentation.Runtime/CHANGELOG.md b/src/OpenTelemetry.Instrumentation.Runtime/CHANGELOG.md index d3b2fec726..23c2377230 100644 --- a/src/OpenTelemetry.Instrumentation.Runtime/CHANGELOG.md +++ b/src/OpenTelemetry.Instrumentation.Runtime/CHANGELOG.md @@ -2,6 +2,9 @@ ## Unreleased +* Add gc.heap.fragmentation.size back for .NET 7 and later + ([#524](https://github.com/open-telemetry/opentelemetry-dotnet-contrib/pull/524)) + ## 1.0.0-rc.2 Released 2022-Jul-19 diff --git a/src/OpenTelemetry.Instrumentation.Runtime/RuntimeMetrics.cs b/src/OpenTelemetry.Instrumentation.Runtime/RuntimeMetrics.cs index 552e2b1b87..c373950e5f 100644 --- a/src/OpenTelemetry.Instrumentation.Runtime/RuntimeMetrics.cs +++ b/src/OpenTelemetry.Instrumentation.Runtime/RuntimeMetrics.cs @@ -121,32 +121,33 @@ static RuntimeMetrics() unit: "bytes", description: "The heap size (including fragmentation), as observed during the latest garbage collection. The value will be unavailable until at least one garbage collection has occurred."); } -#endif -#if NET7_0_OR_GREATER - // TODO: Not valid until .NET 7 where the bug is fixed. See context in https://github.com/open-telemetry/opentelemetry-dotnet-contrib/issues/496 - // TODO: change to ObservableUpDownCounter - MeterInstance.CreateObservableGauge( - $"{metricPrefix}gc.heap.fragmentation.size", - () => - { - if (!IsGcInfoAvailable) + // Not valid until .NET 7 where the bug in the API is fixed. See context in https://github.com/open-telemetry/opentelemetry-dotnet-contrib/issues/496 + if (Environment.Version.Major >= 7) + { + // TODO: change to ObservableUpDownCounter + MeterInstance.CreateObservableGauge( + $"{metricPrefix}gc.heap.fragmentation.size", + () => { - return Array.Empty>(); - } + if (!IsGcInfoAvailable) + { + return Array.Empty>(); + } - 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) - { - measurements[i] = new(generationInfo[i].FragmentationAfterBytes, new KeyValuePair("generation", GenNames[i])); - } + 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) + { + measurements[i] = new(generationInfo[i].FragmentationAfterBytes, new KeyValuePair("generation", GenNames[i])); + } - return measurements; - }, - unit: "bytes", - description: "The heap fragmentation, as observed during the latest garbage collection. The value will be unavailable until at least one garbage collection has occurred."); + return measurements; + }, + unit: "bytes", + description: "The heap fragmentation, as observed during the latest garbage collection. The value will be unavailable until at least one garbage collection has occurred."); + } #endif #if NET6_0_OR_GREATER