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

Remove generic Meter<T> #4342

Merged
merged 21 commits into from
Sep 27, 2023
Merged
Show file tree
Hide file tree
Changes from 17 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 @@ -8,7 +8,6 @@
using System.Threading.Tasks;
using BenchmarkDotNet.Attributes;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Diagnostics.Metrics;
using Microsoft.Extensions.Resilience.FaultInjection;

namespace Microsoft.Extensions.Http.Resilience.FaultInjection.PerformanceTests;
Expand All @@ -18,7 +17,7 @@ public class FaultInjectionRequestBenchmarks
private const string HttpClientIdentifier = "HttpClientClass";
private static readonly Uri _defaultUrl = new("https://www.google.ca/");
private ServiceProvider _serviceProvider = null!;
private System.Net.Http.HttpClient _client = null!;
private HttpClient _client = null!;

[GlobalSetup]
public void GlobalSetup()
Expand All @@ -34,9 +33,7 @@ public void GlobalSetup()
option.ChaosPolicyOptionsGroups = new Dictionary<string, ChaosPolicyOptionsGroup>();
});

services
.RegisterMetrics()
.AddHttpClientFaultInjection(action);
services.AddHttpClientFaultInjection(action);
}

services.AddHttpClient(HttpClientIdentifier);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
using Microsoft.Extensions.Compliance.Testing;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.DependencyInjection.Extensions;
using Microsoft.Extensions.Diagnostics.Metrics;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Logging.Abstractions;

Expand Down Expand Up @@ -39,7 +38,7 @@ public static ServiceProvider InitializeServiceProvider(params HedgingClientType
{
var services = new ServiceCollection();
services
.RegisterMetrics()
.AddMetrics()
.AddSingleton<IRedactorProvider>(NullRedactorProvider.Instance)
.AddTransient<NoRemoteCallHandler>()
.AddHttpClient(StandardClient, client => client.Timeout = Timeout.InfiniteTimeSpan)
Expand Down
4 changes: 4 additions & 0 deletions eng/Version.Details.xml
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,10 @@
<Uri>https://github.com/dotnet/runtime</Uri>
<Sha>9cdbc87dadbf358206f20f17fed005cdcb253452</Sha>
</Dependency>
<Dependency Name="Microsoft.Extensions.Diagnostics" Version="8.0.0-rc.2.23464.16" CoherentParentDependency="Microsoft.AspNetCore.App.Runtime.win-x64">
<Uri>https://github.com/dotnet/runtime</Uri>
<Sha>9cdbc87dadbf358206f20f17fed005cdcb253452</Sha>
</Dependency>
<Dependency Name="Microsoft.Extensions.Hosting.Abstractions" Version="8.0.0-rc.2.23464.16" CoherentParentDependency="Microsoft.AspNetCore.App.Runtime.win-x64">
<Uri>https://github.com/dotnet/runtime</Uri>
<Sha>9cdbc87dadbf358206f20f17fed005cdcb253452</Sha>
Expand Down
3 changes: 2 additions & 1 deletion eng/Versions.props
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
DO NOT UPDATE THESE MANUALLY. Use the `darc` command line tool to update this file so it stays in sync with
Version.Details.xml.

See https://github.com/dotnet/arcade/blob/master/Documentation/Darc.md for instructions on using darc.
See https://github.com/dotnet/arcade/blob/main/Documentation/Darc.md for instructions on using darc.

-->
<PropertyGroup Label="Automated">
Expand All @@ -38,6 +38,7 @@
<MicrosoftExtensionsConfigurationVersion>8.0.0-rc.2.23464.16</MicrosoftExtensionsConfigurationVersion>
<MicrosoftExtensionsDependencyInjectionAbstractionsVersion>8.0.0-rc.2.23464.16</MicrosoftExtensionsDependencyInjectionAbstractionsVersion>
<MicrosoftExtensionsDependencyInjectionVersion>8.0.0-rc.2.23464.16</MicrosoftExtensionsDependencyInjectionVersion>
<MicrosoftExtensionsDiagnosticsVersion>8.0.0-rc.2.23464.16</MicrosoftExtensionsDiagnosticsVersion>
<MicrosoftExtensionsHostingAbstractionsVersion>8.0.0-rc.2.23464.16</MicrosoftExtensionsHostingAbstractionsVersion>
<MicrosoftExtensionsHostingVersion>8.0.0-rc.2.23464.16</MicrosoftExtensionsHostingVersion>
<MicrosoftExtensionsHttpVersion>8.0.0-rc.2.23464.16</MicrosoftExtensionsHttpVersion>
Expand Down
1 change: 1 addition & 0 deletions eng/packages/General.props
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
<PackageVersion Include="Microsoft.Extensions.DependencyInjection.Abstractions" Version="$(MicrosoftExtensionsDependencyInjectionAbstractionsVersion)" />
<PackageVersion Include="Microsoft.Extensions.DependencyInjection" Version="$(MicrosoftExtensionsDependencyInjectionVersion)" />
<PackageVersion Include="Microsoft.Extensions.DiagnosticAdapter" Version="3.1.32" />
<PackageVersion Include="Microsoft.Extensions.Diagnostics" Version="$(MicrosoftExtensionsDiagnosticsVersion)" />
<PackageVersion Include="Microsoft.Extensions.Diagnostics.HealthChecks.Abstractions" Version="$(MicrosoftExtensionsDiagnosticsHealthChecksAbstractionsVersion)" />
<PackageVersion Include="Microsoft.Extensions.Diagnostics.HealthChecks" Version="$(MicrosoftExtensionsDiagnosticsHealthChecksVersion)" />
<PackageVersion Include="Microsoft.Extensions.Features" Version="$(MicrosoftExtensionsFeaturesVersion)" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
using System;
using System.Diagnostics.CodeAnalysis;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Diagnostics.Metrics;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.ObjectPool;
using Microsoft.Shared.Diagnostics;
Expand All @@ -18,8 +17,7 @@ public sealed partial class HeaderParsingFeature
{
private readonly IHeaderRegistry _registry;
private readonly ILogger _logger;
private readonly ParsingErrorCounter _parsingErrorCounter;
private readonly CacheAccessCounter _cacheAccessCounter;
private readonly HeaderParsingMetrics _metrics;

// This is a heterogeneous array of Box<T> instances. These boxes let us keep different Ts
// in a single array, while preventing boxing of the T. The boxes are allocated up front
Expand All @@ -28,12 +26,11 @@ public sealed partial class HeaderParsingFeature

internal HttpContext? Context { get; set; }

internal HeaderParsingFeature(IHeaderRegistry registry, ILogger<HeaderParsingFeature> logger, Meter<HeaderParsingFeature> meter)
internal HeaderParsingFeature(IHeaderRegistry registry, ILogger<HeaderParsingFeature> logger, HeaderParsingMetrics metrics)
{
_logger = logger;
_metrics = metrics;
_registry = registry;
_parsingErrorCounter = Metric.CreateParsingErrorCounter(meter);
_cacheAccessCounter = Metric.CreateCacheAccessCounter(meter);
}

/// <summary>
Expand Down Expand Up @@ -89,12 +86,17 @@ private void Reset()
internal sealed class PoolHelper : IDisposable
{
public HeaderParsingFeature Feature { get; }

private readonly ObjectPool<PoolHelper> _pool;

public PoolHelper(ObjectPool<PoolHelper> pool, IHeaderRegistry registry, ILogger<HeaderParsingFeature> logger, Meter<HeaderParsingFeature> meter)
public PoolHelper(
ObjectPool<PoolHelper> pool,
IHeaderRegistry registry,
ILogger<HeaderParsingFeature> logger,
HeaderParsingMetrics metrics)
{
_pool = pool;
Feature = new HeaderParsingFeature(registry, logger, meter);
Feature = new HeaderParsingFeature(registry, logger, metrics);
}

public void Dispose()
Expand Down Expand Up @@ -141,15 +143,15 @@ public bool Process(HeaderParsingFeature feature, HeaderKey<T> header, out T? va
var o = header.GetCachedValue(values);
if (o != null)
{
feature._cacheAccessCounter.Add(1, header.Name, "Hit");
feature._metrics.CacheAccessCounter.Add(1, header.Name, "Hit");
var b = (Box<T>)o;
b.CopyTo(this);
value = _value;
result = (ParsingResult)_state;
return result == ParsingResult.Success;
}

feature._cacheAccessCounter.Add(1, header.Name, "Miss");
feature._metrics.CacheAccessCounter.Add(1, header.Name, "Miss");
}

if (header.TryParse(values, out _value, out var error))
Expand All @@ -167,7 +169,7 @@ public bool Process(HeaderParsingFeature feature, HeaderKey<T> header, out T? va
{
_state = BoxState.Error;
feature.LogParsingError(header.Name, error!);
feature._parsingErrorCounter.Add(1, header.Name, error);
feature._metrics.ParsingErrorCounter.Add(1, header.Name, error);
}
}
else if (header.HasDefaultValue)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System.Diagnostics.Metrics;

namespace Microsoft.AspNetCore.HeaderParsing;

internal sealed class HeaderParsingMetrics
{
private const string MeterName = "Microsoft.AspNetCore.HeaderParsing";

public HeaderParsingMetrics(IMeterFactory meterFactory)
{
#pragma warning disable CA2000 // Dispose objects before losing scope
// 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(MeterName);
#pragma warning restore CA2000 // Dispose objects before losing scope

ParsingErrorCounter = Metric.CreateParsingErrorCounter(meter);
CacheAccessCounter = Metric.CreateCacheAccessCounter(meter);
}

public ParsingErrorCounter ParsingErrorCounter { get; }

public CacheAccessCounter CacheAccessCounter { get; }
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
using Microsoft.AspNetCore.HeaderParsing;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Diagnostics.Metrics;
using Microsoft.Extensions.ObjectPool;
using Microsoft.Shared.Diagnostics;

Expand All @@ -32,7 +31,8 @@ public static IServiceCollection AddHeaderParsing(this IServiceCollection servic
.AddSingleton<IHeaderRegistry, HeaderRegistry>()
.AddScoped(provider => provider.GetRequiredService<ObjectPool<HeaderParsingFeature.PoolHelper>>().Get())
.AddScoped(provider => provider.GetRequiredService<HeaderParsingFeature.PoolHelper>().Feature)
.RegisterMetrics();
.AddSingleton<HeaderParsingMetrics>()
.AddMetrics();
}

return services;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,16 +26,15 @@
</ItemGroup>

<ItemGroup>
<FrameworkReference Include="Microsoft.AspNetCore.App" />
<PackageReference Include="Microsoft.Extensions.Diagnostics" />
</ItemGroup>

<ItemGroup>
<InternalsVisibleToTest Include="$(AssemblyName).Tests" />
<InternalsVisibleToDynamicProxyGenAssembly2 Include="*" />
<FrameworkReference Include="Microsoft.AspNetCore.App" />
</ItemGroup>

<ItemGroup>
<InternalsVisibleToDynamicProxyGenAssembly2 Remove="HeaderParsingHttpRequestExtensions.cs" />
<InternalsVisibleToDynamicProxyGenAssembly2 Remove="HeaderParsingServiceCollectionExtensions.cs" />
<InternalsVisibleToTest Include="$(AssemblyName).Tests" />
<InternalsVisibleToDynamicProxyGenAssembly2 Include="*" />
</ItemGroup>
</Project>

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -746,26 +746,6 @@
}
]
},
{
"Type": "class Microsoft.Extensions.Diagnostics.Metrics.Meter<TMeterName> : System.Diagnostics.Metrics.Meter",
"Stage": "Experimental",
"Methods": [
{
"Member": "Microsoft.Extensions.Diagnostics.Metrics.Meter<TMeterName>.Meter();",
"Stage": "Experimental"
}
]
},
{
"Type": "static class Microsoft.Extensions.Diagnostics.Metrics.MetricsExtensions",
"Stage": "Stable",
"Methods": [
{
"Member": "static Microsoft.Extensions.DependencyInjection.IServiceCollection Microsoft.Extensions.Diagnostics.Metrics.MetricsExtensions.RegisterMetrics(this Microsoft.Extensions.DependencyInjection.IServiceCollection services);",
"Stage": "Experimental"
}
]
},
{
"Type": "static class Microsoft.Extensions.DependencyInjection.NullLatencyContextServiceCollectionExtensions",
"Stage": "Stable",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Diagnostics.HealthChecks;
using Microsoft.Extensions.Diagnostics.Metrics;
using Microsoft.Shared.Diagnostics;

namespace Microsoft.Extensions.DependencyInjection;
Expand All @@ -20,7 +19,8 @@ public static partial class CommonHealthChecksExtensions
/// <exception cref="ArgumentNullException"><paramref name="services" /> is <see langword="null" />.</exception>
public static IServiceCollection AddTelemetryHealthCheckPublisher(this IServiceCollection services)
=> Throw.IfNull(services)
.RegisterMetrics()
.AddMetrics()
.AddSingleton<HealthCheckMetrics>()
.AddSingleton<IHealthCheckPublisher, TelemetryHealthCheckPublisher>();

/// <summary>
Expand All @@ -33,7 +33,8 @@ public static IServiceCollection AddTelemetryHealthCheckPublisher(this IServiceC
public static IServiceCollection AddTelemetryHealthCheckPublisher(this IServiceCollection services, IConfigurationSection section)
=> Throw.IfNull(services)
.Configure<TelemetryHealthCheckPublisherOptions>(Throw.IfNull(section))
.RegisterMetrics()
.AddMetrics()
.AddSingleton<HealthCheckMetrics>()
.AddSingleton<IHealthCheckPublisher, TelemetryHealthCheckPublisher>();

/// <summary>
Expand All @@ -46,6 +47,7 @@ public static IServiceCollection AddTelemetryHealthCheckPublisher(this IServiceC
public static IServiceCollection AddTelemetryHealthCheckPublisher(this IServiceCollection services, Action<TelemetryHealthCheckPublisherOptions> configure)
=> Throw.IfNull(services)
.Configure(Throw.IfNull(configure))
.RegisterMetrics()
.AddMetrics()
.AddSingleton<HealthCheckMetrics>()
.AddSingleton<IHealthCheckPublisher, TelemetryHealthCheckPublisher>();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System.Diagnostics.Metrics;

namespace Microsoft.Extensions.Diagnostics.HealthChecks;

internal sealed class HealthCheckMetrics
{
public HealthCheckMetrics(IMeterFactory meterFactory)
{
#pragma warning disable CA2000 // Dispose objects before losing scope
// 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("Microsoft.Extensions.Diagnostics.HealthChecks");
#pragma warning restore CA2000 // Dispose objects before losing scope

HealthCheckReportCounter = Metric.CreateHealthCheckReportCounter(meter);
UnhealthyHealthCheckCounter = Metric.CreateUnhealthyHealthCheckCounter(meter);
}

public HealthCheckReportCounter HealthCheckReportCounter { get; }

public UnhealthyHealthCheckCounter UnhealthyHealthCheckCounter { get; }
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
</ItemGroup>

<ItemGroup>
<PackageReference Include="Microsoft.Extensions.Diagnostics" />
<PackageReference Include="Microsoft.Extensions.Diagnostics.HealthChecks" />
<XPackageReference Include="Microsoft.Extensions.Hosting.Abstractions" />
<PackageReference Include="Microsoft.Extensions.Options.ConfigurationExtensions" />
Expand Down
Loading
Loading