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

Revert correct Meter name #5403

Merged
merged 3 commits into from
Sep 6, 2024
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
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);
evgenyfedorov2 marked this conversation as resolved.
Show resolved Hide resolved
}
}