Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Instrumentation.Runtime] Add gc.heap.fragmentation.size back for .NET 7 and later #524

Merged
merged 2 commits into from
Jul 21, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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