Skip to content

Commit

Permalink
[Instrumentation.Runtime] Add gc.heap.fragmentation.size back for .…
Browse files Browse the repository at this point in the history
…NET 7 and later (#524)
  • Loading branch information
xiang17 authored Jul 21, 2022
1 parent b4f4a7e commit b9c9f4b
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 22 deletions.
3 changes: 3 additions & 0 deletions src/OpenTelemetry.Instrumentation.Runtime/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
45 changes: 23 additions & 22 deletions src/OpenTelemetry.Instrumentation.Runtime/RuntimeMetrics.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Measurement<long>>();
}
if (!IsGcInfoAvailable)
{
return Array.Empty<Measurement<long>>();
}

var generationInfo = GC.GetGCMemoryInfo().GenerationInfo;
Measurement<long>[] measurements = new Measurement<long>[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<string, object>("generation", GenNames[i]));
}
var generationInfo = GC.GetGCMemoryInfo().GenerationInfo;
Measurement<long>[] measurements = new Measurement<long>[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<string, object>("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
Expand Down

0 comments on commit b9c9f4b

Please sign in to comment.