Closed
Description
Description
The Microsoft.Extensions.Diagnostics.HealthChecks.ResourceUtilization
throw unhandled exception. System.IO.DirectoryNotFoundException: Could not find a part of the path '/sys/fs/cgroup/memory/memory.limit_in_bytes'
.
When I enter the docker container, I cannot find the /sys/fs/cgroup/memory
directory:
Reproduction Steps
Create a simple console app:
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net8.0</TargetFramework>
<RootNamespace>resource_monitoring</RootNamespace>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.Extensions.Logging.Console" Version="8.0.0" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="8.0.0" />
<PackageReference Include="Microsoft.Extensions.Diagnostics.ResourceMonitoring" Version="8.0.0" />
<PackageReference Include="Spectre.Console" Version="0.48.0" />
</ItemGroup>
</Project>
Update the Program.cs with the following:
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Diagnostics.ResourceMonitoring;
using Microsoft.Extensions.Logging;
using Spectre.Console;
var services = new ServiceCollection()
.AddLogging(static builder => builder.AddConsole())
.AddResourceMonitoring();
var provider = services.BuildServiceProvider();
var monitor = provider.GetRequiredService<IResourceMonitor>();
// </setup>
using var cancellationTokenSource = new CancellationTokenSource();
var token = cancellationTokenSource.Token;
// <monitor>
await StartMonitoringAsync(monitor, token);
async Task StartMonitoringAsync(IResourceMonitor monitor, CancellationToken cancellationToken)
{
var table = new Table()
.Centered()
.Title("Resource Monitoring", new Style(foreground: Color.Purple, decoration: Decoration.Bold))
.Caption("Updates every three seconds. *GTD: Guaranteed ", new Style(decoration: Decoration.Dim))
.RoundedBorder()
.BorderColor(Color.Cyan1)
.AddColumns(
[
new TableColumn("Time").Centered(),
new TableColumn("CPU %").Centered(),
new TableColumn("Memory %").Centered(),
new TableColumn("Memory (bytes)").Centered(),
new TableColumn("GTD / Max Memory (bytes)").Centered(),
new TableColumn("GTD / Max CPU (units)").Centered(),
]);
await AnsiConsole.Live(table)
.StartAsync(async ctx =>
{
var window = TimeSpan.FromSeconds(3);
while (cancellationToken.IsCancellationRequested is false)
{
var utilization = monitor.GetUtilization(window);
var resources = utilization.SystemResources;
Console.WriteLine($"{utilization.CpuUsedPercentage},{utilization.MemoryUsedPercentage}");
table.AddRow(
[
$"{DateTime.Now:T}",
$"{utilization.CpuUsedPercentage:p}",
$"{utilization.MemoryUsedPercentage:p}",
$"{utilization.MemoryUsedInBytes:#,#}",
$"{resources.GuaranteedMemoryInBytes:#,#} / {resources.MaximumMemoryInBytes:#,#}",
$"{resources.GuaranteedCpuUnits} / {resources.MaximumCpuUnits}",
]);
ctx.Refresh();
await Task.Delay(window);
}
});
Console.CancelKeyPress += (_, e) =>
{
e.Cancel = true;
cancellationTokenSource.Cancel();
};
}
This results in the following:
Expected behavior
Both CPU and memory resource utilization
Actual behavior
throw unhandled exception.
Regression?
No response
Known Workarounds
No response
Configuration
No response
Other information
No response