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

Metrics APIs Additions #86567

Merged
merged 5 commits into from
May 23, 2023
Merged
Show file tree
Hide file tree
Changes from 2 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
68 changes: 68 additions & 0 deletions src/libraries/Common/src/System/Diagnostics/DiagnosticsHelper.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System.Collections.Generic;

namespace System.Diagnostics
{
internal static class DiagnosticsHelper
{
internal static bool CompareTags(IEnumerable<KeyValuePair<string, object?>>? tags1, IEnumerable<KeyValuePair<string, object?>>? tags2)
{
if (tags1 is null)
{
return tags2 is null;
}

if (tags2 is null)
{
return false;
}

if (tags1 is ICollection<KeyValuePair<string, object?>> firstCol && tags2 is ICollection<KeyValuePair<string, object?>> secondCol)
{
int count = firstCol.Count;
if (count != secondCol.Count)
{
return false;
}

if (firstCol is IList<KeyValuePair<string, object?>> firstList && secondCol is IList<KeyValuePair<string, object?>> secondList)
{
for (int i = 0; i < count; i++)
{
KeyValuePair<string, object?> pair1 = firstList[i];
KeyValuePair<string, object?> pair2 = secondList[i];
if (pair1.Key != pair2.Key || !object.Equals(pair1.Value, pair2.Value))
{
return false;
}
}

return true;
}
}

using (IEnumerator<KeyValuePair<string, object?>> e1 = tags1.GetEnumerator())
using (IEnumerator<KeyValuePair<string, object?>> e2 = tags2.GetEnumerator())
{
while (e1.MoveNext())
{
KeyValuePair<string, object?> pair1 = e1.Current;
if (!e2.MoveNext())
{
return false;
}

KeyValuePair<string, object?> pair2 = e2.Current;
if (pair1.Key != pair2.Key || !object.Equals(pair1.Value, pair2.Value))
{
return false;
}
}

return !e2.MoveNext();
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
Microsoft Visual Studio Solution File, Format Version 12.00
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "TestUtilities", "..\Common\tests\TestUtilities\TestUtilities.csproj", "{E5B6A94F-615E-4DAF-8110-E5A776BB8296}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Microsoft.Extensions.Diagnostics.Abstractions", "ref\Microsoft.Extensions.Diagnostics.Abstractions.csproj", "{696D69C9-C5F6-405E-A8B5-5375D08BBADC}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Microsoft.Extensions.Diagnostics.Abstractions", "src\Microsoft.Extensions.Diagnostics.Abstractions.csproj", "{2AED2951-7724-4EFC-8E16-6DF877C6B4A6}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "tests", "tests", "{155C3F40-F63D-49DF-87D3-A3EEA27036E8}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "ref", "ref", "{50BA55F5-BD05-4C05-910F-2BFD20BD3465}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "src", "src", "{5725D7DF-DC33-47D2-90C9-D8736C579E77}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "gen", "gen", "{55D04C80-4A8F-40AC-967D-3FA77C814D7B}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{E5B6A94F-615E-4DAF-8110-E5A776BB8296}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{E5B6A94F-615E-4DAF-8110-E5A776BB8296}.Debug|Any CPU.Build.0 = Debug|Any CPU
{E5B6A94F-615E-4DAF-8110-E5A776BB8296}.Release|Any CPU.ActiveCfg = Release|Any CPU
{E5B6A94F-615E-4DAF-8110-E5A776BB8296}.Release|Any CPU.Build.0 = Release|Any CPU
{696D69C9-C5F6-405E-A8B5-5375D08BBADC}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{696D69C9-C5F6-405E-A8B5-5375D08BBADC}.Debug|Any CPU.Build.0 = Debug|Any CPU
{696D69C9-C5F6-405E-A8B5-5375D08BBADC}.Release|Any CPU.ActiveCfg = Release|Any CPU
{696D69C9-C5F6-405E-A8B5-5375D08BBADC}.Release|Any CPU.Build.0 = Release|Any CPU
{2AED2951-7724-4EFC-8E16-6DF877C6B4A6}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{2AED2951-7724-4EFC-8E16-6DF877C6B4A6}.Debug|Any CPU.Build.0 = Debug|Any CPU
{2AED2951-7724-4EFC-8E16-6DF877C6B4A6}.Release|Any CPU.ActiveCfg = Release|Any CPU
{2AED2951-7724-4EFC-8E16-6DF877C6B4A6}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(NestedProjects) = preSolution
{E5B6A94F-615E-4DAF-8110-E5A776BB8296} = {155C3F40-F63D-49DF-87D3-A3EEA27036E8}
{F5E89C81-2D78-4F86-ABD2-7D983F46EB58} = {155C3F40-F63D-49DF-87D3-A3EEA27036E8}
{5351C410-530D-4724-A8E6-430831E7332B} = {155C3F40-F63D-49DF-87D3-A3EEA27036E8}
{696D69C9-C5F6-405E-A8B5-5375D08BBADC} = {50BA55F5-BD05-4C05-910F-2BFD20BD3465}
{6D9C22DB-C4E3-483E-AF78-C1DCE6ED8DD6} = {50BA55F5-BD05-4C05-910F-2BFD20BD3465}
{C8C58A2A-B254-4A74-8C7B-9B0CA0A9E67A} = {50BA55F5-BD05-4C05-910F-2BFD20BD3465}
{CC0E8CC8-88F5-4D72-9B3F-0281DD58461A} = {50BA55F5-BD05-4C05-910F-2BFD20BD3465}
{7323E18C-BF40-4236-9AEF-273A94F3DB03} = {50BA55F5-BD05-4C05-910F-2BFD20BD3465}
{536A305D-DC57-4E7F-B21B-B8DE916A63C3} = {50BA55F5-BD05-4C05-910F-2BFD20BD3465}
{50F89560-1449-44E4-844E-72815256534B} = {50BA55F5-BD05-4C05-910F-2BFD20BD3465}
{8E212A9D-391B-4EFA-943D-7D104A9D3D7E} = {50BA55F5-BD05-4C05-910F-2BFD20BD3465}
{2AED2951-7724-4EFC-8E16-6DF877C6B4A6} = {5725D7DF-DC33-47D2-90C9-D8736C579E77}
{5C580568-6072-4F27-B5C6-FA04556E3B98} = {5725D7DF-DC33-47D2-90C9-D8736C579E77}
{7902A0CA-E94D-4C96-A112-455A1E5E2390} = {5725D7DF-DC33-47D2-90C9-D8736C579E77}
{FA7201FE-097D-4197-BDEC-329986814D8D} = {5725D7DF-DC33-47D2-90C9-D8736C579E77}
{7B4B86A8-EF62-45F0-A431-05F4C11A2E0D} = {55D04C80-4A8F-40AC-967D-3FA77C814D7B}
{6F992337-EAEE-4F7A-BF3B-DDC526F054F7} = {55D04C80-4A8F-40AC-967D-3FA77C814D7B}
{240E7B1C-6D7D-4932-9598-815C2EA420AF} = {55D04C80-4A8F-40AC-967D-3FA77C814D7B}
{B4C5A0BE-3E21-4464-875C-E6EBED176EE3} = {55D04C80-4A8F-40AC-967D-3FA77C814D7B}
{30DEAC25-AFDB-4E91-90A4-7B94E1809C59} = {55D04C80-4A8F-40AC-967D-3FA77C814D7B}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {810D114A-26F4-4151-9EFE-29A7F1BF62AA}
EndGlobalSection
EndGlobal
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# Microsoft.Extensions.Diagnostics.Abstractions

`Microsoft.Extensions.Diagnostics.Abstractions` includes the various abstraction types for telemetry, such as IMeterFactory.

Commonly Used Types:
- Microsoft.Extensions.Metrics.IMeterFactory

## Contribution Bar
- [x] [We consider new features, new APIs, bug fixes, and performance changes](https://github.com/dotnet/runtime/tree/main/src/libraries#contribution-bar)

The APIs and functionality are mature, but do get extended occasionally.

## Deployment
[Microsoft.Extensions.Diagnostics.Abstractions](https://www.nuget.org/packages/Microsoft.Extensions.Diagnostics.Abstractions) is included in the ASP.NET Core shared framework. The package is deployed as out-of-band (OOB) too and can be referenced into projects directly.
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
// 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.Metrics
{
public interface IMeterFactory : System.IDisposable
{
System.Diagnostics.Metrics.Meter Create(System.Diagnostics.Metrics.MeterOptions options);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
tarekgh marked this conversation as resolved.
Show resolved Hide resolved
<TargetFrameworks>$(NetCoreAppCurrent);$(NetCoreAppPrevious);$(NetCoreAppMinimum);netstandard2.0;$(NetFrameworkMinimum)</TargetFrameworks>
<CLSCompliant>false</CLSCompliant>
</PropertyGroup>

<ItemGroup>
<Compile Include="Microsoft.Extensions.Diagnostics.Abstractions.cs" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="$(LibrariesProjectRoot)System.Diagnostics.DiagnosticSource\ref\System.Diagnostics.DiagnosticSource.csproj" />
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
// 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.Diagnostics.Metrics;

namespace Microsoft.Extensions.Diagnostics.Metrics
tarekgh marked this conversation as resolved.
Show resolved Hide resolved
{
/// <summary>
/// A factory for creating <see cref="Meter"/> instances.
/// </summary>
/// <remarks>
/// Meter factories will be accountable for the following responsibilities:
/// - Creating a new meter.
/// - Attaching the factory instance as a scope to the Meter constructor for all created Meter objects.
/// - Storing created meters in a cache and returning a cached instance if a meter with the same parameters (name, version, and tags) is requested.
/// - Disposing of all cached Meter objects upon factory disposal.
/// </remarks>
public interface IMeterFactory : IDisposable
{
/// <summary>
/// Creates a new <see cref="Meter"/> instance.
/// </summary>
/// <param name="options">The <see cref="MeterOptions"/> to use when creating the meter.</param>
/// <returns>A new <see cref="Meter"/> instance.</returns>
/// <remarks>
/// The <see cref="Meter"/> instance returned by this method should be cached by the factory and returned for subsequent requests for a meter with the same parameters (name, version, and tags).
/// </remarks>
Meter Create(MeterOptions options);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
tarekgh marked this conversation as resolved.
Show resolved Hide resolved
<TargetFrameworks>$(NetCoreAppCurrent);$(NetCoreAppPrevious);$(NetCoreAppMinimum);netstandard2.0;$(NetFrameworkMinimum)</TargetFrameworks>
<EnableDefaultItems>true</EnableDefaultItems>
<CLSCompliant>false</CLSCompliant>
<!-- Disabling baseline validation since this is a brand new package.
Once this package has shipped a stable version, the following line
should be removed in order to re-enable validation. -->
<DisablePackageBaselineValidation>true</DisablePackageBaselineValidation>
tarekgh marked this conversation as resolved.
Show resolved Hide resolved
<IsPackable>true</IsPackable>
<PackageDescription>Abstraction types such as IMeterFactory are employed by the telemetry framework extensions.</PackageDescription>
</PropertyGroup>

<ItemGroup>
<ProjectReference Include="$(LibrariesProjectRoot)System.Diagnostics.DiagnosticSource\src\System.Diagnostics.DiagnosticSource.csproj" />
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
Microsoft Visual Studio Solution File, Format Version 12.00
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "TestUtilities", "..\Common\tests\TestUtilities\TestUtilities.csproj", "{B6663ACE-6FE4-4BB4-8B35-AB98EF62EAAE}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Microsoft.Extensions.Diagnostics", "ref\Microsoft.Extensions.Diagnostics.csproj", "{EF75497C-6CB7-4471-980A-619EA1AB8CF6}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Microsoft.Extensions.Diagnostics", "src\Microsoft.Extensions.Diagnostics.csproj", "{09E28D94-B771-48EB-800C-5A80C2C0055C}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Microsoft.Extensions.Diagnostics.Tests", "tests\Microsoft.Extensions.Diagnostics.Tests.csproj", "{43DBAD84-A865-4F5F-AB76-7F3EB6784E99}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "tests", "tests", "{76DC9C4C-EE53-47E6-B6BF-7B135EA8CAF3}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "ref", "ref", "{9BF048D0-411D-4C2A-8C32-3A3255501D27}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "src", "src", "{A447D0CB-601B-479E-A2B2-76E48F5D4D61}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{B6663ACE-6FE4-4BB4-8B35-AB98EF62EAAE}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{B6663ACE-6FE4-4BB4-8B35-AB98EF62EAAE}.Debug|Any CPU.Build.0 = Debug|Any CPU
{B6663ACE-6FE4-4BB4-8B35-AB98EF62EAAE}.Release|Any CPU.ActiveCfg = Release|Any CPU
{B6663ACE-6FE4-4BB4-8B35-AB98EF62EAAE}.Release|Any CPU.Build.0 = Release|Any CPU
{EF75497C-6CB7-4471-980A-619EA1AB8CF6}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{EF75497C-6CB7-4471-980A-619EA1AB8CF6}.Debug|Any CPU.Build.0 = Debug|Any CPU
{EF75497C-6CB7-4471-980A-619EA1AB8CF6}.Release|Any CPU.ActiveCfg = Release|Any CPU
{EF75497C-6CB7-4471-980A-619EA1AB8CF6}.Release|Any CPU.Build.0 = Release|Any CPU
{09E28D94-B771-48EB-800C-5A80C2C0055C}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{09E28D94-B771-48EB-800C-5A80C2C0055C}.Debug|Any CPU.Build.0 = Debug|Any CPU
{09E28D94-B771-48EB-800C-5A80C2C0055C}.Release|Any CPU.ActiveCfg = Release|Any CPU
{09E28D94-B771-48EB-800C-5A80C2C0055C}.Release|Any CPU.Build.0 = Release|Any CPU
{43DBAD84-A865-4F5F-AB76-7F3EB6784E99}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{43DBAD84-A865-4F5F-AB76-7F3EB6784E99}.Debug|Any CPU.Build.0 = Debug|Any CPU
{43DBAD84-A865-4F5F-AB76-7F3EB6784E99}.Release|Any CPU.ActiveCfg = Release|Any CPU
{43DBAD84-A865-4F5F-AB76-7F3EB6784E99}.Release|Any CPU.Build.0 = Release|Any CPU
{87AA8A4F-2785-4E1D-BAB2-3C6414C8D387}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{87AA8A4F-2785-4E1D-BAB2-3C6414C8D387}.Debug|Any CPU.Build.0 = Debug|Any CPU
{87AA8A4F-2785-4E1D-BAB2-3C6414C8D387}.Release|Any CPU.ActiveCfg = Release|Any CPU
{87AA8A4F-2785-4E1D-BAB2-3C6414C8D387}.Release|Any CPU.Build.0 = Release|Any CPU
{959348BC-2D38-41F4-9F3D-2E1CD18D9477}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{959348BC-2D38-41F4-9F3D-2E1CD18D9477}.Debug|Any CPU.Build.0 = Debug|Any CPU
{959348BC-2D38-41F4-9F3D-2E1CD18D9477}.Release|Any CPU.ActiveCfg = Release|Any CPU
{959348BC-2D38-41F4-9F3D-2E1CD18D9477}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(NestedProjects) = preSolution
{B6663ACE-6FE4-4BB4-8B35-AB98EF62EAAE} = {76DC9C4C-EE53-47E6-B6BF-7B135EA8CAF3}
{43DBAD84-A865-4F5F-AB76-7F3EB6784E99} = {76DC9C4C-EE53-47E6-B6BF-7B135EA8CAF3}
{EF75497C-6CB7-4471-980A-619EA1AB8CF6} = {9BF048D0-411D-4C2A-8C32-3A3255501D27}
{09E28D94-B771-48EB-800C-5A80C2C0055C} = {A447D0CB-601B-479E-A2B2-76E48F5D4D61}
{87AA8A4F-2785-4E1D-BAB2-3C6414C8D387} = {069D6714-DE43-45C4-BABC-C8246B974CA7}
{959348BC-2D38-41F4-9F3D-2E1CD18D9477} = {069D6714-DE43-45C4-BABC-C8246B974CA7}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {7D279EE5-E38F-4125-AE82-6ADE52D72F26}
EndGlobalSection
EndGlobal
15 changes: 15 additions & 0 deletions src/libraries/Microsoft.Extensions.Diagnostics/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# Microsoft.Extensions.Diagnostics

`Microsoft.Extensions.Diagnostics` contains the default implementation of meter factory and extension methods for registering this default meter factory to the DI.

Commonly Used APIS:
- MetricsServiceExtensions.AddMetrics(this IServiceCollection services)
- MeterFactoryExtensions.Create(this IMeterFactory, string name, string? version = null, IEnumerable<KeyValuePair<string,object?>> tags = null, object? scope = null)

## Contribution Bar
- [x] [We consider new features, new APIs, bug fixes, and performance changes](https://github.com/dotnet/runtime/tree/main/src/libraries#contribution-bar)

The APIs and functionality are mature, but do get extended occasionally.

## Deployment
[Microsoft.Extensions.Diagnostics](https://www.nuget.org/packages/Microsoft.Extensions.Diagnostics) is included in the ASP.NET Core shared framework. The package is deployed as out-of-band (OOB) too and can be referenced into projects directly.
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
// 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.Metrics
{
public static class MetricsServiceExtensions
{
public static Microsoft.Extensions.DependencyInjection.IServiceCollection AddMetrics(this Microsoft.Extensions.DependencyInjection.IServiceCollection services) { return null!; }
}
public static class MeterFactoryExtensions
{
public static System.Diagnostics.Metrics.Meter Create(this Microsoft.Extensions.Diagnostics.Metrics.IMeterFactory meterFactory, string name, string? version = null, System.Collections.Generic.IEnumerable<System.Collections.Generic.KeyValuePair<string, object?>>? tags = null, object? scope = null) { return null!; }
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
tarekgh marked this conversation as resolved.
Show resolved Hide resolved
<TargetFrameworks>$(NetCoreAppCurrent);$(NetCoreAppPrevious);$(NetCoreAppMinimum);netstandard2.0;$(NetFrameworkMinimum)</TargetFrameworks>
<CLSCompliant>false</CLSCompliant>
</PropertyGroup>

<ItemGroup>
<Compile Include="Microsoft.Extensions.Diagnostics.cs" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="$(LibrariesProjectRoot)System.Diagnostics.DiagnosticSource\ref\System.Diagnostics.DiagnosticSource.csproj" />
<ProjectReference Include="$(LibrariesProjectRoot)Microsoft.Extensions.Diagnostics.Abstractions\ref\Microsoft.Extensions.Diagnostics.Abstractions.csproj" />
<ProjectReference Include="$(LibrariesProjectRoot)Microsoft.Extensions.DependencyInjection.Abstractions\ref\Microsoft.Extensions.DependencyInjection.Abstractions.csproj" />
</ItemGroup>

</Project>
Loading