Skip to content
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,6 @@ public async Task TestCpuAndMemoryChecks_WithMetrics(
{
var fakeClock = new FakeTimeProvider();
var dataTracker = new Mock<IResourceMonitor>();
var meterName = Guid.NewGuid().ToString();
var logger = new FakeLogger<LinuxUtilizationProvider>();
using var meter = new Meter("Microsoft.Extensions.Diagnostics.ResourceMonitoring");
var meterFactoryMock = new Mock<IMeterFactory>();
Expand All @@ -176,23 +175,61 @@ public async Task TestCpuAndMemoryChecks_WithMetrics(
var resourceQuotaProvider = new LinuxResourceQuotaProvider(parser.Object, resourceMonitoringOptions);
var provider = new LinuxUtilizationProvider(resourceMonitoringOptions, parser.Object, meterFactoryMock.Object, resourceQuotaProvider, logger, fakeClock);

var checkContext = new HealthCheckContext();
var checkOptions = new ResourceUtilizationHealthCheckOptions
{
CpuThresholds = cpuThresholds,
MemoryThresholds = memoryThresholds,
UseObservableResourceMonitoringInstruments = true
};

// Use local MeterListener to collect measurements from this specific meter instance to avoid flakiness
double cpuUsedPercentage = 0;
double memoryUsedPercentage = 0;

using var listener = new MeterListener
{
InstrumentPublished = (instrument, listener) =>
{
if (ReferenceEquals(meter, instrument.Meter))
{
listener.EnableMeasurementEvents(instrument);
}
}
};

listener.SetMeasurementEventCallback<double>((instrument, measurement, _, _) =>
{
switch (instrument.Name)
{
case "process.cpu.utilization":
case "container.cpu.limit.utilization":
cpuUsedPercentage = measurement * 100;
break;
case "dotnet.process.memory.virtual.utilization":
case "container.memory.limit.utilization":
memoryUsedPercentage = measurement * 100;
break;
}
});

listener.Start();

// Act
fakeClock.Advance(TimeSpan.FromMilliseconds(1));
listener.RecordObservableInstruments();

var healthCheckResult = await ResourceUtilizationHealthCheck.EvaluateHealthStatusAsync(
cpuUsedPercentage,
memoryUsedPercentage,
checkOptions);

// Also create ResourceUtilizationHealthCheck and call CheckHealthAsync for code coverage
var options = Microsoft.Extensions.Options.Options.Create(checkOptions);
using var healthCheck = new ResourceUtilizationHealthCheck(
options,
dataTracker.Object,
Microsoft.Extensions.Options.Options.Create(new ResourceMonitoringOptions()));

// Act
fakeClock.Advance(TimeSpan.FromMilliseconds(1));
var healthCheckResult = await healthCheck.CheckHealthAsync(checkContext);
_ = await healthCheck.CheckHealthAsync(new HealthCheckContext());

// Assert
Assert.Equal(expected, healthCheckResult.Status);
Expand Down
Loading