TestServer.CreateClient() produces an HttpClient that does not auto-inject distributed tracing when Activity is used #24633
Labels
affected-very-few
This issue impacts very few customers
area-networking
Includes servers, yarp, json patch, bedrock, websockets, http client factory, and http abstractions
bug
This issue describes a behavior which is not expected - a bug.
severity-nice-to-have
This label is used by an internal tool
Milestone
Describe the bug
CreateClient()
produces anHttpClient
object that does not auto-inject correlation data when aSystem.Diagnostics.Activity
is instantiated and started (using theStart()
method).Background information about how to use
Activity
to send correlation data can be found here: https://devblogs.microsoft.com/aspnet/improvements-in-net-core-3-0-for-troubleshooting-and-monitoring-distributed-apps/ (refer to the "Initiate distributed trace in .NET Core 3.0 app" section).Though the (docs.microsoft.com)"Test ASP.NET Core middleware" guidance states that
TestServer
"does not try to replicate allHttpClient
behavior", havingTestServer
create anHttpClient
object such that propagating any correlation data that may be found in the HTTP request header would make functional/integration tests of middleware more complete and righteous as distributed tracing & correlation support is significant to the story of developing & testing ASP.NET Core apps.To Reproduce
Suppose we had a test method like the sample one in (docs.microsoft.com)"Send requests with HttpClient". In addition, we'll wrap the call with an
Activity
:I'd expect that when the registered middleware code is reached, the "traceparent" header would be valued (since the Activity ID's format was set to the W3C standard above). However, when the test is run, the expected "traceparent" header does not exist in the request.
If you were to instantiate a new
System.Net.Http.HttpClient
(i.e.HttpClient httpClient = new HttpClient()
) in the unit test (not fromTestServer.CreateClient()
), the "traceparent" header would be present along with the generated value.Just for your information, my team has developed a workaround so that our instantiated
TestServer
objects in our functional/integration tests produces anHttpClient
that supports distributed tracing (just as if you were instantiating a new ``System.Net.Http.HttpClientobject outside the
TestServer`) using an extension method:Exceptions (if any)
Further technical details
dotnet --info
The text was updated successfully, but these errors were encountered: