Skip to content

Commit

Permalink
[release/8.9] Revert correct Meter name (#5406)
Browse files Browse the repository at this point in the history
* Revert correct Meter name

* Unit tests for ResourceMonitoring meter name

* Fixed Windows test on Linuz

* Increase patch number

---------

Co-authored-by: gitmln <[email protected]>
Co-authored-by: Jose Perez Rodriguez <[email protected]>
  • Loading branch information
3 people authored Sep 6, 2024
1 parent f5b6188 commit 544b8d5
Show file tree
Hide file tree
Showing 10 changed files with 118 additions and 5 deletions.
4 changes: 2 additions & 2 deletions eng/Versions.props
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@
<PropertyGroup Label="Version settings">
<MajorVersion>8</MajorVersion>
<MinorVersion>9</MinorVersion>
<PatchVersion>0</PatchVersion>
<PatchVersion>1</PatchVersion>
<PreReleaseVersionLabel>rtm</PreReleaseVersionLabel>
<PreReleaseVersionIteration>
</PreReleaseVersionIteration>
<VersionPrefix>$(MajorVersion).$(MinorVersion).$(PatchVersion)</VersionPrefix>
<ValidateBaseline>true</ValidateBaseline>
<AssemblyVersion>$(MajorVersion).$(MinorVersion).0.0</AssemblyVersion>
<AssemblyVersion>$(MajorVersion).$(MinorVersion).$(PatchVersion).0</AssemblyVersion>
<!--
When DotNetFinalVersionKind is set to 'release', this branch will produce stable outputs for 'Shipping' packages
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ public LinuxUtilizationProvider(IOptions<ResourceMonitoringOptions> options, ILi
// We don't dispose the meter because IMeterFactory handles that
// An issue on analyzer side: https://github.com/dotnet/roslyn-analyzers/issues/6912
// Related documentation: https://github.com/dotnet/docs/pull/37170
var meter = meterFactory.Create(nameof(Microsoft.Extensions.Diagnostics.ResourceMonitoring));
var meter = meterFactory.Create(ResourceUtilizationInstruments.MeterName);
#pragma warning restore CA2000 // Dispose objects before losing scope

_ = meter.CreateObservableGauge(name: ResourceUtilizationInstruments.ContainerCpuLimitUtilization, observeValue: () => CpuUtilization() * _scaleRelativeToCpuLimit, unit: "1");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,11 @@ namespace Microsoft.Extensions.Diagnostics.ResourceMonitoring;
/// <seealso cref="System.Diagnostics.Metrics.Instrument"/>
internal static class ResourceUtilizationInstruments
{
/// <summary>
/// The name of the ResourceMonitoring Meter.
/// </summary>
public const string MeterName = "Microsoft.Extensions.Diagnostics.ResourceMonitoring";

/// <summary>
/// The name of an instrument to retrieve CPU limit consumption of all processes running inside a container or control group in range <c>[0, 1]</c>.
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ internal WindowsContainerSnapshotProvider(
// We don't dispose the meter because IMeterFactory handles that
// An issue on analyzer side: https://github.com/dotnet/roslyn-analyzers/issues/6912
// Related documentation: https://github.com/dotnet/docs/pull/37170
var meter = meterFactory.Create(nameof(Microsoft.Extensions.Diagnostics.ResourceMonitoring));
var meter = meterFactory.Create(ResourceUtilizationInstruments.MeterName);
#pragma warning restore CA2000 // Dispose objects before losing scope

// Container based metrics:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ internal WindowsSnapshotProvider(
// We don't dispose the meter because IMeterFactory handles that
// An issue on analyzer side: https://github.com/dotnet/roslyn-analyzers/issues/6912
// Related documentation: https://github.com/dotnet/docs/pull/37170
var meter = meterFactory.Create(nameof(Microsoft.Extensions.Diagnostics.ResourceMonitoring));
var meter = meterFactory.Create(ResourceUtilizationInstruments.MeterName);
#pragma warning restore CA2000 // Dispose objects before losing scope

_ = meter.CreateObservableGauge(name: ResourceUtilizationInstruments.ProcessCpuUtilization, observeValue: CpuPercentage);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System;
using System.Collections.Generic;
using System.Diagnostics.Metrics;

namespace Microsoft.Extensions.Diagnostics.ResourceMonitoring.Test.Helpers;

internal class TestMeterFactory : IMeterFactory
{
public List<Meter> Meters { get; } = new List<Meter>();

public Meter Create(MeterOptions options)
{
var meter = new Meter(options.Name, options.Version, Array.Empty<KeyValuePair<string, object?>>(), scope: this);
Meters.Add(meter);

return meter;
}

public Meter Create(string name)
{
return Create(new MeterOptions(name)
{
Version = null,
Tags = null,
Scope = null
});
}

public void Dispose()
{
foreach (var meter in Meters)
{
meter.Dispose();
}

Meters.Clear();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.Extensions.Diagnostics.ResourceMonitoring.Test.Helpers;
using Microsoft.Extensions.Logging.Testing;
using Microsoft.TestUtilities;
using Moq;
Expand Down Expand Up @@ -191,4 +192,17 @@ public Task Provider_EmitsLogRecord()

return Verifier.Verify(logRecords).UseDirectory(VerifiedDataDirectory);
}

[Fact]
public void Provider_Creates_Meter_With_Correct_Name()
{
var options = Options.Options.Create<ResourceMonitoringOptions>(new());
using var meterFactory = new TestMeterFactory();

var parser = new DummyLinuxUtilizationParser();
_ = new LinuxUtilizationProvider(options, parser, meterFactory);

var meter = meterFactory.Meters.Single();
Assert.Equal(ResourceUtilizationInstruments.MeterName, meter.Name);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using Microsoft.Extensions.Diagnostics.ResourceMonitoring.Linux;

namespace Microsoft.Extensions.Diagnostics.ResourceMonitoring.Linux.Test;

internal class DummyLinuxUtilizationParser : ILinuxUtilizationParser
{
public ulong GetAvailableMemoryInBytes() => 1;
public long GetCgroupCpuUsageInNanoseconds() => 0;
public float GetCgroupLimitedCpus() => 1;
public float GetCgroupRequestCpu() => 1;
public ulong GetHostAvailableMemory() => 0;
public float GetHostCpuCount() => 1;
public long GetHostCpuUsageInNanoseconds() => 0;
public ulong GetMemoryUsageInBytes() => 0;
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@

using System;
using System.Diagnostics.Metrics;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.Extensions.Diagnostics.Metrics.Testing;
using Microsoft.Extensions.Diagnostics.ResourceMonitoring.Test.Helpers;
using Microsoft.Extensions.Diagnostics.ResourceMonitoring.Windows.Interop;
using Microsoft.Extensions.Logging.Testing;
using Microsoft.Extensions.Time.Testing;
Expand Down Expand Up @@ -322,4 +324,24 @@ public Task SnapshotProvider_EmitsLogRecord()

return Verifier.Verify(logRecords).UniqueForRuntime().UseDirectory(VerifiedDataDirectory);
}

[Fact]
public void Provider_Creates_Meter_With_Correct_Name()
{
var options = Options.Options.Create<ResourceMonitoringOptions>(new());
using var meterFactory = new TestMeterFactory();

_ = new WindowsContainerSnapshotProvider(
_memoryInfoMock.Object,
_systemInfoMock.Object,
_processInfoMock.Object,
_logger,
meterFactory,
() => _jobHandleMock.Object,
new FakeTimeProvider(),
new());

var meter = meterFactory.Meters.Single();
Assert.Equal(ResourceUtilizationInstruments.MeterName, meter.Name);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@

using System;
using System.Diagnostics.Metrics;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.Extensions.Diagnostics.Metrics.Testing;
using Microsoft.Extensions.Diagnostics.ResourceMonitoring.Test.Helpers;
using Microsoft.Extensions.Diagnostics.ResourceMonitoring.Windows.Interop;
using Microsoft.Extensions.Logging.Testing;
using Microsoft.Extensions.Options;
Expand Down Expand Up @@ -155,4 +157,15 @@ public void Provider_Returns_MemoryConsumption()
var usage = WindowsSnapshotProvider.GetMemoryUsageInBytes();
Assert.InRange(usage, 0, long.MaxValue);
}

[ConditionalFact]
public void Provider_Creates_Meter_With_Correct_Name()
{
using var meterFactory = new TestMeterFactory();

_ = new WindowsSnapshotProvider(_fakeLogger, meterFactory, _options);

var meter = meterFactory.Meters.Single();
Assert.Equal(ResourceUtilizationInstruments.MeterName, meter.Name);
}
}

0 comments on commit 544b8d5

Please sign in to comment.