Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Add ActivityListener test
  • Loading branch information
shirhatti committed Feb 23, 2021
commit 8ce84111be3a999b07d66f1a19c19fa8ed8f4454
36 changes: 26 additions & 10 deletions src/Hosting/Hosting/src/Internal/HostingApplicationDiagnostics.cs
Original file line number Diff line number Diff line change
Expand Up @@ -55,14 +55,14 @@ public void BeginRequest(HttpContext httpContext, HostingApplication.Context con
// TODO: If loggingEnabled check to change to if ActivityPropogation options are enabled
if (loggingEnabled || (diagnosticListenerEnabled && _diagnosticListener.IsEnabled(ActivityName, httpContext)))
{
// Review: Force creation of Activity by creating dummy listener
_dummyListener = new ActivityListener()
{
ShouldListenTo = activitySource => activitySource.Name.Equals(ActivitySourceName),
Sample = (ref ActivityCreationOptions<ActivityContext> _) => ActivitySamplingResult.PropagationData
};
ActivitySource.AddActivityListener(_dummyListener);
}

context.Activity = StartActivity(httpContext, out var hasDiagnosticListener);
context.HasDiagnosticListener = hasDiagnosticListener;

Expand Down Expand Up @@ -269,7 +269,12 @@ private static void RecordRequestStartEventLog(HttpContext httpContext)
hasDiagnosticListener = true;
}

// TODO: Don't look at headers if there no listeners
// Short-circuit to avoid doing an expensive header lookup
if (!(hasDiagnosticListener || _activitySource.HasListeners()))
{
return null;
}

var headers = httpContext.Request.Headers;
if (!headers.TryGetValue(HeaderNames.TraceParent, out var requestId))
{
Expand Down Expand Up @@ -307,6 +312,12 @@ private static void RecordRequestStartEventLog(HttpContext httpContext)

// Review: Breaking change: We will no longer fire OnActivityImport before Activity.Start()
_diagnosticListener.OnActivityImport(activity, httpContext);

if (hasDiagnosticListener)
{
//Review: Do we need to explicity call this?
_diagnosticListener.Write(ActivityStartKey, httpContext);
}
}
return activity;
}
Expand All @@ -316,15 +327,20 @@ private void StopActivity(HttpContext httpContext, Activity activity, bool hasDi
{
if (hasDiagnosticListener)
{
// Stop sets the end time if it was unset, but we want it set before we issue the write
// so we do it now.
if (activity.Duration == TimeSpan.Zero)
{
activity.SetEndTime(DateTime.UtcNow);
}
_diagnosticListener.Write(ActivityStopKey, httpContext);
StopActivity(activity, httpContext);
}
activity.Stop();
}

private void StopActivity(Activity activity, HttpContext httpContext)
{
// Stop sets the end time if it was unset, but we want it set before we issue the write
// so we do it now.
if (activity.Duration == TimeSpan.Zero)
{
activity.SetEndTime(DateTime.UtcNow);
}
activity.Dispose();
_diagnosticListener.Write(ActivityStopKey, httpContext);
}
}
}
30 changes: 30 additions & 0 deletions src/Hosting/Hosting/test/HostingApplicationDiagnosticsTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -362,6 +362,7 @@ public void ActivityBaggagePrefersW3CBaggageHeaderName()
Assert.Contains(Activity.Current.Baggage, pair => pair.Key == "Key2" && pair.Value == "value4");
}


[Fact]
public void ActivityBaggagePreservesItemsOrder()
{
Expand Down Expand Up @@ -494,6 +495,35 @@ public void ActivityOnImportHookIsCalled()
Assert.True(Activity.Current.Recorded);
}

[Fact]
public void ActivityListenersAreCalled()
{
var hostingApplication = CreateApplication(out var features);
using var listener = new ActivityListener
{
ShouldListenTo = activitySource => true,
Sample = (ref ActivityCreationOptions<ActivityContext> _) => ActivitySamplingResult.AllData,
ActivityStarted = activity =>
{
Assert.Equal("0123456789abcdef", Activity.Current.ParentSpanId.ToHexString());
},
//ActivityStopped = activity => Assert..
};

ActivitySource.AddActivityListener(listener);

features.Set<IHttpRequestFeature>(new HttpRequestFeature()
{
Headers = new HeaderDictionary()
{
{"traceparent", "00-0123456789abcdef0123456789abcdef-0123456789abcdef-01"},
{"tracestate", "TraceState1"},
{"baggage", "Key1=value1, Key2=value2"}
}
});
hostingApplication.CreateContext(features);
}


private static void AssertProperty<T>(object o, string name)
{
Expand Down