Skip to content

Commit

Permalink
Move ResourceUtilizationInstruments to Shared project (#5844)
Browse files Browse the repository at this point in the history
* Move ResourceUtilizationInstruments to Shared project

* Update readme

* Fix warnings
  • Loading branch information
amadeuszl authored Feb 5, 2025
1 parent bf3ac53 commit bab8a9b
Show file tree
Hide file tree
Showing 18 changed files with 41 additions and 8 deletions.
4 changes: 4 additions & 0 deletions eng/MSBuild/Shared.props
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@
<Compile Include="$(MSBuildThisFileDirectory)\..\..\src\Shared\EmptyCollections\*.cs" LinkBase="Shared\EmptyCollections" />
</ItemGroup>

<ItemGroup Condition="'$(InjectSharedInstruments)' == 'true'">
<Compile Include="$(MSBuildThisFileDirectory)\..\..\src\Shared\Instruments\*.cs" LinkBase="Shared\Instruments" />
</ItemGroup>

<ItemGroup Condition="'$(InjectSharedRentedSpan)' == 'true'">
<Compile Include="$(MSBuildThisFileDirectory)\..\..\src\Shared\RentedSpan\*.cs" LinkBase="Shared\RentedSpan" />
</ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
<InjectSharedDiagnosticIds>true</InjectSharedDiagnosticIds>
<InjectExperimentalAttributeOnLegacy>true</InjectExperimentalAttributeOnLegacy>
<InjectObsoleteAttributeOnLegacy>true</InjectObsoleteAttributeOnLegacy>
<InjectSharedInstruments>true</InjectSharedInstruments>
</PropertyGroup>

<PropertyGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
using Microsoft.Extensions.Diagnostics.ResourceMonitoring;
using Microsoft.Extensions.Options;
using Microsoft.Shared.Diagnostics;
using Microsoft.Shared.Instruments;

namespace Microsoft.Extensions.Diagnostics.HealthChecks;

Expand Down Expand Up @@ -157,7 +158,7 @@ private void Dispose(bool disposing)

private void OnInstrumentPublished(Instrument instrument, MeterListener listener)
{
if (instrument.Meter.Name is "Microsoft.Extensions.Diagnostics.ResourceMonitoring")
if (instrument.Meter.Name == ResourceUtilizationInstruments.MeterName)
{
listener.EnableMeasurementEvents(instrument);
}
Expand All @@ -169,12 +170,12 @@ private void OnMeasurementRecorded(
{
switch (instrument.Name)
{
case "process.cpu.utilization":
case "container.cpu.limit.utilization":
case ResourceUtilizationInstruments.ProcessCpuUtilization:
case ResourceUtilizationInstruments.ContainerCpuLimitUtilization:
_cpuUsedPercentage = measurement * _multiplier;
break;
case "dotnet.process.memory.virtual.utilization":
case "container.memory.limit.utilization":
case ResourceUtilizationInstruments.ProcessMemoryUtilization:
case ResourceUtilizationInstruments.ContainerMemoryLimitUtilization:
_memoryUsedPercentage = measurement * _multiplier;
break;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Logging.Abstractions;
using Microsoft.Extensions.Options;
using Microsoft.Shared.Instruments;

namespace Microsoft.Extensions.Diagnostics.ResourceMonitoring.Linux;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using System.Collections.Generic;
using System.Diagnostics;
using System.Diagnostics.Metrics;
using Microsoft.Shared.Instruments;

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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
<InjectSharedBufferWriterPool>true</InjectSharedBufferWriterPool>
<InjectSharedDiagnosticIds>true</InjectSharedDiagnosticIds>
<InjectStringSplitExtensions>true</InjectStringSplitExtensions>
<InjectSharedInstruments>true</InjectSharedInstruments>
</PropertyGroup>

<PropertyGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using System.Collections.Generic;
using System.Diagnostics;
using System.Diagnostics.Metrics;
using Microsoft.Shared.Instruments;

namespace Microsoft.Extensions.Diagnostics.ResourceMonitoring.Windows.Network;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Logging.Abstractions;
using Microsoft.Extensions.Options;
using Microsoft.Shared.Instruments;

namespace Microsoft.Extensions.Diagnostics.ResourceMonitoring.Windows;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Logging.Abstractions;
using Microsoft.Extensions.Options;
using Microsoft.Shared.Instruments;

namespace Microsoft.Extensions.Diagnostics.ResourceMonitoring.Windows;

Expand Down
11 changes: 11 additions & 0 deletions src/Shared/Instruments/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# Diagnostic IDs

Contains common telemetry instruments names that are created or consumed in other projects.

To use this in your project, add the following to your `.csproj` file:

```xml
<PropertyGroup>
<InjectSharedInstruments>true</InjectSharedInstruments>
</PropertyGroup>
```
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System;
#pragma warning disable CA1716
namespace Microsoft.Shared.Instruments;
#pragma warning restore CA1716

namespace Microsoft.Extensions.Diagnostics.ResourceMonitoring;
#pragma warning disable CS1574

/// <summary>
/// Represents the names of instruments published by this package.
Expand Down Expand Up @@ -64,3 +66,5 @@ internal static class ResourceUtilizationInstruments
/// </remarks>
public const string SystemNetworkConnections = "system.network.connections";
}

#pragma warning disable CS1574
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,11 @@
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.DependencyInjection.Extensions;
using Microsoft.Extensions.Diagnostics.ResourceMonitoring.Linux.Test;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Hosting.Testing;
using Microsoft.Extensions.Options;
using Microsoft.Extensions.Time.Testing;
using Microsoft.Shared.Instruments;
using Microsoft.TestUtilities;
using Xunit;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
using System.Threading;
using FluentAssertions;
using Microsoft.Extensions.Diagnostics.ResourceMonitoring.Linux.Network;
using Microsoft.Shared.Instruments;
using Moq;
using Xunit;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using System.Linq;
using Microsoft.Extensions.Diagnostics.ResourceMonitoring.Linux.Network;
using Microsoft.Extensions.Diagnostics.ResourceMonitoring.Test.Helpers;
using Microsoft.Shared.Instruments;
using Microsoft.TestUtilities;
using Moq;
using Xunit;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
using System.Threading.Tasks;
using Microsoft.Extensions.Diagnostics.ResourceMonitoring.Test.Helpers;
using Microsoft.Extensions.Logging.Testing;
using Microsoft.Shared.Instruments;
using Microsoft.TestUtilities;
using Moq;
using VerifyXunit;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
using Microsoft.Extensions.Diagnostics.ResourceMonitoring.Windows.Interop;
using Microsoft.Extensions.Logging.Testing;
using Microsoft.Extensions.Time.Testing;
using Microsoft.Shared.Instruments;
using Moq;
using VerifyXunit;
using Xunit;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using System.Linq;
using Microsoft.Extensions.Diagnostics.ResourceMonitoring.Test.Helpers;
using Microsoft.Extensions.Diagnostics.ResourceMonitoring.Windows.Network;
using Microsoft.Shared.Instruments;
using Microsoft.TestUtilities;
using Moq;
using Xunit;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
using Microsoft.Extensions.Logging.Testing;
using Microsoft.Extensions.Options;
using Microsoft.Extensions.Time.Testing;
using Microsoft.Shared.Instruments;
using Microsoft.TestUtilities;
using Moq;
using VerifyXunit;
Expand Down

0 comments on commit bab8a9b

Please sign in to comment.