Skip to content

Commit 8ce8411

Browse files
committed
Add ActivityListener test
1 parent 0f10c14 commit 8ce8411

File tree

2 files changed

+56
-10
lines changed

2 files changed

+56
-10
lines changed

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

Lines changed: 26 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -55,14 +55,14 @@ public void BeginRequest(HttpContext httpContext, HostingApplication.Context con
5555
// TODO: If loggingEnabled check to change to if ActivityPropogation options are enabled
5656
if (loggingEnabled || (diagnosticListenerEnabled && _diagnosticListener.IsEnabled(ActivityName, httpContext)))
5757
{
58-
// Review: Force creation of Activity by creating dummy listener
5958
_dummyListener = new ActivityListener()
6059
{
6160
ShouldListenTo = activitySource => activitySource.Name.Equals(ActivitySourceName),
6261
Sample = (ref ActivityCreationOptions<ActivityContext> _) => ActivitySamplingResult.PropagationData
6362
};
6463
ActivitySource.AddActivityListener(_dummyListener);
6564
}
65+
6666
context.Activity = StartActivity(httpContext, out var hasDiagnosticListener);
6767
context.HasDiagnosticListener = hasDiagnosticListener;
6868

@@ -269,7 +269,12 @@ private static void RecordRequestStartEventLog(HttpContext httpContext)
269269
hasDiagnosticListener = true;
270270
}
271271

272-
// TODO: Don't look at headers if there no listeners
272+
// Short-circuit to avoid doing an expensive header lookup
273+
if (!(hasDiagnosticListener || _activitySource.HasListeners()))
274+
{
275+
return null;
276+
}
277+
273278
var headers = httpContext.Request.Headers;
274279
if (!headers.TryGetValue(HeaderNames.TraceParent, out var requestId))
275280
{
@@ -307,6 +312,12 @@ private static void RecordRequestStartEventLog(HttpContext httpContext)
307312

308313
// Review: Breaking change: We will no longer fire OnActivityImport before Activity.Start()
309314
_diagnosticListener.OnActivityImport(activity, httpContext);
315+
316+
if (hasDiagnosticListener)
317+
{
318+
//Review: Do we need to explicity call this?
319+
_diagnosticListener.Write(ActivityStartKey, httpContext);
320+
}
310321
}
311322
return activity;
312323
}
@@ -316,15 +327,20 @@ private void StopActivity(HttpContext httpContext, Activity activity, bool hasDi
316327
{
317328
if (hasDiagnosticListener)
318329
{
319-
// Stop sets the end time if it was unset, but we want it set before we issue the write
320-
// so we do it now.
321-
if (activity.Duration == TimeSpan.Zero)
322-
{
323-
activity.SetEndTime(DateTime.UtcNow);
324-
}
325-
_diagnosticListener.Write(ActivityStopKey, httpContext);
330+
StopActivity(activity, httpContext);
331+
}
332+
activity.Stop();
333+
}
334+
335+
private void StopActivity(Activity activity, HttpContext httpContext)
336+
{
337+
// Stop sets the end time if it was unset, but we want it set before we issue the write
338+
// so we do it now.
339+
if (activity.Duration == TimeSpan.Zero)
340+
{
341+
activity.SetEndTime(DateTime.UtcNow);
326342
}
327-
activity.Dispose();
343+
_diagnosticListener.Write(ActivityStopKey, httpContext);
328344
}
329345
}
330346
}

src/Hosting/Hosting/test/HostingApplicationDiagnosticsTests.cs

Lines changed: 30 additions & 0 deletions
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
{
@@ -494,6 +495,35 @@ public void ActivityOnImportHookIsCalled()
494495
Assert.True(Activity.Current.Recorded);
495496
}
496497

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

498528
private static void AssertProperty<T>(object o, string name)
499529
{

0 commit comments

Comments
 (0)