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 a metric for total paused duration in GC for .NET 7 and greater versions #1239

Merged
merged 5 commits into from
Jun 16, 2023
Merged
Show file tree
Hide file tree
Changes from 3 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 a metric for total paused duration in GC for .NET 7 and greater versions
xiang17 marked this conversation as resolved.
Show resolved Hide resolved
([#1239](https://github.com/open-telemetry/opentelemetry-dotnet-contrib/pull/1239))

## 1.5.0

Released 2023-Jun-06
Expand Down
16 changes: 16 additions & 0 deletions src/OpenTelemetry.Instrumentation.Runtime/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,22 @@ The API used to retrieve the value is:
* [GCGenerationInfo.FragmentationAfterBytes Property](https://docs.microsoft.com/dotnet/api/system.gcgenerationinfo.fragmentationafterbytes)
Gets the fragmentation in bytes on exit from the reported collection.

#### process.runtime.dotnet.**gc.duration**
xiang17 marked this conversation as resolved.
Show resolved Hide resolved

The total amount of time paused in GC since the process start.

> **Note**
> This metric is only available when targeting .NET 7 or later.

| Units | Instrument Type | Value Type | Attribute Key(s) | Attribute Values |
|-------|-------------------|------------|------------------|------------------|
| `ns` | ObservableCounter | `Int64` | No Attributes | N/A |

The API used to retrieve the value is:

* [GC.GetTotalPauseDuration](https://learn.microsoft.com/dotnet/api/system.gc.gettotalpauseduration)
Gets the total amount of time paused in GC since the beginning of the process.

### JIT Compiler related metrics

These metrics are only available when targeting .NET6 or later.
Expand Down
14 changes: 13 additions & 1 deletion src/OpenTelemetry.Instrumentation.Runtime/RuntimeMetrics.cs
Original file line number Diff line number Diff line change
Expand Up @@ -120,9 +120,9 @@ static RuntimeMetrics()
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.");
}

// 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)
{
// 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
MeterInstance.CreateObservableUpDownCounter(
"process.runtime.dotnet.gc.heap.fragmentation.size",
() =>
Expand All @@ -144,6 +144,18 @@ static RuntimeMetrics()
},
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.");

// GC.GetTotalPauseDuration() is not available until .NET 7. See context in https://github.com/open-telemetry/opentelemetry-dotnet-contrib/issues/1163
var mi = typeof(GC).GetMethod("GetTotalPauseDuration", BindingFlags.Public | BindingFlags.Static);
var getTotalPauseDuration = mi?.CreateDelegate<Func<TimeSpan>>();
if (getTotalPauseDuration != null)
{
MeterInstance.CreateObservableCounter(
"process.runtime.dotnet.gc.duration",
() => getTotalPauseDuration().Ticks * NanosecondsPerTick,
unit: "ns",
description: "The total amount of time paused in GC since the process start.");
}
}

MeterInstance.CreateObservableCounter(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,9 @@ public void GcMetricsTest()
{
var gcHeapFragmentationSizeMetric = exportedItems.FirstOrDefault(i => i.Name == "process.runtime.dotnet.gc.heap.fragmentation.size");
Assert.NotNull(gcHeapFragmentationSizeMetric);

var gcDurationMetric = exportedItems.FirstOrDefault(i => i.Name == "process.runtime.dotnet.gc.duration");
Assert.NotNull(gcDurationMetric);
}
#endif
}
Expand Down