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 1 commit
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
Prev Previous commit
Next Next commit
Address the feedback
  • Loading branch information
tarekgh committed May 22, 2023
commit 9d21af64b6ae5fb2004b9d81c2483e9e4994477c
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
@@ -1,7 +1,7 @@
// 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.Metrics
namespace Microsoft.Extensions.Diagnostics.Metrics
{
public interface IMeterFactory : System.IDisposable
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<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>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
using System;
using System.Diagnostics.Metrics;

namespace Microsoft.Extensions.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.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<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>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +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.Metrics
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.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!; }
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
@@ -1,4 +1,5 @@
<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>
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
// 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;

namespace Microsoft.Extensions.Diagnostics.Metrics
{
internal sealed class DefaultMeterFactory : IMeterFactory
{
private readonly Dictionary<string, List<Meter>> _cachedMeters = new();
private bool _disposed;

public DefaultMeterFactory() { }

public Meter Create(MeterOptions options)
{
if (options is null)
{
throw new ArgumentNullException(nameof(options));
}

Debug.Assert(options.Name is not null);

lock (_cachedMeters)
{
if (_disposed)
{
throw new ObjectDisposedException(nameof(DefaultMeterFactory));
}

if (_cachedMeters.TryGetValue(options.Name, out List<Meter>? meterList))
{
foreach (Meter meter in meterList)
{
if (meter.Version == options.Version && DiagnosticsHelper.CompareTags(meter.Tags, options.Tags))
tarekgh marked this conversation as resolved.
Show resolved Hide resolved
{
return meter;
}
}
}
else
{
meterList = new List<Meter>();
_cachedMeters.Add(options.Name, meterList);
}

Meter m = new Meter(options.Name, options.Version, options.Tags, scope: this);
meterList.Add(m);
return m;
}
}

public void Dispose()
{
lock (_cachedMeters)
{
if (_disposed)
{
return;
}

_disposed = true;

foreach (List<Meter> meterList in _cachedMeters.Values)
{
foreach (Meter meter in meterList)
{
meter.Dispose();
}
}

_cachedMeters.Clear();
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
using System.Collections.Generic;
using System.Diagnostics.Metrics;

namespace Microsoft.Extensions.Metrics
namespace Microsoft.Extensions.Diagnostics.Metrics
{
/// <summary>
/// Extension methods for <see cref="Meter" /> and <see cref="IMeterFactory" />.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
using Microsoft.Extensions.DependencyInjection.Extensions;
using System;

namespace Microsoft.Extensions.Metrics
namespace Microsoft.Extensions.Diagnostics.Metrics
{
/// <summary>
/// Extension methods for setting up metrics services in an <see cref="IServiceCollection" />.
Expand All @@ -19,7 +19,7 @@ public static class MetricsServiceExtensions
/// <returns>The <see cref="IServiceCollection"/> so that additional calls can be chained.</returns>
public static IServiceCollection AddMetrics(this IServiceCollection services)
{
if (services == null)
if (services is null)
{
throw new ArgumentNullException(nameof(services));
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<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>
Expand All @@ -11,6 +12,10 @@
<PackageDescription>This package includes the default implementation of IMeterFactory and additional extension methods to easily register it with the Dependency Injection framework.</PackageDescription>
</PropertyGroup>

<ItemGroup>
<Compile Include="$(CommonPath)System\Diagnostics\DiagnosticsHelper.cs" Link="Common\System\Diagnostics\DiagnosticsHelper.cs" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="$(LibrariesProjectRoot)System.Diagnostics.DiagnosticSource\src\System.Diagnostics.DiagnosticSource.csproj" />
<ProjectReference Include="$(LibrariesProjectRoot)Microsoft.Extensions.Diagnostics.Abstractions\src\Microsoft.Extensions.Diagnostics.Abstractions.csproj" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
using Microsoft.DotNet.RemoteExecutor;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.DependencyInjection.Extensions;
using Microsoft.Extensions.Metrics;
using Microsoft.Extensions.Diagnostics.Metrics;
using System.Collections.Generic;
using System.Diagnostics.Metrics;
using System.Linq;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFrameworks>$(NetCoreAppCurrent);$(NetCoreAppPrevious);$(NetCoreAppMinimum);netstandard2.0;$(NetFrameworkMinimum)</TargetFrameworks>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
Expand Down Expand Up @@ -70,6 +71,7 @@ System.Diagnostics.DiagnosticSource</PackageDescription>
<Compile Include="System\Diagnostics\Metrics\UpDownCounter.cs" />

<Compile Include="$(CommonPath)System\HexConverter.cs" Link="Common\System\HexConverter.cs" />
<Compile Include="$(CommonPath)System\Diagnostics\DiagnosticsHelper.cs" Link="Common\System\Diagnostics\DiagnosticsHelper.cs" />

<None Include="DiagnosticSourceUsersGuide.md" />
<None Include="ActivityUserGuide.md" />
Expand Down
Loading