Skip to content

Use Microsoft.Extensions.Diagnostics #48403

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

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all 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
1 change: 1 addition & 0 deletions eng/Dependencies.props
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ and are generated based on the last package release.
<LatestPackageReference Include="Microsoft.Extensions.DependencyInjection" />
<LatestPackageReference Include="Microsoft.Extensions.DependencyModel" />
<LatestPackageReference Include="Microsoft.Extensions.DiagnosticAdapter" />
<LatestPackageReference Include="Microsoft.Extensions.Diagnostics" />
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't you need the Microsoft.Extensions.Diagnostics.Abstraction too? or will the dependency be automatically picked?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

<LatestPackageReference Include="Microsoft.Extensions.FileProviders.Abstractions" />
<LatestPackageReference Include="Microsoft.Extensions.FileProviders.Composite" />
<LatestPackageReference Include="Microsoft.Extensions.FileProviders.Physical" />
Expand Down
1 change: 1 addition & 0 deletions eng/SharedFramework.External.props
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
<ExternalAspNetCoreAppReference Include="Microsoft.Extensions.Configuration" Version="$(MicrosoftExtensionsConfigurationVersion)" />
<ExternalAspNetCoreAppReference Include="Microsoft.Extensions.DependencyInjection.Abstractions" Version="$(MicrosoftExtensionsDependencyInjectionAbstractionsVersion)" />
<ExternalAspNetCoreAppReference Include="Microsoft.Extensions.DependencyInjection" Version="$(MicrosoftExtensionsDependencyInjectionVersion)" />
<ExternalAspNetCoreAppReference Include="Microsoft.Extensions.Diagnostics" Version="$(MicrosoftExtensionsDiagnosticsVersion)" />
<ExternalAspNetCoreAppReference Include="Microsoft.Extensions.FileProviders.Abstractions" Version="$(MicrosoftExtensionsFileProvidersAbstractionsVersion)" />
<ExternalAspNetCoreAppReference Include="Microsoft.Extensions.FileProviders.Composite" Version="$(MicrosoftExtensionsFileProvidersCompositeVersion)" />
<ExternalAspNetCoreAppReference Include="Microsoft.Extensions.FileProviders.Physical" Version="$(MicrosoftExtensionsFileProvidersPhysicalVersion)" />
Expand Down
4 changes: 4 additions & 0 deletions eng/Version.Details.xml
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,10 @@
<Uri>https://github.com/dotnet/runtime</Uri>
<Sha>1e421670a6456d9c5b924b7ffea14cab8559a2e9</Sha>
</Dependency>
<Dependency Name="Microsoft.Extensions.Diagnostics" Version="8.0.0-preview.5.23273.1">
<Uri>https://github.com/dotnet/runtime</Uri>
<Sha>1e421670a6456d9c5b924b7ffea14cab8559a2e9</Sha>
</Dependency>
<Dependency Name="Microsoft.Extensions.FileProviders.Abstractions" Version="8.0.0-preview.5.23273.1">
<Uri>https://github.com/dotnet/runtime</Uri>
<Sha>1e421670a6456d9c5b924b7ffea14cab8559a2e9</Sha>
Expand Down
1 change: 1 addition & 0 deletions eng/Versions.props
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@
<MicrosoftExtensionsConfigurationXmlVersion>8.0.0-preview.5.23273.1</MicrosoftExtensionsConfigurationXmlVersion>
<MicrosoftExtensionsDependencyInjectionAbstractionsVersion>8.0.0-preview.5.23273.1</MicrosoftExtensionsDependencyInjectionAbstractionsVersion>
<MicrosoftExtensionsDependencyInjectionVersion>8.0.0-preview.5.23273.1</MicrosoftExtensionsDependencyInjectionVersion>
<MicrosoftExtensionsDiagnosticsVersion>8.0.0-preview.5.23273.1</MicrosoftExtensionsDiagnosticsVersion>
<MicrosoftExtensionsFileProvidersAbstractionsVersion>8.0.0-preview.5.23273.1</MicrosoftExtensionsFileProvidersAbstractionsVersion>
<MicrosoftExtensionsFileProvidersCompositeVersion>8.0.0-preview.5.23273.1</MicrosoftExtensionsFileProvidersCompositeVersion>
<MicrosoftExtensionsFileProvidersPhysicalVersion>8.0.0-preview.5.23273.1</MicrosoftExtensionsFileProvidersPhysicalVersion>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.DependencyInjection.Extensions;
using Microsoft.Extensions.Diagnostics.Metrics;
using Microsoft.Extensions.Hosting;

namespace Microsoft.AspNetCore.Hosting;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.DependencyInjection.Extensions;
using Microsoft.Extensions.Diagnostics.Metrics;
using Microsoft.Extensions.Hosting;

namespace Microsoft.AspNetCore.Hosting;
Expand Down
4 changes: 2 additions & 2 deletions src/Hosting/Hosting/src/Internal/HostingMetrics.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
using System.Diagnostics;
using System.Diagnostics.Metrics;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Metrics;
using Microsoft.Extensions.Diagnostics.Metrics;

namespace Microsoft.AspNetCore.Hosting;

Expand All @@ -19,7 +19,7 @@ internal sealed class HostingMetrics : IDisposable

public HostingMetrics(IMeterFactory meterFactory)
{
_meter = meterFactory.CreateMeter(MeterName);
_meter = meterFactory.Create(MeterName);

_currentRequestsCounter = _meter.CreateUpDownCounter<long>(
"current-requests",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
<Reference Include="Microsoft.Extensions.Configuration.EnvironmentVariables" />
<Reference Include="Microsoft.Extensions.Configuration.FileExtensions" />
<Reference Include="Microsoft.Extensions.Configuration" />
<Reference Include="Microsoft.Extensions.Diagnostics" />
<Reference Include="Microsoft.Extensions.DependencyInjection" />
<Reference Include="Microsoft.Extensions.FileProviders.Physical" />
<Reference Include="Microsoft.Extensions.FileProviders.Composite" />
Expand Down
1 change: 1 addition & 0 deletions src/Hosting/Hosting/src/WebHostBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.DependencyInjection.Extensions;
using Microsoft.Extensions.Diagnostics.Metrics;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;

Expand Down
14 changes: 7 additions & 7 deletions src/Hosting/Hosting/test/HostingApplicationDiagnosticsTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,15 @@
// The .NET Foundation licenses this file to you under the MIT license.

using System.Diagnostics;
using System.Diagnostics.Metrics;
using System.Diagnostics.Tracing;
using System.Reflection;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Http.Features;
using Microsoft.AspNetCore.Internal;
using Microsoft.AspNetCore.Testing;
using Microsoft.Extensions.Diagnostics.Metrics;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Metrics;
using Moq;

namespace Microsoft.AspNetCore.Hosting.Tests;
Expand Down Expand Up @@ -44,17 +46,15 @@ public async Task EventCountersAndMetricsValues()
});

var testMeterFactory1 = new TestMeterFactory();
var testMeterRegister1 = new TestMeterRegistry(testMeterFactory1.Meters);
var testMeterFactory2 = new TestMeterFactory();
var testMeterRegister2 = new TestMeterRegistry(testMeterFactory2.Meters);

var hostingApplication1 = CreateApplication(out var features1, eventSource: hostingEventSource, meterFactory: testMeterFactory1);
var hostingApplication2 = CreateApplication(out var features2, eventSource: hostingEventSource, meterFactory: testMeterFactory2);

using var currentRequestsRecorder1 = new InstrumentRecorder<long>(testMeterRegister1, HostingMetrics.MeterName, "current-requests");
using var currentRequestsRecorder2 = new InstrumentRecorder<long>(testMeterRegister2, HostingMetrics.MeterName, "current-requests");
using var requestDurationRecorder1 = new InstrumentRecorder<double>(testMeterRegister1, HostingMetrics.MeterName, "request-duration");
using var requestDurationRecorder2 = new InstrumentRecorder<double>(testMeterRegister2, HostingMetrics.MeterName, "request-duration");
using var currentRequestsRecorder1 = new InstrumentRecorder<long>(testMeterFactory1, HostingMetrics.MeterName, "current-requests");
using var currentRequestsRecorder2 = new InstrumentRecorder<long>(testMeterFactory2, HostingMetrics.MeterName, "current-requests");
using var requestDurationRecorder1 = new InstrumentRecorder<double>(testMeterFactory1, HostingMetrics.MeterName, "request-duration");
using var requestDurationRecorder2 = new InstrumentRecorder<double>(testMeterFactory2, HostingMetrics.MeterName, "request-duration");

// Act/Assert 1
var context1 = hostingApplication1.CreateContext(features1);
Expand Down
5 changes: 1 addition & 4 deletions src/Hosting/Hosting/test/HostingApplicationTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,14 @@
// The .NET Foundation licenses this file to you under the MIT license.

using System.Collections;
using System.Collections.ObjectModel;
using System.Diagnostics;
using System.Diagnostics.Metrics;
using Microsoft.AspNetCore.Hosting.Fakes;
using Microsoft.AspNetCore.Hosting.Server.Abstractions;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Http.Features;
using Microsoft.AspNetCore.Testing;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Diagnostics.Metrics;
using Microsoft.Extensions.Logging.Abstractions;
using Microsoft.Extensions.Metrics;
using Moq;
using static Microsoft.AspNetCore.Hosting.HostingApplication;

Expand Down
17 changes: 7 additions & 10 deletions src/Hosting/Hosting/test/HostingMetricsTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@
using Microsoft.AspNetCore.Internal;
using Microsoft.AspNetCore.Testing;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Diagnostics.Metrics;
using Microsoft.Extensions.Logging.Abstractions;
using Microsoft.Extensions.Metrics;

namespace Microsoft.AspNetCore.Hosting.Tests;

Expand All @@ -21,13 +21,12 @@ public void MultipleRequests()
{
// Arrange
var meterFactory = new TestMeterFactory();
var meterRegistry = new TestMeterRegistry(meterFactory.Meters);
var hostingApplication = CreateApplication(meterFactory: meterFactory);
var httpContext = new DefaultHttpContext();
var meter = meterFactory.Meters.Single();

using var requestDurationRecorder = new InstrumentRecorder<double>(meterRegistry, HostingMetrics.MeterName, "request-duration");
using var currentRequestsRecorder = new InstrumentRecorder<long>(meterRegistry, HostingMetrics.MeterName, "current-requests");
using var requestDurationRecorder = new InstrumentRecorder<double>(meterFactory, HostingMetrics.MeterName, "request-duration");
using var currentRequestsRecorder = new InstrumentRecorder<long>(meterFactory, HostingMetrics.MeterName, "current-requests");

// Act/Assert
Assert.Equal(HostingMetrics.MeterName, meter.Name);
Expand Down Expand Up @@ -111,7 +110,6 @@ public async Task StartListeningDuringRequest_NotMeasured()
// Arrange
var syncPoint = new SyncPoint();
var meterFactory = new TestMeterFactory();
var meterRegistry = new TestMeterRegistry(meterFactory.Meters);
var hostingApplication = CreateApplication(meterFactory: meterFactory, requestDelegate: async ctx =>
{
await syncPoint.WaitToContinue();
Expand All @@ -130,8 +128,8 @@ public async Task StartListeningDuringRequest_NotMeasured()

await syncPoint.WaitForSyncPoint().DefaultTimeout();

using var requestDurationRecorder = new InstrumentRecorder<double>(meterRegistry, HostingMetrics.MeterName, "request-duration");
using var currentRequestsRecorder = new InstrumentRecorder<long>(meterRegistry, HostingMetrics.MeterName, "current-requests");
using var requestDurationRecorder = new InstrumentRecorder<double>(meterFactory, HostingMetrics.MeterName, "request-duration");
using var currentRequestsRecorder = new InstrumentRecorder<long>(meterFactory, HostingMetrics.MeterName, "current-requests");
context1.HttpContext.Response.StatusCode = StatusCodes.Status200OK;

syncPoint.Continue();
Expand All @@ -148,13 +146,12 @@ public void IHttpMetricsTagsFeatureNotUsedFromFeatureCollection()
{
// Arrange
var meterFactory = new TestMeterFactory();
var meterRegistry = new TestMeterRegistry(meterFactory.Meters);
var hostingApplication = CreateApplication(meterFactory: meterFactory);
var httpContext = new DefaultHttpContext();
var meter = meterFactory.Meters.Single();

using var requestDurationRecorder = new InstrumentRecorder<double>(meterRegistry, HostingMetrics.MeterName, "request-duration");
using var currentRequestsRecorder = new InstrumentRecorder<long>(meterRegistry, HostingMetrics.MeterName, "current-requests");
using var requestDurationRecorder = new InstrumentRecorder<double>(meterFactory, HostingMetrics.MeterName, "request-duration");
using var currentRequestsRecorder = new InstrumentRecorder<long>(meterFactory, HostingMetrics.MeterName, "current-requests");

// Act/Assert
Assert.Equal(HostingMetrics.MeterName, meter.Name);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
<Compile Include="$(SharedSourceRoot)EventSource.Testing\TestEventListener.cs" />
<Compile Include="$(SharedSourceRoot)EventSource.Testing\TestCounterListener.cs" />
<Compile Include="$(SharedSourceRoot)SyncPoint\SyncPoint.cs" />
<Compile Include="$(SharedSourceRoot)Metrics\TestMeterFactory.cs" LinkBase="shared" />
<Content Include="testroot\**\*" CopyToOutputDirectory="PreserveNewest" CopyToPublishDirectory="PreserveNewest" />
<Content Include="Microsoft.AspNetCore.Hosting.StaticWebAssets.xml" CopyToOutputDirectory="PreserveNewest" />
</ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,11 +54,6 @@ Microsoft.AspNetCore.Http.HttpResponse</Description>
</Compile>
</ItemGroup>

<!-- Temporary hack to make prototype Metrics DI integration types available -->
<!-- TODO: Remove when Metrics DI intergration package is available https://github.com/dotnet/aspnetcore/issues/47618 -->
<ItemGroup>
<Compile Include="$(SharedSourceRoot)Metrics\**\*.cs" LinkBase="Metrics" />
</ItemGroup>
<ItemGroup>
<InternalsVisibleTo Include="Microsoft.AspNetCore.Hosting" />
<InternalsVisibleTo Include="Microsoft.AspNetCore.Hosting.Tests" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// The .NET Foundation licenses this file to you under the MIT license.

using System.Diagnostics;
using System.Diagnostics.Metrics;
using System.Net;
using System.Net.Http;
using System.Net.Http.Headers;
Expand All @@ -14,8 +15,8 @@
using Microsoft.AspNetCore.TestHost;
using Microsoft.AspNetCore.Testing;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Diagnostics.Metrics;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Metrics;

namespace Microsoft.AspNetCore.Diagnostics;

Expand Down Expand Up @@ -540,9 +541,9 @@ public async Task UnhandledError_ExceptionNameTagAdded()
var tcs = new TaskCompletionSource(TaskCreationOptions.RunContinuationsAsynchronously);

var meterFactory = new TestMeterFactory();
var meterRegistry = new TestMeterRegistry(meterFactory.Meters);
var instrumentRecorder = new InstrumentRecorder<double>(meterRegistry, "Microsoft.AspNetCore.Hosting", "request-duration");
instrumentRecorder.Register(m =>
using var instrumentRecorder = new InstrumentRecorder<double>(meterFactory, "Microsoft.AspNetCore.Hosting", "request-duration");
using var measurementReporter = new MeasurementReporter<double>(meterFactory, "Microsoft.AspNetCore.Hosting", "request-duration");
measurementReporter.Register(m =>
{
tcs.SetResult();
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,18 @@
// The .NET Foundation licenses this file to you under the MIT license.

using System.Diagnostics;
using System.Diagnostics.Metrics;
using System.Net;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.TestHost;
using Microsoft.AspNetCore.Testing;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Diagnostics.Metrics;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Logging.Testing;
using Microsoft.Extensions.Metrics;

namespace Microsoft.AspNetCore.Diagnostics;

Expand Down Expand Up @@ -917,9 +918,9 @@ public async Task UnhandledError_ExceptionNameTagAdded()
var tcs = new TaskCompletionSource(TaskCreationOptions.RunContinuationsAsynchronously);

var meterFactory = new TestMeterFactory();
var meterRegistry = new TestMeterRegistry(meterFactory.Meters);
var instrumentRecorder = new InstrumentRecorder<double>(meterRegistry, "Microsoft.AspNetCore.Hosting", "request-duration");
instrumentRecorder.Register(m =>
using var instrumentRecorder = new InstrumentRecorder<double>(meterFactory, "Microsoft.AspNetCore.Hosting", "request-duration");
using var measurementReporter = new MeasurementReporter<double>(meterFactory, "Microsoft.AspNetCore.Hosting", "request-duration");
measurementReporter.Register(m =>
{
tcs.SetResult();
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@

<ItemGroup>
<ProjectReference Include="..\testassets\ClassLibraryWithPortablePdbs\ClassLibraryWithPortablePdbs.csproj" />

<Compile Include="$(SharedSourceRoot)Metrics\TestMeterFactory.cs" LinkBase="shared" />
</ItemGroup>

<ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
<Reference Include="Microsoft.AspNetCore.Http.Abstractions" />
<Reference Include="Microsoft.Extensions.Logging.Abstractions" />
<Reference Include="Microsoft.Extensions.Options" />
<Reference Include="Microsoft.Extensions.Diagnostics" />
<Reference Include="System.Threading.RateLimiting" />

<Compile Include="$(SharedSourceRoot)ValueStopwatch\*.cs" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

using Microsoft.AspNetCore.RateLimiting;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Diagnostics.Metrics;

namespace Microsoft.AspNetCore.Builder;

Expand Down
4 changes: 2 additions & 2 deletions src/Middleware/RateLimiting/src/RateLimitingMetrics.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
using System.Diagnostics;
using System.Diagnostics.Metrics;
using System.Runtime.CompilerServices;
using Microsoft.Extensions.Metrics;
using Microsoft.Extensions.Diagnostics.Metrics;

namespace Microsoft.AspNetCore.RateLimiting;

Expand All @@ -21,7 +21,7 @@ internal sealed class RateLimitingMetrics : IDisposable

public RateLimitingMetrics(IMeterFactory meterFactory)
{
_meter = meterFactory.CreateMeter(MeterName);
_meter = meterFactory.Create(MeterName);

_currentLeasedRequestsCounter = _meter.CreateUpDownCounter<long>(
"current-leased-requests",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
<Reference Include="Microsoft.AspNetCore.RateLimiting" />
<Reference Include="Microsoft.AspNetCore.Routing" />

<Compile Include="$(SharedSourceRoot)SyncPoint\SyncPoint.cs" Link="Internal\SyncPoint.cs" />
<Compile Include="$(SharedSourceRoot)Metrics\TestMeterFactory.cs" LinkBase="shared" />
<Compile Include="$(SharedSourceRoot)SyncPoint\SyncPoint.cs" LinkBase="shared" />
</ItemGroup>
</Project>
Loading