Skip to content

Commit 8df6490

Browse files
authored
Migrate to ActivitySource (#30089)
* WIP: Migrate to ActivitySource * Add ActivityListener test * Fix remaining tests * Remove dummy listener * Use ActivitySource.CreateActivity APIs * Undo changes to Activity.Stop * Undo test change
1 parent 853eabf commit 8df6490

File tree

2 files changed

+48
-5
lines changed

2 files changed

+48
-5
lines changed

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

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,9 @@ 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+
2730
private readonly DiagnosticListener _diagnosticListener;
2831
private readonly ILogger _logger;
2932

@@ -46,11 +49,13 @@ public void BeginRequest(HttpContext httpContext, HostingApplication.Context con
4649
}
4750

4851
var diagnosticListenerEnabled = _diagnosticListener.IsEnabled();
52+
var diagnosticListenerActivityCreationEnabled = (diagnosticListenerEnabled && _diagnosticListener.IsEnabled(ActivityName, httpContext));
4953
var loggingEnabled = _logger.IsEnabled(LogLevel.Critical);
5054

51-
if (loggingEnabled || (diagnosticListenerEnabled && _diagnosticListener.IsEnabled(ActivityName, httpContext)))
55+
56+
if (loggingEnabled || diagnosticListenerActivityCreationEnabled || _activitySource.HasListeners())
5257
{
53-
context.Activity = StartActivity(httpContext, out var hasDiagnosticListener);
58+
context.Activity = StartActivity(httpContext, loggingEnabled, diagnosticListenerActivityCreationEnabled, out var hasDiagnosticListener);
5459
context.HasDiagnosticListener = hasDiagnosticListener;
5560
}
5661

@@ -245,11 +250,20 @@ private static void RecordRequestStartEventLog(HttpContext httpContext)
245250
}
246251

247252
[MethodImpl(MethodImplOptions.NoInlining)]
248-
private Activity StartActivity(HttpContext httpContext, out bool hasDiagnosticListener)
253+
private Activity? StartActivity(HttpContext httpContext, bool loggingEnabled, bool diagnosticListenerActivityCreationEnabled, out bool hasDiagnosticListener)
249254
{
250-
var activity = new Activity(ActivityName);
255+
var activity = _activitySource.CreateActivity(ActivityName, ActivityKind.Server);
256+
if (activity is null && (loggingEnabled || diagnosticListenerActivityCreationEnabled))
257+
{
258+
activity = new Activity(ActivityName);
259+
}
251260
hasDiagnosticListener = false;
252261

262+
if (activity is null)
263+
{
264+
return null;
265+
}
266+
253267
var headers = httpContext.Request.Headers;
254268
if (!headers.TryGetValue(HeaderNames.TraceParent, out var requestId))
255269
{

src/Hosting/Hosting/test/HostingApplicationDiagnosticsTests.cs

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -362,6 +362,7 @@ public void ActivityBaggagePrefersW3CBaggageHeaderName()
362362
Assert.Contains(Activity.Current.Baggage, pair => pair.Key == "Key2" && pair.Value == "value4");
363363
}
364364

365+
365366
[Fact]
366367
public void ActivityBaggagePreservesItemsOrder()
367368
{
@@ -465,7 +466,7 @@ public void ActivityTraceParentAndTraceStateFromHeaders()
465466
}
466467

467468
[Fact]
468-
public void ActivityOnExportHookIsCalled()
469+
public void ActivityOnImportHookIsCalled()
469470
{
470471
var diagnosticListener = new DiagnosticListener("DummySource");
471472
var hostingApplication = CreateApplication(out var features, diagnosticListener: diagnosticListener);
@@ -492,6 +493,34 @@ public void ActivityOnExportHookIsCalled()
492493
Assert.True(Activity.Current.Recorded);
493494
}
494495

496+
[Fact]
497+
public void ActivityListenersAreCalled()
498+
{
499+
var hostingApplication = CreateApplication(out var features);
500+
using var listener = new ActivityListener
501+
{
502+
ShouldListenTo = activitySource => true,
503+
Sample = (ref ActivityCreationOptions<ActivityContext> _) => ActivitySamplingResult.AllData,
504+
ActivityStarted = activity =>
505+
{
506+
Assert.Equal("0123456789abcdef", Activity.Current.ParentSpanId.ToHexString());
507+
}
508+
};
509+
510+
ActivitySource.AddActivityListener(listener);
511+
512+
features.Set<IHttpRequestFeature>(new HttpRequestFeature()
513+
{
514+
Headers = new HeaderDictionary()
515+
{
516+
{"traceparent", "00-0123456789abcdef0123456789abcdef-0123456789abcdef-01"},
517+
{"tracestate", "TraceState1"},
518+
{"baggage", "Key1=value1, Key2=value2"}
519+
}
520+
});
521+
hostingApplication.CreateContext(features);
522+
}
523+
495524

496525
private static void AssertProperty<T>(object o, string name)
497526
{

0 commit comments

Comments
 (0)