-
Notifications
You must be signed in to change notification settings - Fork 841
Add Windows disk I/O metrics to ResourceMonitoring #6181
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
Merged
Merged
Changes from 10 commits
Commits
Show all changes
31 commits
Select commit
Hold shift + click to select a range
555b98e
create an empty file for WindowsDiskMetrics
makazeu 2e8a9be
Implement Windows Disk I/O bytes metric
makazeu fe0c09b
update
makazeu 664053a
Add SupportedOSPlatform attribute for Windows in disk metrics classes
makazeu 3180f66
update
makazeu ccb3d27
Fix property name for disk I/O metrics enabling in Windows
makazeu 24e60bd
update
makazeu f6957fe
Improve error handling in disk performance counter initialization
makazeu 07ceca6
Rename disk performance counter classes
makazeu 9e12912
Implement Disk Operations metric
makazeu 37c9e8a
Style updates
makazeu 8bba5a4
Add PlatformAttributes in LegacySupport
makazeu 878e61b
Add a simple unit test
makazeu e9985af
Add interfaces for PerformanceCounter
makazeu 6d96e50
update ut
makazeu 6095dd0
Merge branch 'main' into windows-disk-metrics
makazeu 718bc13
Use TimeProvider
makazeu db2949a
Add TimeProvider as a singleton in ResourceMonitoringServiceCollectio…
makazeu 12edc8a
Add unit tests for PerformanceCounterFactory and PerformanceCounterWr…
makazeu 23a0f3d
update
makazeu 6c108da
Add Unit Tests
makazeu 080ae7d
Refine
makazeu a3efed7
Refine
makazeu 7053f3f
Refine
makazeu 93ea836
Add WindowsDiskMetricsTests
makazeu 54836ec
Refine
makazeu a1b620e
Merge branch 'main' into windows-disk-metrics
makazeu a1de91a
clean
makazeu a0395c0
Refine
makazeu c0fbc4d
Merge branch 'main' into windows-disk-metrics
makazeu d9d9194
Merge branch 'dotnet:main' into windows-disk-metrics
makazeu File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
145 changes: 145 additions & 0 deletions
145
...es/Microsoft.Extensions.Diagnostics.ResourceMonitoring/Windows/Disk/WindowsDiskMetrics.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,145 @@ | ||
| // 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; | ||
| using System.Diagnostics.Metrics; | ||
| using Microsoft.Extensions.Options; | ||
| using Microsoft.Shared.Instruments; | ||
|
|
||
| #if NETCOREAPP | ||
| using System.Runtime.Versioning; | ||
| #endif | ||
|
|
||
| namespace Microsoft.Extensions.Diagnostics.ResourceMonitoring.Windows.Disk; | ||
|
|
||
| #if NETCOREAPP | ||
| [SupportedOSPlatform("windows")] | ||
| #endif | ||
makazeu marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| internal sealed class WindowsDiskMetrics | ||
| { | ||
| private const string LogicalDiskCategory = "LogicalDisk"; | ||
|
|
||
| private const string DeviceKey = "system.device"; | ||
|
|
||
| private const string DirectionKey = "disk.io.direction"; | ||
|
|
||
| private static readonly KeyValuePair<string, object?> _directionReadTag = new(DirectionKey, "read"); | ||
|
|
||
| private static readonly KeyValuePair<string, object?> _directionWriteTag = new(DirectionKey, "write"); | ||
|
|
||
| private readonly Dictionary<string, WindowsDiskPerSecondPerfCounters> _perSecondCounters = new(); | ||
makazeu marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| public WindowsDiskMetrics(IMeterFactory meterFactory, IOptions<ResourceMonitoringOptions> options) | ||
| { | ||
| if (!options.Value.EnableDiskIoMetrics) | ||
| { | ||
| return; | ||
| } | ||
|
|
||
| #pragma warning disable CA2000 // Dispose objects before losing scope | ||
| // We don't dispose the meter because IMeterFactory handles that | ||
| // It's a false-positive, see: https://github.com/dotnet/roslyn-analyzers/issues/6912. | ||
| // Related documentation: https://github.com/dotnet/docs/pull/37170 | ||
| Meter meter = meterFactory.Create(ResourceUtilizationInstruments.MeterName); | ||
| #pragma warning restore CA2000 // Dispose objects before losing scope | ||
|
|
||
| InitializeDiskCounters(); | ||
|
|
||
| // The metric is aligned with | ||
| // https://github.com/open-telemetry/semantic-conventions/blob/main/docs/system/system-metrics.md#metric-systemdiskio | ||
| _ = meter.CreateObservableCounter( | ||
| ResourceUtilizationInstruments.SystemDiskIo, | ||
| GetDiskIoMeasurements, | ||
| unit: "By", | ||
| description: "Disk bytes transferred"); | ||
|
|
||
| // The metric is aligned with | ||
| // https://github.com/open-telemetry/semantic-conventions/blob/main/docs/system/system-metrics.md#metric-systemdiskoperations | ||
| _ = meter.CreateObservableCounter( | ||
| ResourceUtilizationInstruments.SystemDiskOperations, | ||
| GetDiskOperationMeasurements, | ||
| unit: "{operation}", | ||
| description: "Disk operations"); | ||
| } | ||
|
|
||
| private void InitializeDiskCounters() | ||
| { | ||
| var diskCategory = new PerformanceCounterCategory(LogicalDiskCategory); | ||
|
|
||
| foreach (string counterName in _perSecondPerformanceCounters) | ||
| { | ||
| try | ||
| { | ||
| var diskPerfCounter = new WindowsDiskPerSecondPerfCounters(diskCategory, counterName); | ||
| diskPerfCounter.InitializeDiskCounters(); | ||
| _perSecondCounters.Add(counterName, diskPerfCounter); | ||
| } | ||
| #pragma warning disable CA1031 | ||
| catch (Exception ex) | ||
| #pragma warning restore CA1031 | ||
| { | ||
| Debug.WriteLine("Error initializing disk performance counter: " + ex.Message); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| private IEnumerable<Measurement<long>> GetDiskIoMeasurements() | ||
| { | ||
| List<Measurement<long>> measurements = []; | ||
|
|
||
| if (_perSecondCounters.TryGetValue(WindowsDiskPerfCounterNames.DiskWriteBytesCounter, out WindowsDiskPerSecondPerfCounters? perSecondWriteCounter)) | ||
| { | ||
| perSecondWriteCounter.UpdateDiskCounters(); | ||
| foreach (KeyValuePair<string, long> pair in perSecondWriteCounter.TotalCountDict) | ||
| { | ||
| measurements.Add(new Measurement<long>(pair.Value, new TagList { _directionWriteTag, new(DeviceKey, pair.Key) })); | ||
| } | ||
| } | ||
|
|
||
| if (_perSecondCounters.TryGetValue(WindowsDiskPerfCounterNames.DiskReadBytesCounter, out WindowsDiskPerSecondPerfCounters? perSecondReadCounter)) | ||
| { | ||
| perSecondReadCounter.UpdateDiskCounters(); | ||
| foreach (KeyValuePair<string, long> pair in perSecondReadCounter.TotalCountDict) | ||
| { | ||
| measurements.Add(new Measurement<long>(pair.Value, new TagList { _directionReadTag, new(DeviceKey, pair.Key) })); | ||
| } | ||
| } | ||
|
|
||
| return measurements; | ||
| } | ||
|
|
||
| private IEnumerable<Measurement<long>> GetDiskOperationMeasurements() | ||
| { | ||
| List<Measurement<long>> measurements = []; | ||
|
|
||
| if (_perSecondCounters.TryGetValue(WindowsDiskPerfCounterNames.DiskWritesCounter, out WindowsDiskPerSecondPerfCounters? perSecondWriteCounter)) | ||
| { | ||
| perSecondWriteCounter.UpdateDiskCounters(); | ||
| foreach (KeyValuePair<string, long> pair in perSecondWriteCounter.TotalCountDict) | ||
| { | ||
| measurements.Add(new Measurement<long>(pair.Value, new TagList { _directionWriteTag, new(DeviceKey, pair.Key) })); | ||
| } | ||
| } | ||
|
|
||
| if (_perSecondCounters.TryGetValue(WindowsDiskPerfCounterNames.DiskReadsCounter, out WindowsDiskPerSecondPerfCounters? perSecondReadCounter)) | ||
| { | ||
| perSecondReadCounter.UpdateDiskCounters(); | ||
| foreach (KeyValuePair<string, long> pair in perSecondReadCounter.TotalCountDict) | ||
| { | ||
| measurements.Add(new Measurement<long>(pair.Value, new TagList { _directionReadTag, new(DeviceKey, pair.Key) })); | ||
| } | ||
| } | ||
|
|
||
| return measurements; | ||
| } | ||
|
|
||
| private static readonly List<string> _perSecondPerformanceCounters = | ||
makazeu marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| [ | ||
| WindowsDiskPerfCounterNames.DiskWriteBytesCounter, | ||
| WindowsDiskPerfCounterNames.DiskReadBytesCounter, | ||
| WindowsDiskPerfCounterNames.DiskWritesCounter, | ||
| WindowsDiskPerfCounterNames.DiskReadsCounter, | ||
| ]; | ||
| } | ||
76 changes: 76 additions & 0 deletions
76
...xtensions.Diagnostics.ResourceMonitoring/Windows/Disk/WindowsDiskPerSecondPerfCounters.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,76 @@ | ||
| // 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; | ||
|
|
||
| #if NETCOREAPP | ||
| using System.Runtime.Versioning; | ||
| #endif | ||
|
|
||
| namespace Microsoft.Extensions.Diagnostics.ResourceMonitoring.Windows.Disk; | ||
|
|
||
| #if NETCOREAPP | ||
| [SupportedOSPlatform("windows")] | ||
| #endif | ||
| internal sealed class WindowsDiskPerSecondPerfCounters | ||
| { | ||
| internal Dictionary<string, long> TotalCountDict { get; } = []; | ||
makazeu marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| private readonly List<PerformanceCounter> _counters = []; | ||
|
|
||
| private readonly PerformanceCounterCategory _category; | ||
|
|
||
| private readonly string _counterName; | ||
|
|
||
| private long _lastTimestamp; | ||
|
|
||
| internal WindowsDiskPerSecondPerfCounters(PerformanceCounterCategory category, string counterName) | ||
| { | ||
| _category = category; | ||
| _counterName = counterName; | ||
| } | ||
|
|
||
| internal void InitializeDiskCounters() | ||
| { | ||
| string[]? instanceNames = _category.GetInstanceNames(); | ||
| foreach (string instance in instanceNames) | ||
| { | ||
| // Skip the total instance | ||
| if (instance.Equals("_Total", StringComparison.OrdinalIgnoreCase)) | ||
| { | ||
| continue; | ||
| } | ||
|
|
||
| // Create counters for each disk | ||
| _counters.Add(new PerformanceCounter(_category.CategoryName, _counterName, instance)); | ||
| TotalCountDict.Add(instance, 0); | ||
| } | ||
|
|
||
| foreach (PerformanceCounter counter in _counters) | ||
| { | ||
| _ = counter.NextValue(); | ||
| } | ||
|
|
||
| #pragma warning disable EA0002 | ||
| _lastTimestamp = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds(); | ||
| #pragma warning restore EA0002 | ||
| } | ||
|
|
||
| internal void UpdateDiskCounters() | ||
| { | ||
| #pragma warning disable EA0002 | ||
| long currentTimestamp = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds(); | ||
| #pragma warning restore EA0002 | ||
| double elapsedTime = (currentTimestamp - _lastTimestamp) / 1000.0; // Convert to seconds | ||
|
|
||
| foreach (PerformanceCounter counter in _counters) | ||
| { | ||
| double value = counter.NextValue() * elapsedTime; | ||
| TotalCountDict[counter.InstanceName] += (long)value; | ||
| } | ||
|
|
||
| _lastTimestamp = currentTimestamp; | ||
| } | ||
| } | ||
15 changes: 15 additions & 0 deletions
15
...oft.Extensions.Diagnostics.ResourceMonitoring/Windows/Disk/WindowsDiskPerfCounterNames.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,15 @@ | ||
| // Licensed to the .NET Foundation under one or more agreements. | ||
| // The .NET Foundation licenses this file to you under the MIT license. | ||
|
|
||
| namespace Microsoft.Extensions.Diagnostics.ResourceMonitoring.Windows.Disk; | ||
|
|
||
| internal static class WindowsDiskPerfCounterNames | ||
| { | ||
| internal const string DiskWriteBytesCounter = "Disk Write Bytes/sec"; | ||
|
|
||
| internal const string DiskReadBytesCounter = "Disk Read Bytes/sec"; | ||
|
|
||
| internal const string DiskWritesCounter = "Disk Writes/sec"; | ||
|
|
||
| internal const string DiskReadsCounter = "Disk Reads/sec"; | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.