elastic / apm-agent-dotnet

https://www.elastic.co/guide/en/apm/agent/dotnet/current/index.html
Apache License 2.0
579 stars 207 forks source link

HttpWebRequest not Instrumented w/ Spans (.NET Framework) #1977

Open altnp opened 1 year ago

altnp commented 1 year ago

APM Agent version

1.19

Environment

Windows 11, Server 2020

.NET 4.6+

Describe the bug

Spans for HttpWebRequest are not completed in .NET Framework 4.6+. Spans are open, and the APMLogger reads:

{HttpDiagnosticListenerFullFrameworkImpl} Called with key: `System.Net.Http.Desktop.HttpRequestOut.Start'
{HttpDiagnosticListenerFullFrameworkImpl} Processing start event... Request URL: https://www.google.com/

but the corresponding System.Net.Http.Desktop.HttpRequestOut.Stop is never called to complete the span.

This seems to be because Microsofts's HttpHandlerDiagnosticListener never raises the RaiseResponseEvent for HttpWebRequest.

Even worse, the open http span then breaks subsequent http request tracking (even if they are HttpClient).

To Reproduce

Steps to reproduce the behavior:

Create a console app and manually create a transaction with:

Agent.Tracer.CaptureTransaction("Test", "Console", () =>
{
    var request = WebRequest.CreateHttp("https://www.google.com");
    request.Method = "GET";
    var response = (HttpWebResponse)request.GetResponse();
    Console.WriteLine(response.StatusCode);

    var client = new HttpClient();
    var response2 = client.GetAsync("https://www.google.com").Result;
});

Expected behavior

  1. HttpWebRequest should close the span correctly
  2. If that is not possible, HttpWebRequests should be ignored and not break subsequent HttpClient calls. Also, documentation should be updated as you do not suppport HttpWebRequest as stated here: https://www.elastic.co/guide/en/apm/agent/dotnet/current/supported-technologies.html#supported-networking-client-side-technologies

Actual behavior

  1. HttpWebRequest starts a span, does not complete it, and breaks subsequent HttpClient calls from correctly being instrumented.
z1c0 commented 1 year ago

Thanks for reporting this @altnp, we will look into this issue at our earlier convenience.