diff --git a/src/Hosting/Hosting/src/GenericHost/GenericWebHostBuilder.cs b/src/Hosting/Hosting/src/GenericHost/GenericWebHostBuilder.cs index ddb35bf8495a..76a19460037f 100644 --- a/src/Hosting/Hosting/src/GenericHost/GenericWebHostBuilder.cs +++ b/src/Hosting/Hosting/src/GenericHost/GenericWebHostBuilder.cs @@ -86,6 +86,7 @@ public GenericWebHostBuilder(IHostBuilder builder, WebHostBuilderOptions options // We need to flow this differently services.TryAddSingleton(sp => new DiagnosticListener("Microsoft.AspNetCore")); services.TryAddSingleton(sp => sp.GetRequiredService()); + services.TryAddSingleton(sp => new ActivitySource("Microsoft.AspNetCore")); services.TryAddSingleton(); services.TryAddScoped(); diff --git a/src/Hosting/Hosting/src/GenericHost/GenericWebHostedService.cs b/src/Hosting/Hosting/src/GenericHost/GenericWebHostedService.cs index 2bd1d1fcf43f..ef270b596a98 100644 --- a/src/Hosting/Hosting/src/GenericHost/GenericWebHostedService.cs +++ b/src/Hosting/Hosting/src/GenericHost/GenericWebHostedService.cs @@ -24,6 +24,7 @@ public GenericWebHostService(IOptions options, IServer server, ILoggerFactory loggerFactory, DiagnosticListener diagnosticListener, + ActivitySource activitySource, IHttpContextFactory httpContextFactory, IApplicationBuilderFactory applicationBuilderFactory, IEnumerable startupFilters, @@ -35,6 +36,7 @@ public GenericWebHostService(IOptions options, Logger = loggerFactory.CreateLogger("Microsoft.AspNetCore.Hosting.Diagnostics"); LifetimeLogger = loggerFactory.CreateLogger("Microsoft.Hosting.Lifetime"); DiagnosticListener = diagnosticListener; + ActivitySource = activitySource; HttpContextFactory = httpContextFactory; ApplicationBuilderFactory = applicationBuilderFactory; StartupFilters = startupFilters; @@ -48,6 +50,7 @@ public GenericWebHostService(IOptions options, // Only for high level lifetime events public ILogger LifetimeLogger { get; } public DiagnosticListener DiagnosticListener { get; } + public ActivitySource ActivitySource { get; } public IHttpContextFactory HttpContextFactory { get; } public IApplicationBuilderFactory ApplicationBuilderFactory { get; } public IEnumerable StartupFilters { get; } @@ -111,7 +114,7 @@ public async Task StartAsync(CancellationToken cancellationToken) application = ErrorPageBuilder.BuildErrorPageApplication(HostingEnvironment.ContentRootFileProvider, Logger, showDetailedErrors, ex); } - var httpApplication = new HostingApplication(application, Logger, DiagnosticListener, HttpContextFactory); + var httpApplication = new HostingApplication(application, Logger, DiagnosticListener, ActivitySource, HttpContextFactory); await Server.StartAsync(httpApplication, cancellationToken); diff --git a/src/Hosting/Hosting/src/Internal/HostingApplication.cs b/src/Hosting/Hosting/src/Internal/HostingApplication.cs index b186a9227c5a..cafd0c92f2e1 100644 --- a/src/Hosting/Hosting/src/Internal/HostingApplication.cs +++ b/src/Hosting/Hosting/src/Internal/HostingApplication.cs @@ -23,10 +23,11 @@ public HostingApplication( RequestDelegate application, ILogger logger, DiagnosticListener diagnosticSource, + ActivitySource activitySource, IHttpContextFactory httpContextFactory) { _application = application; - _diagnostics = new HostingApplicationDiagnostics(logger, diagnosticSource); + _diagnostics = new HostingApplicationDiagnostics(logger, diagnosticSource, activitySource); if (httpContextFactory is DefaultHttpContextFactory factory) { _defaultHttpContextFactory = factory; diff --git a/src/Hosting/Hosting/src/Internal/HostingApplicationDiagnostics.cs b/src/Hosting/Hosting/src/Internal/HostingApplicationDiagnostics.cs index 5b4f058d6515..373bf65542fa 100644 --- a/src/Hosting/Hosting/src/Internal/HostingApplicationDiagnostics.cs +++ b/src/Hosting/Hosting/src/Internal/HostingApplicationDiagnostics.cs @@ -24,16 +24,15 @@ internal class HostingApplicationDiagnostics private const string DeprecatedDiagnosticsEndRequestKey = "Microsoft.AspNetCore.Hosting.EndRequest"; private const string DiagnosticsUnhandledExceptionKey = "Microsoft.AspNetCore.Hosting.UnhandledException"; - private const string ActivitySourceName = "Microsoft.AspNetCore.Hosting"; - private static readonly ActivitySource _activitySource = new ActivitySource(ActivitySourceName); - + private readonly ActivitySource _activitySource; private readonly DiagnosticListener _diagnosticListener; private readonly ILogger _logger; - public HostingApplicationDiagnostics(ILogger logger, DiagnosticListener diagnosticListener) + public HostingApplicationDiagnostics(ILogger logger, DiagnosticListener diagnosticListener, ActivitySource activitySource) { _logger = logger; _diagnosticListener = diagnosticListener; + _activitySource = activitySource; } [MethodImpl(MethodImplOptions.AggressiveInlining)] diff --git a/src/Hosting/Hosting/src/Internal/WebHost.cs b/src/Hosting/Hosting/src/Internal/WebHost.cs index e20b071d6cd0..45ea00c96090 100644 --- a/src/Hosting/Hosting/src/Internal/WebHost.cs +++ b/src/Hosting/Hosting/src/Internal/WebHost.cs @@ -155,8 +155,9 @@ public virtual async Task StartAsync(CancellationToken cancellationToken = defau await _hostedServiceExecutor.StartAsync(cancellationToken).ConfigureAwait(false); var diagnosticSource = _applicationServices.GetRequiredService(); + var activitySource = _applicationServices.GetRequiredService(); var httpContextFactory = _applicationServices.GetRequiredService(); - var hostingApp = new HostingApplication(application, _logger, diagnosticSource, httpContextFactory); + var hostingApp = new HostingApplication(application, _logger, diagnosticSource, activitySource, httpContextFactory); await Server.StartAsync(hostingApp, cancellationToken).ConfigureAwait(false); _startedServer = true; diff --git a/src/Hosting/Hosting/src/WebHostBuilder.cs b/src/Hosting/Hosting/src/WebHostBuilder.cs index 0ae151dd83f6..ac72077eeefb 100644 --- a/src/Hosting/Hosting/src/WebHostBuilder.cs +++ b/src/Hosting/Hosting/src/WebHostBuilder.cs @@ -292,6 +292,7 @@ private IServiceCollection BuildCommonServices(out AggregateException? hostingSt services.TryAddSingleton(sp => new DiagnosticListener("Microsoft.AspNetCore")); services.TryAddSingleton(sp => sp.GetRequiredService()); + services.TryAddSingleton(sp => new ActivitySource("Microsoft.AspNetCore")); services.AddTransient(); services.AddTransient(); @@ -346,6 +347,9 @@ private void AddApplicationServices(IServiceCollection services, IServiceProvide var listener = hostingServiceProvider.GetService(); services.Replace(ServiceDescriptor.Singleton(typeof(DiagnosticListener), listener!)); services.Replace(ServiceDescriptor.Singleton(typeof(DiagnosticSource), listener!)); + + var activitySource = hostingServiceProvider.GetService(); + services.Replace(ServiceDescriptor.Singleton(typeof(ActivitySource), activitySource!)); } private string ResolveContentRootPath(string contentRootPath, string basePath) diff --git a/src/Hosting/Hosting/test/HostingApplicationDiagnosticsTests.cs b/src/Hosting/Hosting/test/HostingApplicationDiagnosticsTests.cs index d255265e1e96..364d8a3e4af2 100644 --- a/src/Hosting/Hosting/test/HostingApplicationDiagnosticsTests.cs +++ b/src/Hosting/Hosting/test/HostingApplicationDiagnosticsTests.cs @@ -390,7 +390,7 @@ public void ActivityBaggagePreservesItemsOrder() hostingApplication.CreateContext(features); Assert.Equal("Microsoft.AspNetCore.Hosting.HttpRequestIn", Activity.Current.OperationName); - var expectedBaggage = new [] + var expectedBaggage = new[] { KeyValuePair.Create("Key1","value1"), KeyValuePair.Create("Key2","value2"), @@ -493,17 +493,18 @@ public void ActivityOnImportHookIsCalled() Assert.True(Activity.Current.Recorded); } - [Fact(Skip = "https://github.com/dotnet/aspnetcore/issues/30582")] + [Fact] public void ActivityListenersAreCalled() { var hostingApplication = CreateApplication(out var features); + var parentSpanId = ""; using var listener = new ActivityListener { ShouldListenTo = activitySource => true, Sample = (ref ActivityCreationOptions _) => ActivitySamplingResult.AllData, ActivityStarted = activity => { - Assert.Equal("0123456789abcdef", Activity.Current.ParentSpanId.ToHexString()); + parentSpanId = Activity.Current.ParentSpanId.ToHexString(); } }; @@ -518,7 +519,9 @@ public void ActivityListenersAreCalled() {"baggage", "Key1=value1, Key2=value2"} } }); + hostingApplication.CreateContext(features); + Assert.Equal("0123456789abcdef", parentSpanId); } @@ -533,7 +536,7 @@ private static void AssertProperty(object o, string name) } private static HostingApplication CreateApplication(out FeatureCollection features, - DiagnosticListener diagnosticListener = null, ILogger logger = null, Action configure = null) + DiagnosticListener diagnosticListener = null, ActivitySource activitySource = null, ILogger logger = null, Action configure = null) { var httpContextFactory = new Mock(); @@ -548,6 +551,7 @@ private static HostingApplication CreateApplication(out FeatureCollection featur ctx => Task.CompletedTask, logger ?? new NullScopeLogger(), diagnosticListener ?? new NoopDiagnosticListener(), + activitySource ?? new ActivitySource("Microsoft.AspNetCore"), httpContextFactory.Object); return hostingApplication; diff --git a/src/Hosting/Hosting/test/HostingApplicationTests.cs b/src/Hosting/Hosting/test/HostingApplicationTests.cs index 4dd986371860..cbee0c783c54 100644 --- a/src/Hosting/Hosting/test/HostingApplicationTests.cs +++ b/src/Hosting/Hosting/test/HostingApplicationTests.cs @@ -102,6 +102,7 @@ private static HostingApplication CreateApplication(IHttpContextFactory httpCont ctx => Task.CompletedTask, NullLogger.Instance, new DiagnosticListener("Microsoft.AspNetCore"), + new ActivitySource("Microsoft.AspNetCore"), httpContextFactory); return hostingApplication;