Skip to content

Commit bc1ff6a

Browse files
authored
Removed static activity source from hosting (#31483)
* Removed static activity source from hosting - Get it from DI instead * Fixed test
1 parent d1ce542 commit bc1ff6a

8 files changed

+25
-11
lines changed

src/Hosting/Hosting/src/GenericHost/GenericWebHostBuilder.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,7 @@ public GenericWebHostBuilder(IHostBuilder builder, WebHostBuilderOptions options
8686
// We need to flow this differently
8787
services.TryAddSingleton(sp => new DiagnosticListener("Microsoft.AspNetCore"));
8888
services.TryAddSingleton<DiagnosticSource>(sp => sp.GetRequiredService<DiagnosticListener>());
89+
services.TryAddSingleton(sp => new ActivitySource("Microsoft.AspNetCore"));
8990

9091
services.TryAddSingleton<IHttpContextFactory, DefaultHttpContextFactory>();
9192
services.TryAddScoped<IMiddlewareFactory, MiddlewareFactory>();

src/Hosting/Hosting/src/GenericHost/GenericWebHostedService.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ public GenericWebHostService(IOptions<GenericWebHostServiceOptions> options,
2424
IServer server,
2525
ILoggerFactory loggerFactory,
2626
DiagnosticListener diagnosticListener,
27+
ActivitySource activitySource,
2728
IHttpContextFactory httpContextFactory,
2829
IApplicationBuilderFactory applicationBuilderFactory,
2930
IEnumerable<IStartupFilter> startupFilters,
@@ -35,6 +36,7 @@ public GenericWebHostService(IOptions<GenericWebHostServiceOptions> options,
3536
Logger = loggerFactory.CreateLogger("Microsoft.AspNetCore.Hosting.Diagnostics");
3637
LifetimeLogger = loggerFactory.CreateLogger("Microsoft.Hosting.Lifetime");
3738
DiagnosticListener = diagnosticListener;
39+
ActivitySource = activitySource;
3840
HttpContextFactory = httpContextFactory;
3941
ApplicationBuilderFactory = applicationBuilderFactory;
4042
StartupFilters = startupFilters;
@@ -48,6 +50,7 @@ public GenericWebHostService(IOptions<GenericWebHostServiceOptions> options,
4850
// Only for high level lifetime events
4951
public ILogger LifetimeLogger { get; }
5052
public DiagnosticListener DiagnosticListener { get; }
53+
public ActivitySource ActivitySource { get; }
5154
public IHttpContextFactory HttpContextFactory { get; }
5255
public IApplicationBuilderFactory ApplicationBuilderFactory { get; }
5356
public IEnumerable<IStartupFilter> StartupFilters { get; }
@@ -111,7 +114,7 @@ public async Task StartAsync(CancellationToken cancellationToken)
111114
application = ErrorPageBuilder.BuildErrorPageApplication(HostingEnvironment.ContentRootFileProvider, Logger, showDetailedErrors, ex);
112115
}
113116

114-
var httpApplication = new HostingApplication(application, Logger, DiagnosticListener, HttpContextFactory);
117+
var httpApplication = new HostingApplication(application, Logger, DiagnosticListener, ActivitySource, HttpContextFactory);
115118

116119
await Server.StartAsync(httpApplication, cancellationToken);
117120

src/Hosting/Hosting/src/Internal/HostingApplication.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,11 @@ public HostingApplication(
2323
RequestDelegate application,
2424
ILogger logger,
2525
DiagnosticListener diagnosticSource,
26+
ActivitySource activitySource,
2627
IHttpContextFactory httpContextFactory)
2728
{
2829
_application = application;
29-
_diagnostics = new HostingApplicationDiagnostics(logger, diagnosticSource);
30+
_diagnostics = new HostingApplicationDiagnostics(logger, diagnosticSource, activitySource);
3031
if (httpContextFactory is DefaultHttpContextFactory factory)
3132
{
3233
_defaultHttpContextFactory = factory;

src/Hosting/Hosting/src/Internal/HostingApplicationDiagnostics.cs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,16 +24,15 @@ internal class HostingApplicationDiagnostics
2424
private const string DeprecatedDiagnosticsEndRequestKey = "Microsoft.AspNetCore.Hosting.EndRequest";
2525
private const string DiagnosticsUnhandledExceptionKey = "Microsoft.AspNetCore.Hosting.UnhandledException";
2626

27-
private const string ActivitySourceName = "Microsoft.AspNetCore.Hosting";
28-
private static readonly ActivitySource _activitySource = new ActivitySource(ActivitySourceName);
29-
27+
private readonly ActivitySource _activitySource;
3028
private readonly DiagnosticListener _diagnosticListener;
3129
private readonly ILogger _logger;
3230

33-
public HostingApplicationDiagnostics(ILogger logger, DiagnosticListener diagnosticListener)
31+
public HostingApplicationDiagnostics(ILogger logger, DiagnosticListener diagnosticListener, ActivitySource activitySource)
3432
{
3533
_logger = logger;
3634
_diagnosticListener = diagnosticListener;
35+
_activitySource = activitySource;
3736
}
3837

3938
[MethodImpl(MethodImplOptions.AggressiveInlining)]

src/Hosting/Hosting/src/Internal/WebHost.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -155,8 +155,9 @@ public virtual async Task StartAsync(CancellationToken cancellationToken = defau
155155
await _hostedServiceExecutor.StartAsync(cancellationToken).ConfigureAwait(false);
156156

157157
var diagnosticSource = _applicationServices.GetRequiredService<DiagnosticListener>();
158+
var activitySource = _applicationServices.GetRequiredService<ActivitySource>();
158159
var httpContextFactory = _applicationServices.GetRequiredService<IHttpContextFactory>();
159-
var hostingApp = new HostingApplication(application, _logger, diagnosticSource, httpContextFactory);
160+
var hostingApp = new HostingApplication(application, _logger, diagnosticSource, activitySource, httpContextFactory);
160161
await Server.StartAsync(hostingApp, cancellationToken).ConfigureAwait(false);
161162
_startedServer = true;
162163

src/Hosting/Hosting/src/WebHostBuilder.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -292,6 +292,7 @@ private IServiceCollection BuildCommonServices(out AggregateException? hostingSt
292292

293293
services.TryAddSingleton(sp => new DiagnosticListener("Microsoft.AspNetCore"));
294294
services.TryAddSingleton<DiagnosticSource>(sp => sp.GetRequiredService<DiagnosticListener>());
295+
services.TryAddSingleton(sp => new ActivitySource("Microsoft.AspNetCore"));
295296

296297
services.AddTransient<IApplicationBuilderFactory, ApplicationBuilderFactory>();
297298
services.AddTransient<IHttpContextFactory, DefaultHttpContextFactory>();
@@ -346,6 +347,9 @@ private void AddApplicationServices(IServiceCollection services, IServiceProvide
346347
var listener = hostingServiceProvider.GetService<DiagnosticListener>();
347348
services.Replace(ServiceDescriptor.Singleton(typeof(DiagnosticListener), listener!));
348349
services.Replace(ServiceDescriptor.Singleton(typeof(DiagnosticSource), listener!));
350+
351+
var activitySource = hostingServiceProvider.GetService<ActivitySource>();
352+
services.Replace(ServiceDescriptor.Singleton(typeof(ActivitySource), activitySource!));
349353
}
350354

351355
private string ResolveContentRootPath(string contentRootPath, string basePath)

src/Hosting/Hosting/test/HostingApplicationDiagnosticsTests.cs

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -390,7 +390,7 @@ public void ActivityBaggagePreservesItemsOrder()
390390
hostingApplication.CreateContext(features);
391391
Assert.Equal("Microsoft.AspNetCore.Hosting.HttpRequestIn", Activity.Current.OperationName);
392392

393-
var expectedBaggage = new []
393+
var expectedBaggage = new[]
394394
{
395395
KeyValuePair.Create("Key1","value1"),
396396
KeyValuePair.Create("Key2","value2"),
@@ -493,17 +493,18 @@ public void ActivityOnImportHookIsCalled()
493493
Assert.True(Activity.Current.Recorded);
494494
}
495495

496-
[Fact(Skip = "https://github.com/dotnet/aspnetcore/issues/30582")]
496+
[Fact]
497497
public void ActivityListenersAreCalled()
498498
{
499499
var hostingApplication = CreateApplication(out var features);
500+
var parentSpanId = "";
500501
using var listener = new ActivityListener
501502
{
502503
ShouldListenTo = activitySource => true,
503504
Sample = (ref ActivityCreationOptions<ActivityContext> _) => ActivitySamplingResult.AllData,
504505
ActivityStarted = activity =>
505506
{
506-
Assert.Equal("0123456789abcdef", Activity.Current.ParentSpanId.ToHexString());
507+
parentSpanId = Activity.Current.ParentSpanId.ToHexString();
507508
}
508509
};
509510

@@ -518,7 +519,9 @@ public void ActivityListenersAreCalled()
518519
{"baggage", "Key1=value1, Key2=value2"}
519520
}
520521
});
522+
521523
hostingApplication.CreateContext(features);
524+
Assert.Equal("0123456789abcdef", parentSpanId);
522525
}
523526

524527

@@ -533,7 +536,7 @@ private static void AssertProperty<T>(object o, string name)
533536
}
534537

535538
private static HostingApplication CreateApplication(out FeatureCollection features,
536-
DiagnosticListener diagnosticListener = null, ILogger logger = null, Action<DefaultHttpContext> configure = null)
539+
DiagnosticListener diagnosticListener = null, ActivitySource activitySource = null, ILogger logger = null, Action<DefaultHttpContext> configure = null)
537540
{
538541
var httpContextFactory = new Mock<IHttpContextFactory>();
539542

@@ -548,6 +551,7 @@ private static HostingApplication CreateApplication(out FeatureCollection featur
548551
ctx => Task.CompletedTask,
549552
logger ?? new NullScopeLogger(),
550553
diagnosticListener ?? new NoopDiagnosticListener(),
554+
activitySource ?? new ActivitySource("Microsoft.AspNetCore"),
551555
httpContextFactory.Object);
552556

553557
return hostingApplication;

src/Hosting/Hosting/test/HostingApplicationTests.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,7 @@ private static HostingApplication CreateApplication(IHttpContextFactory httpCont
102102
ctx => Task.CompletedTask,
103103
NullLogger.Instance,
104104
new DiagnosticListener("Microsoft.AspNetCore"),
105+
new ActivitySource("Microsoft.AspNetCore"),
105106
httpContextFactory);
106107

107108
return hostingApplication;

0 commit comments

Comments
 (0)